如何在图例画布外部将图例绘制成多列? [英] How to plot legend into multiple column outside the plot canvas?
问题描述
我具有一个数据文件,该数据文件由组成131列和4行.我将其绘制到python中,如下所示:
I have a data file which consists of 131 columns and 4 rows. I am plotting it into python as follows
df = pd.read_csv('data.csv')
df.plot(figsize = (15,10))
一旦绘制完成,所有131个图例都像线条图上的巨大塔一样汇聚在一起.
Once it is plotted, all 131 legends are coming together like a huge tower over the line plots.
请在此处查看我得到的图像: 链接到图片,为了更好的理解,我将v82剪掉了
Please see the image here, which I have got : Link to Image, I have clipped after v82 for better understanding
我在Stackoverflow(SO)上找到了一些解决方案,可将图例转移到图中的任何地方,但我找不到任何解决方案可将图例塔分成多个小块,一小块一小块地堆叠.
I have found some solutions on Stackoverflow (SO) to shift legend anywhere in the plot but I could not find any solution to break this legend tower into multiple small-small pieces and stack them one beside another.
此外,我希望我的情节看起来像这样
Moreover, I want my plot something look like this
我想要的情节:
任何帮助都是可取的.谢谢.
Any help would be appreciable. Thank you.
推荐答案
您可以使用loc
在相对坐标中指定图例的位置,并使用ncol
参数将单个图例列拆分为多个列.为此,您需要由df.plot
You can specify the position of the legend in relative coordinates using loc
and use ncol
parameter to split the single legend column into multiple columns. To do so, you need an axis handle returned by the df.plot
df = pd.read_csv('data.csv')
ax = df.plot(figsize = (10,7))
ax.legend(loc=(1.01, 0.01), ncol=4)
plt.tight_layout()
这篇关于如何在图例画布外部将图例绘制成多列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!