I am trying to plot data_1 and data_2 against data (data supplied in example) using 2 y-axis on different scales. When I run my code it seems only data_2 gets plotted. I am limited to only use the matplotlib library for this. Am I missing something here?
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')
CodePudding user response:
They are plotted over each other. Change the linestyle or axis limits to plot them differently.
CodePudding user response:
**The code works properly. Data_1 and data_2 overlap.
Try to change data_2 values: you will see two different curves. **
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')