前言
matplotlib是基于Python语言的开源项目,旨在为Python提供一个数据绘图包。在使用Python matplotlib库绘制数据图时,需要使用图例标注数据类别,但是传参时,会出现图例解释文字只显示第一个字符,需要在传参时在参数后加一个逗号(应该是python语法,加逗号,才可以把参数理解为元组类型吧),就可解决这个问题,
word-wrap: break-word; margin: 5px 0px; color: rgb(0, 0, 0); font-family: ">示例如下
代码如下复制代码
importnumpy as np
importmatplotlib.pyplot as plt
frommatplotlib.tickerimportMultipleLocator
frompylabimportmpl
xmajorLocator=MultipleLocator(24*3)#将x轴主刻度标签设置为24 * 3的倍数
ymajorLocator=MultipleLocator(100*2)#将y轴主刻度标签设置为100 * 2的倍数
# 设置中文字体
mpl.rcParams['font.sans-serif']=['SimHei']
# 导入文件数据
data=np.loadtxt('H:/dataset/爸爸去哪儿/统计数据_每小时_ba.csv', delimiter=',', dtype=int)
# 截取数组数据
x=data[:,0]
y=data[:,1]
plt.figure(num=1, figsize=(8,6))
ax=plt.subplot(111)
ax.xaxis.set_major_locator(xmajorLocator)
ax.yaxis.set_major_locator(ymajorLocator)
ax.xaxis.grid(True, which='major')#x坐标轴的网格使用主刻度
ax.yaxis.grid(True, which='major')#x坐标轴的网格使用主刻度
plt.xlabel('时间索引')
plt.ylabel('活动频数')
plt.title('折线图')
plt.xlim(0,1152)
plt.ylim(0,2200)
#plt.plot(x, y, 'rs-')
line1=ax.plot(x, y,'b.-')
ax.legend(line1, ('微博'))
plt.show()
显示效果如下
代码修改
代码如下
frompylabimportmpl
xmajorLocator=MultipleLocator(24*3)#将x轴主刻度标签设置为24 * 3的倍数
ymajorLocator=MultipleLocator(100*2)#将y轴主刻度标签设置为100 * 2的倍数
# 设置中文字体
mpl.rcParams['font.sans-serif']=['SimHei']
# 导入文件数据
data=np.loadtxt('H:/dataset/爸爸去哪儿/统计数据_每小时_ba.csv', delimiter=',', dtype=int)
# 截取数组数据
x=data[:,0]
y=data[:,1]
plt.figure(num=1, figsize=(8,6))
ax=plt.subplot(111)
ax.xaxis.set_major_locator(xmajorLocator)
ax.yaxis.set_major_locator(ymajorLocator)
ax.xaxis.grid(True, which='major')#x坐标轴的网格使用主刻度
ax.yaxis.grid(True, which='major')#x坐标轴的网格使用主刻度
plt.xlabel('时间索引')
plt.ylabel('活动频数')
plt.title('折线图')
plt.xlim(0,1152)
plt.ylim(0,2200)
#plt.plot(x, y, 'rs-')
line1=ax.plot(x, y,'b.-')
ax.legend(line1, ('微博',))# 多加一个逗号
plt.show()
显示效果如下
python matplotlib|python使用matplotlib绘图时图例显示问题
http://m.bbyears.com/jiaocheng/126665.html
推荐访问: