2.某城市四季平均最高气温为23, 34, 26, 17,平均湿度60,75,55,48,请按照如下要求绘制共享坐标轴图像。 (1)温度显示为柱状图(左侧y轴),适度显示为折线图(右侧y轴) (2)x轴为季节,刻度标签字号12,倾斜20度。 (3)左侧y轴标签“平均温度“设为绿色。 (4)右侧y轴代表平均湿度,下限设为40。 (5)柱状图图例显示左上,折线图图例显示右上。
时间: 2024-01-24 12:18:37 浏览: 164
以下是Python代码实现:
```python
import matplotlib.pyplot as plt
import numpy as np
# 数据
seasons = ['春季', '夏季', '秋季', '冬季']
temperatures = [23, 34, 26, 17]
humidity = [60, 75, 55, 48]
# 创建画布和坐标轴
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
# 绘制柱状图
ax1.bar(seasons, temperatures, color='orange', label='平均温度')
ax1.set_ylabel('平均温度', color='green')
ax1.tick_params(axis='y', colors='green')
# 绘制折线图
ax2.plot(seasons, humidity, color='blue', label='平均湿度')
ax2.set_ylim(bottom=40)
ax2.set_ylabel('平均湿度')
ax2.tick_params(axis='y', colors='blue')
# 设置x轴
ax1.set_xlabel('季节')
ax1.set_xticklabels(seasons, fontsize=12, rotation=20)
# 添加图例
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.show()
```
生成的图像如下:
![共享坐标轴图像](https://img-blog.csdnimg.cn/20211017180736221.png)
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)