Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

python - 如何将图例排除在情节之外(How to put the legend out of the plot)

I have a series of 20 plots (not subplots) to be made in a single figure.

(我要在一个图中制作一系列20个图(不是子图)。)

I want the legend to be outside of the box.

(我希望图例在框外。)

At the same time, I do not want to change the axes, as the size of the figure gets reduced.

(同时,由于图形尺寸变小,我不想更改轴。)

Kindly help me for the following queries:

(请帮助我进行以下查询:)

  1. I want to keep the legend box outside the plot area.

    (我想将图例框保留在绘图区域之外。)

    (I want the legend to be outside at the right side of the plot area).

    ((我希望图例位于绘图区域的右侧)。)

  2. Is there anyway that I reduce the font size of the text inside the legend box, so that the size of the legend box will be small.

    (无论如何,我是否减小了图例框内文本的字体大小,以使图例框的大小变小。)

  ask by pottigopi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a number of ways to do what you want.

(有很多方法可以做您想要的。)

To add to what @inalis and @Navi already said, you can use the bbox_to_anchor keyword argument to place the legend partially outside the axes and/or decrease the font size.

(要添加@inalis和@Navi所说的内容,可以使用bbox_to_anchor关键字参数将图例部分地放置在轴外和/??或减小字体大小。)

Before you consider decreasing the font size (which can make things awfully hard to read), try playing around with placing the legend in different places:

(在考虑减小字体大小(这会使阅读起来非常困难)之前,请尝试将图例放在不同的位置:)

So, let's start with a generic example:

(因此,让我们从一个通用示例开始:)

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(5):
    ax.plot(x, i * x, label='$y = %ix$' % i)

ax.legend()

plt.show()

替代文字

If we do the same thing, but use the bbox_to_anchor keyword argument we can shift the legend slightly outside the axes boundaries:

(如果我们做同样的事情,但是使用bbox_to_anchor关键字参数,我们可以将图例稍微bbox_to_anchor轴边界:)

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(5):
    ax.plot(x, i * x, label='$y = %ix$' % i)

ax.legend(bbox_to_anchor=(1.1, 1.05))

plt.show()

替代文字

Similarly, you can make the legend more horizontal and/or put it at the top of the figure (I'm also turning on rounded corners and a simple drop shadow):

(同样,您可以使图例更加水平和/或将其放在图的顶部(我也打开了圆角和简单的阴影):)

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(5):
    line, = ax.plot(x, i * x, label='$y = %ix$'%i)

ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
          ncol=3, fancybox=True, shadow=True)
plt.show()

替代文字

Alternatively, you can shrink the current plot's width, and put the legend entirely outside the axis of the figure:

(另外,您可以缩小当前图的宽度,并将图例完全放在图的轴外:)

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(5):
    ax.plot(x, i * x, label='$y = %ix$'%i)

# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

plt.show()

替代文字

And in a similar manner, you can shrink the plot vertically, and put the a horizontal legend at the bottom:

(同样,您可以垂直缩小图,将水平图例放在底部:)

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(5):
    line, = ax.plot(x, i * x, label='$y = %ix$'%i)

# Shrink current axis's height by 10% on the bottom
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])

# Put a legend below current axis
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
          fancybox=True, shadow=True, ncol=5)

plt.show()

替代文字

Have a look at the matplotlib legend guide .

(看一下matplotlib图例指南 。)

You might also take a look at plt.figlegend() .

(您还可以查看plt.figlegend() 。)

Hope that helps a bit, anyway!

(希望无论如何能有所帮助!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...