简体   繁体   English

如何使用matplotlib制作第二个垂直轴,左侧带有标签?

[英]How to Make a secondary vertical axis, with labels on the left, using matplotlib?

I was trying to make a graph with two secondary vertical axis (y-axis), with python matplotlib.我试图用 python matplotlib 制作一个带有两个辅助垂直轴(y 轴)的图形。

I was using twinx() method, where one of the two new axis is with the default behavior (labels on the right) and the other with labels on the left , like the example of tan(x) on the figure bellow (created in an specific software).我用twinx()方法,其中,所述两个新的轴中的一个是与所述默认行为(在右侧的标签)和其他与波纹管在左侧标签,像黄褐色(X)的例子(在创建特定的软件)。

Is there an easy way to do that?有没有简单的方法来做到这一点? I'm not restricted to use the twinx() method, if there is another way如果有另一种方法,我不限于使用twinx()方法

在此处输入图片说明

Here is a way to add two secondary y-axis, one towards the inside:这是一种添加两个辅助 y 轴的方法,一个朝向内部:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(.5, 10, 1000)
y1 = np.cos(x)
y2 = np.sin(2 * x)
y3 = np.clip(np.tan(x * .6), -75, 75)

fig, ax1 = plt.subplots()

color = 'dodgerblue'
ax1.set_ylabel('$cos(x)$', color=color)
ax1.plot(x, y1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()
color = 'limegreen'
ax2.set_ylabel('$sin(2 x)$', color=color)
ax2.plot(x, y2, color=color)
ax2.tick_params(axis="y", labelcolor=color)

ax3 = ax1.twinx()
color = 'crimson'
ax3.set_ylabel('$tan(.6 x)$', color=color, labelpad=-40)
ax3.plot(x, y3, color=color)
ax3.tick_params(axis="y", labelcolor=color, direction="in", pad=-5)
plt.setp(ax3.get_yticklabels(), ha="right")

ax1.set_xlim(0, 12)

fig.tight_layout()
plt.show()

结果图

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

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