关于matplotlib库的一些练习
import matplotlib.pyplotas plt
plt.rcParams['font.sans-serif'] = ['SimHei']# 设置字体为黑体
plt.figure(figsize=(8,4),dpi=100)#设置尺寸与分辨率
dict_egg= {'1月':43,'2月':52,'3月':59,'4月':63,'5月':68,'6月':79}
dict_apple= {'1月':33,'2月':32,'3月':39,'4月':43,'5月':52}
plt.plot(dict_egg.keys(), dict_egg.values(),label='鸡蛋',color='r')
plt.bar(dict_apple.keys(), dict_apple.values(),label='苹果',color='b',width=0.3)
plt.title('商品销售情况')#表名
for month, salesin dict_egg.items():#添加数据标签sales+1表在图上一个单位显示标签
plt.text(month,sales+1,f'{sales}',ha='center',fontsize=9)
plt.legend()#显示图例
plt.xlabel(xlabel='月份',fontsize=13,color='black',loc='right')#X轴设置
plt.ylabel(ylabel='销量',fontsize=13,color='black',loc='top',rotation=0)#Y轴设置
plt.show()