Чтобы построить график на вторичной оси с помощью Matplotlib, вы можете использовать функцию twinx(). Вот несколько различных методов, которые вы можете использовать:
- Метод 1: использование
twinx()
import matplotlib.pyplot as plt
# Create the first plot
fig, ax1 = plt.subplots()
# Plot on the first axis
ax1.plot(x1, y1, color='blue')
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Y-axis 1', color='blue')
# Create the second axis
ax2 = ax1.twinx()
# Plot on the second axis
ax2.plot(x2, y2, color='red')
ax2.set_ylabel('Y-axis 2', color='red')
# Show the plot
plt.show()
- Метод 2: использование
subplots(2, 1,sharex=True)
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 1, sharex=True)
# Plot on the first axis
axs[0].plot(x1, y1, color='blue')
axs[0].set_ylabel('Y-axis 1', color='blue')
# Plot on the second axis
axs[1].plot(x2, y2, color='red')
axs[1].set_ylabel('Y-axis 2', color='red')
# Show the plot
plt.show()
- Метод 3: использование
twinx()сax.plot()
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
# Plot on the first axis
ax1.plot(x1, y1, color='blue')
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Y-axis 1', color='blue')
# Create the second axis
ax2 = ax1.twinx()
# Plot on the second axis using ax.plot()
ax2.plot(x2, y2, color='red')
ax2.set_ylabel('Y-axis 2', color='red')
# Show the plot
plt.show()