Log scale mathplotlib in Python?

1k views Asked by At

I'm trying to fit a lognormal curve and I'm not sure how to change the y (and x axis) to log scale? I tried using logspace instead of linspace but I got an error

 OverflowError: cannot convert float infinity to integer

I tried:

 x = np.linspace(np.log(min(bins)),np.log(max(bins)),10000)

but that didn't seem to work either . . . I've attached my graph to show you what I mean. I can't really see what's happening around 400 so I would like to log scale the y (and also log-scale the x to compare)

from collections import Counter
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from scipy.stats import lognorm
import numpy as np

data = list(pre_data)
params = (0.40951093774076597, 5.1802137214177684, 60.158303995566413)
shape, loc, scale = params[0], params[1], params[2]
print params
prob = 1-lognorm.cdf(388,shape,loc=params[1], scale=params[2])
print prob * 2994
count, bins, ignored = plt.hist(data,100,normed=True)

mu = np.mean(np.log(data))
sigma = np.std(np.log(data))
x = np.linspace(min(bins),max(bins),10000)
pdf = (np.exp(-(np.log(x)-mu)**2 / (2 * sigma**2)) / (x * sigma * np.sqrt(2*np.pi)))
plt.plot(x,pdf,color='r',linewidth= 2)

enter image description here

1

There are 1 answers

0
O.rka On BEST ANSWER
plt.xscale('log')
plt.yscale('log')