读取Excel数据,plot 画图实例,python编码,横轴显示为日期

import csv
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 解决plot中文显示乱码问题

info=pd.read_csv('./2019-nCoV.csv')
print(info)
date=info['date']
confirm = info['confirm']
predict = info['predict']
addConfirm = info['addConfirm']
addPredict = info['addPredict']
confirmNow = info['confirmNow']
predictConfirmNow = info['predictConfirmNow']
cure = info['cure']
predictCure = info['predictCure']
addCure = info['addCure']

plt.scatter(date, confirmNow, c = 'orange',marker = 'o', s=10, label='实际现存确诊人数')
plt.plot(date, predictConfirmNow, '-', color='orange', linewidth=2, label='预测现存确诊人数')#ms为圆圈大小

plt.scatter(date, cure, c = 'green',marker = 'o', s=10, label='实际治愈人数')
plt.plot(date, predictCure, 'g-', linewidth=2,  label='预测治愈人数')#ms为圆圈大小
plt.legend()
#plt.plot(date, addCure, 'b-', linewidth=2, ms=3, label='实际新增治愈人数')#ms为圆圈大小

plt.title('新冠肺炎全国现存确诊人数、累计治愈人数预测', fontsize=14)
plt.tick_params(axis='both', which='major', labelsize=8)
plt.xlabel('日期', fontsize=12)# 刻度上的大小
plt.ylabel('人数', fontsize=12)
plt.legend()
x_major_locator = MultipleLocator(5)# 把x轴的刻度间隔设置为5,并存在变量里
ax = plt.gca() # ax为两条坐标轴的实例
ax.xaxis.set_major_locator(x_major_locator) # 把x轴的主刻度设置为1的倍数

plt.gcf().autofmt_xdate()#自动旋转日期标记,比如转换为斜的

plt.savefig('figure.eps', dip=3000)
plt.savefig('figure.png', dip=3000)
plt.show()
发布了4 篇原创文章 · 获赞 2 · 访问量 324

猜你喜欢

转载自blog.csdn.net/aspeipei/article/details/104928528