Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96264

Plotting with multiple Y-axes

When using ax.<plot_function> for plotting objects on a figure. How can I "hold on" the plot and render multiple plots on the same plot?

For example:

f = plt.figure(figsize=(13,6))
ax = f.add_subplot(111)

ax.plot(x1,y1)
ax.plot(x2,y2)

ax.plot(xN,yN)

I have tried adding:

ax.hold(True)

before I start plotting, but it doesn't work. I think the problem is that they all share the same y-scale and I only see the plot with the largest range of values.

How can I plot multiple 1D arrays of different scales on the same plot? Is there any way to plot them with different Y-axis next to each other?

Upvotes: 0

Views: 1165

Answers (1)

DrV
DrV

Reputation: 23500

There should be nothing wrong with the code in the question:

import matplotlib.pyplot as plt
import numpy as np

# some data
x1 = np.linspace(0, 10, 100)
x2 = np.linspace(1, 11, 100)
xN = np.linspace(4, 5, 100)
y1 = np.random.random(100)
y2 = 0.5 + np.random.random(100)
yN = 1 + np.random.random(100)![enter image description here][1]

# and then the code in the question
f = plt.figure(figsize=(13,6))
ax = f.add_subplot(111)

ax.plot(x1,y1)
ax.plot(x2,y2)

ax.plot(xN,yN) 

# save the figure
f.savefig("/tmp/test.png")

Creates:

enter image description here

which should be pretty much what is expected. So the problem is not in the code.

Are you running the commands in a shell? Which one? IPython?

One wild guess: All three plots are plot, but the data in the plots is exactly the same for all plots, and they overlap each other.

Upvotes: 2

Related Questions