Matplotlib中的多重坐标轴,且刻度不同。

106

如何在Matplotlib中实现多个比例尺?我不是指针对同一x轴绘制的主轴和副轴,而是像许多具有不同比例尺的趋势一样绘制在同一y轴上,并且可以通过它们的颜色进行识别。

例如,如果我要绘制trend1 ([0,1,2,3,4])trend2 ([5000,6000,7000,8000,9000])相对于时间,并希望这两个趋势的颜色不同且在Y轴上处于不同的比例尺,那么我该如何使用Matplotlib完成这个任务?

当我查看Matplotlib时,他们说他们目前还没有这个功能,但肯定会考虑添加,有没有方法可以实现这个功能呢?

是否有其他Python绘图工具可以实现此功能?


Matthew Kudija在这里提供了一个更近期的例子链接 - Mike O'Connor
3个回答

127

由于每当我在谷歌上搜索多个y轴时,Steve Tjoa's answer总是第一个且大多数情况下孤立无援,因此我决定添加他的答案的稍微修改版本。这是来自this matplotlib example的方法。

原因:

  • 他的模块有时会在未知情况和神秘的内部错误中失败。
  • 我不喜欢加载我不知道的奇异模块(mpl_toolkits.axisartistmpl_toolkits.axes_grid1)。
  • 下面的代码包含更多明确的命令,解决人们经常遇到的问题(如多个轴的单个图例,使用viridis等),而不是隐含的行为。

Plot

import matplotlib.pyplot as plt 

# Create figure and subplot manually
# fig = plt.figure()
# host = fig.add_subplot(111)

# More versatile wrapper
fig, host = plt.subplots(figsize=(8,5), layout='constrained') # (width, height) in inches
# (see https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html and
# .. https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html)
    
ax2 = host.twinx()
ax3 = host.twinx()
    
host.set_xlim(0, 2)
host.set_ylim(0, 2)
ax2.set_ylim(0, 4)
ax3.set_ylim(1, 65)
    
host.set_xlabel("Distance")
host.set_ylabel("Density")
ax2.set_ylabel("Temperature")
ax3.set_ylabel("Velocity")

color1, color2, color3 = plt.cm.viridis([0, .5, .9])

p1 = host.plot([0, 1, 2], [0, 1, 2],    color=color1, label="Density")
p2 = ax2.plot( [0, 1, 2], [0, 3, 2],    color=color2, label="Temperature")
p3 = ax3.plot( [0, 1, 2], [50, 30, 15], color=color3, label="Velocity")

host.legend(handles=p1+p2+p3, loc='best')

# right, left, top, bottom
ax3.spines['right'].set_position(('outward', 60))

# no x-ticks                 
host.xaxis.set_ticks([])

# Alternatively (more verbose):
# host.tick_params(
#     axis='x',          # changes apply to the x-axis
#     which='both',      # both major and minor ticks are affected
#     bottom=False,      # ticks along the bottom edge are off)
#     labelbottom=False) # labels along the bottom edge are off
# sometimes handy:  direction='in'    

# Move "Velocity"-axis to the left
# ax3.spines['left'].set_position(('outward', 60))
# ax3.spines['left'].set_visible(True)
# ax3.spines['right'].set_visible(False)
# ax3.yaxis.set_label_position('left')
# ax3.yaxis.set_ticks_position('left')

host.yaxis.label.set_color(p1[0].get_color())
ax2.yaxis.label.set_color(p2[0].get_color())
ax3.yaxis.label.set_color(p3[0].get_color())

# For professional typesetting, e.g. LaTeX, use .pgf or .pdf
# For raster graphics use the dpi argument. E.g. '[...].png", dpi=300)'
plt.savefig("pyplot_multiple_y-axis.pdf", bbox_inches='tight')
# bbox_inches='tight': Try to strip excess whitespace
# https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html

12
+1 是为了启用标准的 matplotlib 模块版本。我还会引导当前用户使用现代的、更符合 Python 风格的 subplots() 方法,如此处所述:(https://matplotlib.org/users/recipes.html) 和 jarondl 在这里[https://dev59.com/snA65IYBdhLWcg3w4CwL#mqOeEYcBWogLw_1bMncM] 所强调的那样。幸运的是,它可以与此答案一起使用。你只需要将导入后的两行替换为fig, host = plt.subplots(nrows=1, ncols=1)即可。 - Wayne
7
注意到这个回答仍然允许应用Rutger Kassies的解决方案将次坐标轴(也称为寄生坐标轴)移动到左侧。要实现这个功能,只需将代码中的 par2.spines['right'].set_position(('outward', 60)) 替换为以下 行代码:par2.spines ['left'] .set_position (('outward', 60))
par2.spines ["left"].set_visible(True)
par2.yaxis.set_label_position('left')
par2.yaxis.set_ticks_position('left')
- Wayne
1
这是根据matplotlib页面上显示的示例所述,比host_subplots更易于使用。 - ImportanceOfBeingErnest
2
@Wayne 谢谢你的提示!我已经将它们整合到上面了。 - Suuuehgi
1
做最多魔法的两行是, 第一行:par2 = host.twinx(),第二行: par2.spines['right'].set_position(('outward', 60)) - Stefan

125
如果我理解问题正确的话,您可能对Matplotlib图库中这个示例感兴趣。

要输入图像描述

Yann在上面的评论中提供了一个类似的示例。

编辑 - 修复了上面的链接。从Matplotlib库中复制了相应的代码:

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)

par1 = host.twinx()
par2 = host.twinx()

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right", axes=par2,
                                        offset=(offset, 0))

par2.axis["right"].toggle(all=True)

host.set_xlim(0, 2)
host.set_ylim(0, 2)

host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")

par1.set_ylim(0, 4)
par2.set_ylim(1, 65)

host.legend()

host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())

plt.draw()
plt.show()

#plt.savefig("Test")

8
因为隐藏在链接后面的答案不太有用并且容易失效,所以给出负分。 - dlras2
1
我在任何文档中都找不到get_grid_helper函数的相关说明。它确切地是做什么的? - tommy.carstensen
1
为什么要使用 if 1: - Jonathan Wheeler
1
右轴上的“温度”标签没有显示出来?运行MPL版本2.2.2。 - user1834164
3
par1.axis["right"].toggle(all=True) 缺失! - henry
显示剩余3条评论

80

如果您想要使用辅助 Y 轴进行快速绘图,则可以使用 Pandas 包装函数更轻松地完成,只需两行代码即可。首先绘制第一列,然后使用参数 secondary_y=True 绘制第二列,像这样:

df.A.plot(label="Points", legend=True)
df.B.plot(secondary_y=True, label="Comments", legend=True)

这将会看起来像下面这样:

在此输入图片描述

你还可以做更多的事情。请查看Pandas绘图文档


7
secondary_y=True表示启用图表的第二个y轴。 - Ivelin
我喜欢这个解决方案,但想确保两条线都从图表左侧的同一点开始。你会如何做到这一点? - Alexandros
5
这个方法适用于超过2行吗?看起来这个方法只适用于最多2行。 - tsando

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接