某户全年每月的电费统计如下:220, 175, 198, 187,190,260,350,320,330,230,290,330元。请采用折线图表示电费的每月浮动趋势。 2.某城市四季平均最高气温为23, 34, 26, 17,平均湿度60,75,55,48,请按照如下要求绘制共享坐标轴图像。 (1)温度显示为柱状图(左侧y轴),适度显示为折线图(右侧y轴) (2)x轴为季节,刻度标签字号12,倾斜20度。 (3)左侧y轴标签“平均温度“设为绿色。 (4)右侧y轴代表平均湿度,下限设为40。 (5)柱状图图例显示左上,折线图图例显示右上。
时间: 2023-08-08 14:13:21 浏览: 54
1. 折线图表示电费的每月浮动趋势:
```
import matplotlib.pyplot as plt
x = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
y = [220, 175, 198, 187, 190, 260, 350, 320, 330, 230, 290, 330]
plt.plot(x, y)
plt.title('Electricity Bill Monthly Fluctuation')
plt.xlabel('Month')
plt.ylabel('Electricity Bill (yuan)')
plt.show()
```
2. 共享坐标轴图像,温度显示为柱状图,适度显示为折线图,平均温度标签为绿色,右侧y轴代表平均湿度,下限为40,柱状图图例显示左上,折线图图例显示右上:
```
import matplotlib.pyplot as plt
temp = [23, 34, 26, 17]
humid = [60, 75, 55, 48]
seasons = ['Spring', 'Summer', 'Autumn', 'Winter']
fig, ax1 = plt.subplots()
# plot temperature as a bar chart
ax1.bar(seasons, temp, color='blue', label='Temperature')
ax1.set_xlabel('Season', fontsize=12, fontweight='bold')
ax1.set_ylabel('Average Temperature', color='green', fontsize=12, fontweight='bold')
ax1.tick_params(axis='y', labelcolor='green')
ax1.spines['left'].set_color('green')
ax1.spines['right'].set_color('blue')
ax1.spines['left'].set_linewidth(2)
ax1.spines['right'].set_linewidth(2)
# plot humidity as a line chart
ax2 = ax1.twinx()
ax2.plot(seasons, humid, color='red', label='Humidity')
ax2.set_ylabel('Average Humidity', color='red', fontsize=12, fontweight='bold')
ax2.set_ylim(bottom=40)
ax2.tick_params(axis='y', labelcolor='red')
ax2.spines['right'].set_linewidth(2)
# set title and legend
plt.title('Seasonal Temperature and Humidity', fontsize=14, fontweight='bold')
plt.legend(loc='upper left', bbox_to_anchor=(0.02, 0.98), fontsize=10)
ax2.legend(loc='upper right', bbox_to_anchor=(0.98, 0.98), fontsize=10)
# set x-axis label rotation and font size
plt.xticks(rotation=20, fontsize=12)
plt.show()
```
结果如下图所示:
![image](https://user-images.githubusercontent.com/39282146/132943744-7d3af6a4-1a3b-4bdc-8b9c-9f03ae8b284d.png)