数据挖掘Matplotlib 画图示例----plt.plot(折线图)
import random
import matplotlib.pyplot as plt
##这个是解决中文编码问题的,方法一:
#plt.rcParams['font.sans-serif']=['SimHei']
##这个是解决中文编码问题的,方法二:
#见第二张图
#1,准备数据x,y
x = range(60)
y_shanghai = [random.uniform(15,18) for i in x]
y_beijing = [random.uniform(1,3) for i in x]
#2,创建画布
# plt.figure(figsize=(20,8),dpi=80)
figure,axes = plt.subplots(nrows = 1,ncols=2,figsize = (20,8),dpi=85)
#3,绘制图像
# plt.plot(x,y_shanghai,color='r',linestyle = '-.',label = '上海')
# plt.plot(x,y_beijing ,label = '北京')
axes[0].plot(x,y_shanghai,color='r',linestyle = '-.',label = '上海')
axes[1].plot(x,y_beijing ,label = '北京')
#显示标签,图例
# plt.legend(loc=2)
axes[0].legend()
axes[1].legend()
#修改x,y刻度
#准备x的刻度说明
# x_lable = ["11点{}分".format(i) for i in x]
# plt.xticks(x[::5],x_lable[::5])
# plt.yticks(range(0,40,5))
x_lable = ["11点{}分".format(i) for i in x]
axes[0].set_xticks(x[::5])
axes[0].set_xticklabels(x_lable[::5])
axes[0].set_yticks(range(0,40,5))
axes[1].set_xticks(x[::5])
axes[1].set_xticklabels(x_lable[::5])
axes[1].set_yticks(range(0,40,5))
#添加网格
# plt.grid(True,linestyle='--',alpha=0.5)
axes[0].grid(linestyle='--',alpha=0.5)
axes[1].grid(linestyle='--',alpha=0.5)
#添加描述信息
# plt.xlabel('时间')
# plt.ylabel('温度')
# plt.title("11点0分~12点温度变化")
axes[0].set_xlabel('时间变化')
axes[0].set_ylabel('温度变化')
axes[0].set_title("上海11点0分~12点温度变化")
#4,显示图
plt.show()
第二张图的具体描述没有写,大家可以自己完善一下,本文解决了Matplotlib画图中中文现实问题,还有用plt.subplots 同时画两幅图的操作,与注释的plot语法稍微有一点区别 : axes[0].set_方法
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)