Добавьте вторичную ось Y в Matplotlib

Чтобы добавить дополнительную ось Y в Matplotlib, вы можете использовать функцию Twinx(). Вот несколько методов, которые вы можете использовать:

Метод 1: использование Twinx()

import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
# Plot data on the primary y-axis (ax1)
ax1.plot(x1, y1, color='red', label='Primary Y-axis')
# Plot data on the secondary y-axis (ax2)
ax2.plot(x2, y2, color='blue', label='Secondary Y-axis')
# Set labels for y-axes
ax1.set_ylabel('Primary Y-axis')
ax2.set_ylabel('Secondary Y-axis')
# Set legend
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.show()

Метод 2: использование Twiny()

import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax2 = ax1.twiny()
# Plot data on the primary y-axis (ax1)
ax1.plot(x1, y1, color='red', label='Primary Y-axis')
# Plot data on the secondary y-axis (ax2)
ax2.plot(x2, y2, color='blue', label='Secondary Y-axis')
# Set labels for y-axes
ax1.set_ylabel('Primary Y-axis')
ax2.set_xlabel('Secondary Y-axis')
# Set legend
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.show()

Метод 3. Использование параметра Secondary_y

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# Plot data on the primary y-axis
ax.plot(x1, y1, color='red', label='Primary Y-axis')
# Create a secondary y-axis
ax2 = ax.twinx()
# Plot data on the secondary y-axis
ax2.plot(x2, y2, color='blue', label='Secondary Y-axis')
# Set labels for y-axes
ax.set_ylabel('Primary Y-axis')
ax2.set_ylabel('Secondary Y-axis')
# Set legend
ax.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.show()