问题描述
我正在尝试创建一个图例,其中将包含与之对应的行的颜色和标签.我当前的代码正在绘制图例,但仅绘制标签的首字母(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
将获得所需的结果.
要了解为什么这样做,请参见 https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py#L422
调用plt.legend(('DL11'))
等同于调用plt.legend('DL11')
(属于len(args) == 1
情况),然后将字符串压缩到行列表中->因为只有一行,所以会生成标签"D". /p>
不要以为这是一个错误,但它很微妙.
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图例标签被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!