python柱状图折线图共⽤⼀个图例_matplotlib:组合不同的图
形并将它们放在⼀个。。。
这能解决你的问题吗?x = [ 1, 2, 3, 5, 10, 100, 1000 ]
y1 = [ 1, 0.822, 0.763, 0.715, 0.680, 0.648, 0.645 ]
y2 = [ 1, 0.859, 0.812, 0.774, 0.746, 0.721, 0.718 ]
import matplotlib.pyplot as plt
ansforms import BlendedGenericTransform
# mode 01 from one case
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
line1, = ax1.plot( x, y1, label='mode 01' )
# mode 01 from other case
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
line2, = ax2.plot( x, y2, label='mode 01' )
# Create new figure and two subplots, sharing both axes
fig3, (ax3, ax4) = plt.subplots(1,2,sharey=True, sharex=True,figsize=(10,5))
# Plot data from fig1 and fig2
matplotlib中subplotline3, = ax3._data()[0], _data()[1])
line4, = ax4._data()[0], _data()[1])
# If possible (easy access to plotting data) use
# ax3.plot(x, y1)
# ax4.lpot(x, y2)
ax3.set_ylabel('y-axis')
# Add legend
fig3.legend((line3, line4),
('label 3', 'label 4'),
loc = 'upper center',
bbox_to_anchor = [0.5, -0.05],
bbox_transform = ansFigure, ansAxes))
# Make space for the legend beneath the subplots
plt.subplots_adjust(bottom = 0.2)
# Show only fig3
fig3.show()
输出如下所⽰
编辑
看看你上传的zip⽂件中的代码,我会说⼤部分请求的功能都实现了?
我看到你已经改变了创建图的函数,使得你的问题的解决⽅案完全不同,因为你不再试图“合并”来⾃不同图形的两个⼦块。您的解决⽅案基本上与我前⾯介绍的解决⽅案相同,因为这两个⽅案都是在同⼀个图形上创建两个作为⼦块的Axes实例(给出所需的布局),然后绘制然后绘制图,⽽不是绘制图,然后提取/移动轴,正如您最初关⼼的问题。
正如我所怀疑的,最简单和最琐碎的解决⽅案是使单个的Axes⼦块成为同⼀个图形,⽽不是将它们绑
定到单独的图形,因为按照注释中的指定,将⼀个Axes实例从⼀个Figure移动到另⼀个Axes实例并不容易(如果可能的话)。“原始”问题似乎仍然很难完成,因为简单地将Axes实例添加到Figure的_axstack中,就很难⾃定义到所需的布局。
对当前代码的ax.legend(...进⾏⼀次修改,使图例⽔平居中,顶部正好位于轴的下⽅:# Add this line
ansforms import BlendedGenericTransform
# Edit the function call to use the BlendedGenericTransform
ax.legend(loc='upper center',
ncol=7,
labelspacing=-0.7,
columnspacing=0.75,
fontsize=8,
handlelength=2.6,
markerscale=0.75,
bbox_to_anchor=(0.5, -0.05),
bbox_transform=ansFigure, ax.transAxes))
这⾥,应该定制bbox_to_anchor参数以适合我们的图形边界。
^{}允许x轴和y轴的变换不同,这在许多情况下⾮常有⽤。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论