首先填充matplotlib图例的右列 [英] Fill the right column of a matplotlib legend first

查看:78
本文介绍了首先填充matplotlib图例的右列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试将图例拟合到图上,以免模糊图.

Hey I am trying to fit a legend onto a plot so that it doesn't obscure the graph.

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(0,100,11)

plt.plot(X,-X, label='plot 1')
plt.plot(X,-2*X, label='plot 2')
plt.plot(X,-3*X, label='plot 3')

leg=plt.legend(ncol=2)
leg.get_frame().set_visible(False)

plt.show()

因此,在上面的最小工作示例中,我想做的是将图例中的图2"标签移到右列,即直接在图3"下.

So in the minimum working example, above, what I want to be able to do is move the 'plot 2' label in the legend into the right column, i.e. directly under 'plot 3'.

感谢您的任何帮助.

推荐答案

图例从左至右填充在各列中.换句话说,如果您诱使它认为还有另一行(图例中没有任何文本或行颜色),则可以填充图3"下的空间.

The legend is filling in the columns from left to right. In other words, if you trick it into believing that there is another line (without any text or line color in the legend), then you can populate the space under 'plot 3'.

import numpy as np
import matplotlib.pyplot as plt
from pylab import *

X = np.linspace(0,100,11)

plt.plot(X,-X, label='plot 1', color='red')
plt.plot(X,-2*X, label='plot 2', color='green')
plt.plot(X,-3*X, label='plot 3', color='blue')


line1 = Line2D(range(10), range(10), marker='', color="red")
line2 = Line2D(range(10), range(10), marker='',color="green")
line3 = Line2D(range(10), range(10), marker='', color="blue")
line4 = Line2D(range(10), range(10), marker='', color="white")
plt.legend((line1,line4, line3,line2),('plot1','','plot3','plot2'),numpoints=1, loc=4,ncol=2)

plt.show()

这篇关于首先填充matplotlib图例的右列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆