python画图的图例显示不出来_python使用matplotlib绘图时图例显示问题的解决

前言

matplotlib是基于Python语言的开源项目,旨在为Python提供一个数据绘图包。在使用Python matplotlib库绘制数据图时,需要使用图例标注数据类别,但是传参时,会出现图例解释文字只显示第一个字符,需要在传参时在参数后加一个逗号(应该是python语法,加逗号,才可以把参数理解为元组类型吧),就可解决这个问题,

示例如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.tickerimport MultipleLocator

from pylabimport mpl

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()

显示效果如下

1-200930144449.jpg

代码修改

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

from pylabimport mpl

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()

显示效果如下

1-200930144449-50.jpg

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://blog.csdn.net/hfut_jf/article/details/50755260

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值