图例中的matplotlib数字 [英] matplotlib numbers in legend
问题描述
我有两行内容,我想展示一个图例.问题是,我不能使用不同的样式(--
,:
,-.
),因为样式太少了,我也不能使用标记(+
,*
等). ),因为我需要它们在直线上显示一些点.
I have a couple of lines and I want to show a legend. The problem is, I can't use different styles (--
, :
, -.
) because there are too few of them, and I can't use markers (+
, *
, etc.) because I need them to show some points on the lines.
所以我想出的最好的主意是使用数字.但是我不知道如何用数字创建图例.我什至可以自己在线条附近绘制数字(以将它们放置在最佳位置),但是我该如何用数字绘制图例?
So the best idea I've come up with is to use numbers. But I can't figure how I can create legends with numbers. I can even draw numbers near lines myself (to place them in the best position), but how can I then draw a legend with the numbers?
即代替:
-- H
-.- Li
我想要类似的东西:
1 H
2 Li
推荐答案
也许在混合物中加入了一些乳胶?
Perhaps a little Latex thrown into the mix?
#In which we make a legend; not with lines, but numbers!
import pylab as pl
pl.rc('text', usetex=True)
pl.figure(1)
pl.clf()
ax = pl.subplot(111)
pl.plot(range(0,10), 'k', label = r'\makebox[25]{1\hfill}Bla')
pl.plot(range(1,11), 'k', label = r'\makebox[25]{12\hfill}Bla12')
lgd = pl.legend(handlelength = -0.4)
for k in lgd.get_lines():
k.set_linewidth(0)
pl.draw()
pl.show()
使用具有特定宽度和\ hfill的\ makebox对齐数字/标签,以占用标签未使用的空间.数字不是自动的,但是如果使用循环画线,则可以添加一个计数器来跟踪数字.
The numbers/labels are aligned by using \makebox with specific width and \hfill to take up the space not used by your labels. Numbers are not automatic, but if you use a loop to draw your lines then you could add a counter to keep track of the numbers.
不知道这是否是您的要求的一部分,但是可以通过将行的线宽设置为0并将图例中的保留空间设置为负数来删除这些行.找不到更整洁的方法,因为我相信图例总是要显示一行(例如,您不能将numpoints设置为0).
Don't know if this is part of your requirement, but the lines are removed by setting their linewidth to 0 and making the space reserved in the legend negative. Couldn't find a neater way of doing this as I believe a legend is always meant to show a line (e.g. you can't set numpoints to 0).
您当然也可以在情节的正确位置添加一些文本,而根本不使用图例.
You could of course also just add some text in the right spot in your plot and not use a legend at all.
这篇关于图例中的matplotlib数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!