Home > Back-end >  problem work with large numbers list in [python]
problem work with large numbers list in [python]

Time:11-04

my program must generate 100 million numbers between zero to infinity with distribution function 1/x^2 and then plot loglog histogram for this . but size of this list is very large and this is my problem. when i tried to plot bar plot it takes long time to run. (this problem is related to probability and statistic course) my code is as below :

def func(x):
  return(1/x**1.5)
x=np.arange(100*10**6)
p=func(x)
plt.loglog(np.log10(x),np.log10(p)) or plot.bar(...)

CodePudding user response:

You cannot log-scaled a negative value array and here p has negative values. However you can get the log-scaled plot of its opposite by directly calculate the log10 of the function func and avoid 0 division error due to underflow:

def log_func(x):
  return -1.5 * np.log10(x)
x = np.arange(1, 100 * 10**6, dtype=np.float32)
p = - log_func(x)
plt.loglog(np.log10(x), p)

CodePudding user response:

If you plan to use a log scale you should probably not use np.arange but rather np.logspace. You do not need 100 million points just to plot a graph (one or a few hundreds is usually more than enough).

Furthermore, as your p has all negative values, you should plot the opposite (positive) value

import matplotlib.pyplot as plt

def func(x):
    return(1/x**1.5)
x = np.logspace(0,8,100)
p = func(x)

plt.loglog(np.log10(x), -np.log10(p))

output:

loglog plot

  • Related