0
点赞
收藏
分享

微信扫一扫

matplot绘图

小猪肥 2022-03-21 阅读 46
python

利用matplot绘制折线图,
要点:1、有间隔的显示横坐标信息;
数据的横坐标为日期,如果每一个都显示出来,横坐标日期将会拥挤在一起,无法看清,因此要有间隔的显示日期,也可以拓展为有间隔的显示其他的数据形式
2、如何保存完整的图
具体内容看代码
代码如下:

import matplotlib as mpl
import matplotlib.pyplot as plt
#绘图部分

times=east_day_volum['日期']
times = [i for i in times][:85]

# 分时间区间,保证最后一位纳入标签
ticks=list(range(0,len(times),5))

if ticks[-1]!=len(times)-1:
    ticks.append(len(times)-1)
    print(ticks)
labels=[times[i].date() for i in ticks]
print(labels)
# 中文和负号的正常显示
mpl.rcParams['font.sans-serif'] = ['Times New Roman']
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
fig= plt.figure(figsize=(8, 4),dpi=100)
# 设置图形的显示风格
# plt.style.use('ggplot')
ax1 = fig.add_subplot(111)
ax1.plot(east_day_volum['Unnamed: 4'][:86],ls = '--',color = 'blue',marker = 'o',linewidth=1.5,label='总客流')
ax1.plot(east_day_volum['男'][:86],ls = '-.',color = 'green',marker = 'x',linewidth=1.5,label='男')
ax1.plot(east_day_volum['女'][:86],ls = ':',color = 'red',marker = '*',linewidth=1.5,label='女')
ax1.legend(loc='upper right', frameon=False,fontsize = 10)

ax1.set_xlabel('时间',fontsize =10)
ax1.set_ylabel('人流量',fontsize =10)

ax1.set(xlim=[0,len(times)-1])
ax1.set_xticks(ticks)
ax1.set_xticklabels(labels, rotation=45, horizontalalignment='right')

ax1.tick_params(labelsize=8)

ax1.set_title('客流统计情况',fontsize =20)
ax1.legend(loc='upper right', frameon=False,fontsize = 10)


plt.savefig('time_distribute.jpg', dpi=300, bbox_inches='tight')  # 加上这个之后可以让图显示全
plt.show()
举报

相关推荐

0 条评论