0

I am trying two plot multiple plots on the same figure, however only the first two plots are included. I was expecting to have 5 plots

from numpy import *

from matplotlib.pyplot import *

x = linspace(-1, 1, 200)

y_0 = ones(len(x))
y_1 = x
y_2 = 1/2*(3*x**2-1)
y_3 = 1/2*(5*x**3-3*x)
y_4 = 1/8*(35*x**4-30*x**2+3)

figure()
plot(x, y_0)   # Plot some data
plot(x, y_1)   # Plot some data
plot(x, y_2)   # Plot some data
plot(x, y_3)   # Plot some data
plot(x, y_4)   # Plot some data

grid(True)   # Set the thin grid lines

savefig("plot_1.png")   # Save the figure to a file "png",
                                # "pdf", "eps" and some more file types are valid
show("plot_1.png")

1 Answer 1

1

when you things like y_1 = x you are not making a copy of x just a pointer to the same object.

More importantly if you are using python2

>>> 1/2*(3*x**2-1)
array([ 0.,  0.,  0.,  0.,  0., -0., -0., -0., -0., -0., -0., -0., -0.,
       -0., -0.,  0.,  0.,  0.,  0.,  0.])

because the 1/2 gets turned to zero (I know, one of the most annoying things about python)

So matplotlib might be plotting several graphs but not where you expect. Put in some print() commands to see what you have and generally debug

If it's the python 2 issue this will fix it

y_2 = 1.0/2*(3*x**2-1)
y_3 = 1.0/2*(5*x**3-3*x)
y_4 = 1.0/8*(35*x**4-30*x**2+3)
Sign up to request clarification or add additional context in comments.

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.