帮我完成可视化分析import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'] temperatures = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 33.4, 23.0, 16.5, 12.0, 6.2] precipitations = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3] evaporations = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3] fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.bar(months, precipitations, label='降水量', color='#B0C4DE') ax1.bar(months, evaporations, bottom=precipitations, label='蒸发量', color='#FFCC00') ax2.plot(months, temperatures, label='平均气温', color='magenta', linestyle='--', marker='*') ax1.set_ylabel('水量 (ml)') ax1.legend(loc='upper left') ax2.set_ylabel('气温 (℃)') ax2.legend(loc='upper right') ax1.set_xlabel('月份') plt.title('平均气温与降水量、蒸发量的关系') plt.show()
时间: 2023-08-06 19:19:49 浏览: 56
好的,这是一个展示平均气温、降水量和蒸发量的柱状图和折线图的可视化分析。这里有三个列表,分别是月份、温度、降水量和蒸发量。以下是完整的代码:
```
import matplotlib.pyplot as plt
# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 数据
months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
temperatures = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 33.4, 23.0, 16.5, 12.0, 6.2]
precipitations = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
evaporations = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
# 创建子图
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
# 绘制柱状图
ax1.bar(months, precipitations, label='降水量', color='#B0C4DE')
ax1.bar(months, evaporations, bottom=precipitations, label='蒸发量', color='#FFCC00')
# 绘制折线图
ax2.plot(months, temperatures, label='平均气温', color='magenta', linestyle='--', marker='*')
# 设置图例和标签
ax1.set_ylabel('水量 (ml)')
ax1.legend(loc='upper left')
ax2.set_ylabel('气温 (℃)')
ax2.legend(loc='upper right')
ax1.set_xlabel('月份')
plt.title('平均气温与降水量、蒸发量的关系')
# 显示图像
plt.show()
```
代码中通过 `subplots()` 方法创建了一个子图对象,然后通过 `bar()` 方法绘制了两个柱状图,分别表示降水量和蒸发量。其中,第二个柱状图使用了 `bottom` 参数,将它放在了第一个柱状图的顶部。然后,通过 `plot()` 方法绘制了平均气温的折线图。最后,调用 `show()` 方法显示图像。