Matplotlib自动图例外图 [英] Matplotlib automatic legend outside plot
问题描述
我正在尝试在Python的matplotlib图中使用关键字bbox_to_anchor()
.
I am trying to use the keyword bbox_to_anchor()
in a matplotlib plot in Python.
这是我制作的一个非常基本的情节:
Here is a very basic plot that I have produced:
import matplotlib.pyplot as plt
x = [1,2,3]
plt.subplot(211)
plt.plot(x, label="test1")
plt.plot([3,2,1], label="test2")
plt.legend(bbox_to_anchor=(0, -0.15, 1, 0), loc=2, ncol=2, mode="expand", borderaxespad=0)
plt.show()
它基于此处的示例: http://matplotlib.org/users/legend_guide.html#plotting-guide-legend
It is based on the example here: http://matplotlib.org/users/legend_guide.html#plotting-guide-legend
我正在尝试使用bbox_to_anchor()
自动将图例放置在图外.在此示例中,bbox_to_anchor()
列出了4个参数.
I am trying to automatically place the legend outside the plot using bbox_to_anchor()
. In this example, bbox_to_anchor()
has 4 arguments listed.
在此特定示例中(上图),图例位于图形下方,因此每次更改图形(字体大小,轴标题已删除等)时,都需要手动输入数字-0.15. 是否可以在以下情况下自动计算这4个数字?:
In this particular example (above), the legend is placed below the plot so the number -0.15 needs to be manually entered each time a plot is changed (font size, axis title removed, etc.). Is it possible to automatically calculate these 4 numbers for the following scenarios?:
- 情节下方的传说
- 故事上方的传说
- 图右的传说
如果没有,是否可以在Python中对这些数字做出很好的猜测?
此外,在上面的示例代码中,由于我不了解它们的含义或工作方式,因此我将bbox_to_anchor()
中的最后两个数字设置为1和0. bbox_to_anchor()
中的最后两个数字是什么意思?
Also, in the example code above I have set the last 2 numbers in bbox_to_anchor()
to 1 and 0 since I do not understand what they are or how they work. What do the last 2 numbers in bbox_to_anchor()
mean?
推荐答案
我强烈建议您从重要性的角度使用答案: 如何将图例从剧情中删除
I HIGHLY RECOMMEND USING THE ANSWER FROM ImportanceOfBeingErnest: How to put the legend out of the plot
这个更容易理解:
import matplotlib.pyplot as plt
x = [1,2,3]
plt.subplot(211)
plt.plot(x, label="test1")
plt.plot([3,2,1], label="test2")
plt.legend(bbox_to_anchor=(0, 1), loc='upper left', ncol=1)
plt.show()
现在使用to坐标(x,y)进行播放.对于loc
,您可以使用:
now play with the to coordinates (x,y). For loc
you can use:
valid locations are:
right
center left
upper right
lower right
best
center
lower left
center right
upper left
upper center
lower center
这篇关于Matplotlib自动图例外图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!