使用 matplotlib 时不显示第二个 y 轴

Second y-axis not showing when using matplotlib

我正在尝试将 data_1data_2data(示例中提供的数据)在不同尺度上使用 2 个 y 轴。当我 运行 我的代码时,似乎只有 data_2 被绘制。我仅限于为此使用 matplotlib 库。我在这里遗漏了什么吗?

import matplotlib
import matplotlib.pyplot as plt

data_1 = [10751, 12255, 12255]
data_2 = [143, 202, 202]
data   = [1, 2, 3]


# Create Plot
fig3, ax1 = plt.subplots()
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Y1-axis', color = 'black')
plot_1 = ax1.plot(data, data_1, color = 'black')
ax1.tick_params(axis ='y', labelcolor = 'black')
# Adding Twin Axes
ax2 = ax1.twinx()
ax2.set_ylabel('Y2-axis', color = 'green')
plot_2 = ax2.plot(data, data_2, color = 'green')
ax2.tick_params(axis ='y', labelcolor = 'green')

fig3.savefig('x.png')

它们相互重叠。更改线型或轴限制以不同方式绘制它们。

**代码运行正常。 Data_1 和 data_2 重叠。

尝试更改 data_2 值:您将看到两条不同的曲线。 **

import matplotlib
import matplotlib.pyplot as plt

data_1 = [10751, 12255, 12255]
data_2 = [143, 100, 100]
data   = [1, 2, 3]


# Create Plot
fig3, ax1 = plt.subplots()
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Y1-axis', color = 'black')
plot_1 = ax1.plot(data, data_1, color = 'black')
ax1.tick_params(axis ='y', labelcolor = 'black')
# Adding Twin Axes
ax2 = ax1.twinx()
ax2.set_ylabel('Y2-axis', color = 'green')
plot_2 = ax2.plot(data, data_2, color = 'green')
ax2.tick_params(axis ='y', labelcolor = 'green')