简体   繁体   English

在一行中锚定相同 plot 的两个图例

[英]anchor two legends for the same plot in line one line

I have a plot with two legends that are not of the same height.我有一个 plot 有两个高度不同的图例。 I would like to place them such that their titles start at the same place on the y-axis.我想放置它们,使它们的标题从 y 轴上的同一位置开始。 I know I could go and manually change bbox_to_anchor=(-0.1, -0.3) but I would have to redo it each time there are minor changes.我知道我可以 go 并手动更改bbox_to_anchor=(-0.1, -0.3)但每次有小的更改时我都必须重做。 Is there no way to set this automatically?有没有办法自动设置?

fig = plt.figure(1)
ax = fig.add_subplot(1, 1, 1)
xticks = mtick.FormatStrFormatter(fmt)
yticks = mtick.FormatStrFormatter(fmt)
x1 = plt.scatter([1,2],[2,3], color='b')
x2 = plt.scatter([2,3],[4,3], color='r')
x3 = plt.scatter([1,3],[4,5], color='k')
x4 = plt.scatter(0, 0, color='white')
marker1 = 'marker 1'
marker2 = 'marker 2'
marker3 = 'marker 3'
plt.xlabel("x axis")
plt.ylabel("y axis")
marker4 = ('some text that needs to go here too'+'\n'+'and is very important and just way too long so that it wont ever look good')
legend_two = plt.legend([x1, x4],[marker1, marker4],fontsize=8,
                           title='some other title', title_fontsize=10,
                           bbox_to_anchor=(0.5, -0.3), ncol=1, loc="center left", borderaxespad=0)
plt.legend([x1,x2,x3], [marker1,marker2,marker3], fontsize=8,
           title='some title', title_fontsize=10,
           bbox_to_anchor=(-0.1, -0.3), ncol=3, loc="center left", borderaxespad=0)
plt.gca().add_artist(legend_two)
plt.tight_layout()
plt.show()

In this case, if the coordinate axis of the bounding box is used as the FIGURE reference, it will be displayed at a fixed position without being affected by others.在这种情况下,如果边界框的坐标轴作为图形参考,它将显示在一个固定的 position 处,不受其他影响。 See here for more information on coordinate axes.有关坐标轴的更多信息,请参见此处。 For other examples of responses, please refer to this .有关其他响应示例,请参阅

import matplotlib.pyplot as plt
from matplotlib import ticker

fmt = '$%g$'
fig = plt.figure(1)
ax = fig.add_subplot(1, 1, 1)
xticks = ticker.FormatStrFormatter(fmt)
yticks = ticker.FormatStrFormatter(fmt)
x1 = plt.scatter([1,2],[2,3], color='b')
x2 = plt.scatter([2,3],[4,3], color='r')
x3 = plt.scatter([1,3],[4,5], color='k')
x4 = plt.scatter(0, 0, color='white')
marker1 = 'marker 1'
marker2 = 'marker 2'
marker3 = 'marker 3'
plt.xlabel("x axis")
plt.ylabel("y axis")
marker4 = ('some text that needs to go here too'+'\n'+'and is very important and just way too long so that it wont ever look good')
legend_two = plt.legend([x1, x4],[marker1, marker4],
                        fontsize=8,
                        title='some other title',
                        title_fontsize=10,
                        bbox_to_anchor=(0.5, 0.0), #update
                        bbox_transform=fig.transFigure, # update
                        ncol=1,
                        loc="upper left",
                        borderaxespad=0)
plt.legend([x1,x2,x3], [marker1,marker2,marker3],
           fontsize=8,
           title='some title',
           title_fontsize=10,
           bbox_to_anchor=(0.0, 0.0), # update
           bbox_transform=fig.transFigure, # update
           ncol=3,
           loc="upper left",
           borderaxespad=0)
plt.gca().add_artist(legend_two)
plt.tight_layout()
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM