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?
JavaScript
x
22
22
1
import matplotlib
2
import matplotlib.pyplot as plt
3
4
data_1 = [10751, 12255, 12255]
5
data_2 = [143, 202, 202]
6
data = [1, 2, 3]
7
8
9
# Create Plot
10
fig3, ax1 = plt.subplots()
11
ax1.set_xlabel('X-axis')
12
ax1.set_ylabel('Y1-axis', color = 'black')
13
plot_1 = ax1.plot(data, data_1, color = 'black')
14
ax1.tick_params(axis ='y', labelcolor = 'black')
15
# Adding Twin Axes
16
ax2 = ax1.twinx()
17
ax2.set_ylabel('Y2-axis', color = 'green')
18
plot_2 = ax2.plot(data, data_2, color = 'green')
19
ax2.tick_params(axis ='y', labelcolor = 'green')
20
21
fig3.savefig('x.png')
22
Advertisement
Answer
They are plotted over each other. Change the linestyle or axis limits to plot them differently.