Pregunta

I everybody, I usually use spyder to write in python and I write these simple lines of code for plotting some graph but I can't understand why it doesn't work properly when I run it, but if I copy and paste the lines in the python console it works perfetly. This is the code:

import matplotlib.pyplot as plt
import numpy as np
z=np.arange(0,250,1)
f_z1=np.append([z[0:100]*0],[z[100:150]/2500 -(1/25) ])
f_z3=np.append(f_z2,[z[200:] *0])
plt.plot(z,f_z3)

I like to understand why I have this problem, thank for help.

¿Fue útil?

Solución

Division in Python < 3 works differently from what you may expect if you are used to for instance Matlab. So from a standard Python console you will get this (dividing integers results in an integer):

>>> 1/2
0

This has been changed in Python 3. To get the new behaviour put

from __future__ import division

above all the other imports in your script. Alternatively you could force floating point behaviour as follows:

>>> 1./2.
0.5

The reason why your code works in the Spyder console is because that already does the above import automatically.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top