pyplot图例标签被截断 [英] pyplot legend label being truncated

查看:113
本文介绍了pyplot图例标签被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个图例,其中将包含与之对应的行的颜色和标签.我当前的代码正在绘制图例,但仅绘制标签的首字母(D而不是DL11).我想知道如何让我的情节停止删减标签.我希望将来可以添加更多的行和相应的颜色/标签.任何帮助将不胜感激.谢谢

from numpy import *
import matplotlib.pyplot as plt
import pylab


data = loadtxt("/home/***")
d, tno1, qno1 = data[:,1], data[:,2], data[:,3]                         
d, tno1, qno1 = loadtxt("/home/***", usecols = (1,2,3), unpack=True)


plt.plot(tno1, qno1, label='DL11')
plt.legend( ('DL11') )
plt.show()

解决方案

您遇到了一个有趣的拆包问题.

任何一个

 plt.legend()

 plt.legend(('DL11',)) # <- note the comma

将获得所需的结果.

要了解为什么这样做,请参见

不要以为这是一个错误,但它很微妙.

I'm trying to create a legend that will contain the color of the line it corresponds to and the label. My current code is plotting the legend but is only plotting the first letter of the label (D instead of DL11). I'm wondering how I can get my plot to stop truncating the label. I'd like to be able to add more lines and corresponding colors/labels in the future. Any help would be greatly appreciated. thanks

from numpy import *
import matplotlib.pyplot as plt
import pylab


data = loadtxt("/home/***")
d, tno1, qno1 = data[:,1], data[:,2], data[:,3]                         
d, tno1, qno1 = loadtxt("/home/***", usecols = (1,2,3), unpack=True)


plt.plot(tno1, qno1, label='DL11')
plt.legend( ('DL11') )
plt.show()

You have hit an interesting un-packing issue.

Either

 plt.legend()

or

 plt.legend(('DL11',)) # <- note the comma

will get the desired result.

To understand why it does this, see the code at https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py#L422

calling plt.legend(('DL11')) in equivalent to calling plt.legend('DL11') which falls into the len(args) == 1 case, it then zips your string against the list of lines -> generates the label of 'D' as you only have one line.

Don't think this is a bug, but it is subtle.

这篇关于pyplot图例标签被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆