Controlling the Plot range of matplotlib , use problem of 100% of ram memory

Asked

Viewed 141 times

1

Hello community of Stackoverflow :)

I am developing a small code that will plot you from 2 filled lists from an equation while a while condition is not satisfied.

Once the lists are filled in, I use matplotlib to plot the two-dimensional graph with the extracted values, but it’s a lot of values and I’d like to know if you can only plot points at specific intervals, because if I plot them all, memory usage of my PC goes up to 100% and Python crashes and gives memory error. There would be a way to optimize this process?

For example, using the range of one in a nanometer (which is the case I’m trying to solve)

# Modelando a energia do fóton em uma equação

from matplotlib import pyplot as plt

h = 1.05457168 * 10**(-34) # J * s
c = 299792458.0 #m/s
i = 0.000004 #metros
energias =[] 
wavelenghts=[]


while (i!=0.000007):
    E = (h*c)/i
    energias.append(E)
    wavelenghts.append(i)
    i=i+0.000001

print (energias)
print (wavelenghts)

plt.plot (energias, wavelenghts, color='blue', marker = 'o', linestyle = 'solid')
  • 1

    you can use a for to set an interval for the calculation

  • How? : ( I thought q would fall into the same problem, due to the number of digits between the values.

  • 1

    onosendai has already corrected your script with minimal effort

1 answer

1


Perhaps the problem is with the accuracy of the variable. Change line 12 of

while (i!=0.000007):

for

while (i<=0.000007): # menor ou igual a 0.000007

The comment below from @Eltonnunes is perfect. Just complementing: comparisons of inequity (!=) are problematic when using floating point operations given the imprecision as the calculation involves more and more decimals as a result of the specification IEEE-754.

Another way to avoid this behavior would be to maintain its loop with an entire variable, thus preserving the expected behavior by comparing inequity:

i = 4 #metros
[...]

while (i!=7):
    vi = i/1000000
    E = (h*c)/vi
    energias.append(E)
    wavelenghts.append(vi)
    i=i+1
  • I got it, it worked! But why did it happen? : the

  • 2

    vc put so that it is different from 7e-6, the loop will expect exactly that, IE if it is a value greater or less, it will not stop, the correction was to calculate equation i is smaller, arithmetic operations of fractional numbers house a minuscule variation that hinders the accuracy

  • Got it, thank you very much !

  • 1

    Hello, Onosendai, not to generate this problem would be interesting, but this code is precisely for physical problems and I found this sensational because it turned out that my question ended up serving for the case where the inequality in the code did not allow to operate the values float, I imagined that if I controlled the number of decimal places this would not happen, but it was not the case. And to tell the truth, the values 0.000004 and 0.000007 are the energies associated with the wavelength photons that are in the range visible to the human eye, so these are the values. the/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.