控制图例中的行数 [英] Control the number of rows within a legend
问题描述
我目前正在尝试在单个图上绘制大量数据.我使用重复的颜色和符号来构造我的表示.但是,在绘制最终结果时,图例似乎略有偏离,因为我无法控制其中的行数.因此,与其得到5个重复的绿色,然后5个重复的红色,5个重复的蓝色,然后是另外2个,我得到5 -4 -4 -4(我更喜欢5-5-5-2)
I am currently trying to plot a large amount of data on a single plot. I have structured my representation using repeated colors and symbols. However, when plotting the final results, the legend appears slightly off because I cannot control the number of rows within it. Thus, instead of getting 5 repeated green, then 5 repeated red, 5 repeated blue then 2 other, I get 5 -4 -4 -4 (where I would have prefered 5 - 5 - 5 - 2)
您可以在附件图像中清楚地看到这一点.
You can clearly see this in attached image.
现在,我将以下选项用于图例:
Right now I use these options for the legend:
axp.legend(loc="lower right",ncol=4)
推荐答案
我也遇到过几次这个问题,并且如果有更多可用的优雅方法,可以通过在图例中添加虚拟项以填充最后一列来使用此变通方法我也非常有兴趣听到他们的消息.
I also had this problem a couple of times and use this workaround by adding dummy items to the legend to fill the last column, if there are more elegant methods available I would also be very interested to hear about them.
import numpy as np
import matplotlib.pylab as pl
pl.figure()
pl.plot(np.arange(10), np.random.random([10,5]), color='r', label='red')
pl.plot(np.arange(10), np.random.random([10,5]), color='g', label='green')
pl.plot(np.arange(10), np.random.random([10,5]), color='b', label='blue')
pl.plot(np.arange(10), np.random.random([10,2]), color='k', label='black')
# Add empty dummy legend items
pl.plot(np.zeros(1), np.zeros([1,3]), color='w', alpha=0, label=' ')
pl.legend(ncol=4)
这篇关于控制图例中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!