简体   繁体   中英

numpy.linspace - bad labels on the X axis

I have this small plotting program.
But when I run it I notice the labels on the X axis are incorrect.
They go from 0 to 5000 while actually I have the interval [-1.5, 1.5]

1... How can I fix that?

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1.5, 1.5, 5000)
y1 = np.tan(x) * np.arctan(x)
y2 = x * x

plt.plot(y1)
plt.plot(y2)

plt.show()

2... Also, if I change the linspace to call
x = np.linspace(-mt.pi/2.0 + 1/(10**6), mt.pi/2.0 - 1/(10**6), 5000)

I get an even stranger and really incorrect plot.
Something gets messed up completely.
Why? I want to plot these 2 functions in the range (-pi/2, pi/2)
How do I do this?

Try:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1.5, 1.5, 5000)
y1 = np.tan(x) * np.arctan(x)
y2 = x * x
plt.plot(x,y1)
plt.plot(x,y2)
plt.show()

Now x axis values are is between -1.5 and 1.5.

Regarding strange plot in second case just notice that:

np.tan(-1.5) -14.101419947171719

and:

np.tan(-mt.pi/2.0) -1.633123935319537e+16

which is much much bigger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM