1

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')

enter image description here

0

2 Answers 2

1

They are plotted over each other. Change the linestyle or axis limits to plot them differently.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

**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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.