Skip to content Skip to sidebar Skip to footer

Plotting With Multiple Y-axes

When using ax. 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=

Solution 1:

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.

Post a Comment for "Plotting With Multiple Y-axes"