<p>由于<a href="https://stackoverflow.com/a/9103464/3383640">Steve Tjoa's answer</a>总是首先出现,而且当我在Google搜索多个y轴时,大多数情况下是孤独的,所以我决定添加一个稍微修改过的版本。这是来自<a href="https://matplotlib.org/2.2.2/gallery/ticks_and_spines/multiple_yaxis_with_spines.html" rel="noreferrer">this matplotlib example</a>的方法。</p>
<p>原因:</p>
<ul>
<li>他的模块有时会在未知的环境和隐秘的实习错误中失败。</li>
<li>我不喜欢加载我不知道的外来模块(<code>mpl_toolkits.axisartist</code>,<code>mpl_toolkits.axes_grid1</code>)。</li>
<li>下面的代码包含了人们经常遇到的问题(比如多轴的单个图例,使用viridis,…)的显式</em>命令,而不是隐式行为。</li>
</ul>
<p><a href="https://i.stack.imgur.com/POXK4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/POXK4.png" alt="Plot"/></a></p>
<pre><code>import matplotlib.pyplot as plt
fig = plt.figure()
host = fig.add_subplot(111)
par1 = host.twinx()
par2 = host.twinx()
host.set_xlim(0, 2)
host.set_ylim(0, 2)
par1.set_ylim(0, 4)
par2.set_ylim(1, 65)
host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")
color1 = plt.cm.viridis(0)
color2 = plt.cm.viridis(0.5)
color3 = plt.cm.viridis(.9)
p1, = host.plot([0, 1, 2], [0, 1, 2], color=color1,label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], color=color2, label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], color=color3, label="Velocity")
lns = [p1, p2, p3]
host.legend(handles=lns, loc='best')
# right, left, top, bottom
par2.spines['right'].set_position(('outward', 60))
# no x-ticks
par2.xaxis.set_ticks([])
# Sometimes handy, same for xaxis
#par2.yaxis.set_ticks_position('right')
host.yaxis.label.set_color(p1.get_color())
par1.yaxis.label.set_color(p2.get_color())
par2.yaxis.label.set_color(p3.get_color())
plt.savefig("pyplot_multiple_y-axis.png", bbox_inches='tight')
</code></pre>