下面是一个实例,csv文件中存储了一段时间内,某条微博每天的点赞数及评论数。用折线图将两条折线绘制出来。
import csv
import matplotlib
font = {
'family':'SimHei',
'weight':'bold',
'size':12
}
matplotlib.rc("font", **font)
with open('weibo.csv','r')as file:#接受数据传入列表
data=csv.reader(file)
date=next(data)
data1=next(data)
data2=next(data)
x=date[1:]
y1=[int(data1[i]) for i in range(1,len(data1))]
y2=[int(data2[i]) for i in range(1,len(data2))]#把列表中的字符串转换成整数
from matplotlib import pyplot as plt
#设置画布大小
plt.figure(figsize=(20,20))
#画数据
plt.plot(x,y1,color='red',marker='o',label='评论数',linewidth=10,markerfacecolor='blue',markersize=10)
plt.plot(x,y2,color='yellow',marker='o',label='点赞数',linewidth=5,markerfacecolor='black',markersize=10,linestyle='--')
#设置标题
plt.title('微博数据分析',fontsize=30)
ax=plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')#去掉不需要的轴
ax.spines['bottom'].set_position(('data',0))
#设置x轴刻度
plt.xticks(list(range(len(x))),x,rotation=30)
#设置y轴刻度
plt.yticks([500,1000,1500,2000],['一星','二星','三星','四星'],fontsize=15)
#设置坐标轴名称
plt.xlabel('日期',fontdict={'color':'black','size':20})
plt.ylabel('数量',fontdict={'color':'black','size':20},fontsize=20)
for m,n in zip(x,y1):
plt.text(m,n,n,fontsize=25, ha='center', va='bottom')
for m,n in zip(x,y2):
plt.text(m,n,n,fontsize=25, ha='center', va='bottom')
plt.legend(loc='best',fontsize=30)
plt.show()
版权声明:本文为wander_ing原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。