0

I installed matplotlib 2.1 from version 2.0.2 and my simple 2D x-y plots are now showing the y-axis with a log scale, when before the scale was linear. I am not sure how this is happening.

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.gridspec as gridspec
from numpy import sqrt, pi, exp, linspace, loadtxt
from lmfit import Model

path=r'C:\users\test.mca'
f=open(path,'r')
lines=f.readlines()[14:2060]
x=[]

for i in range(0,2046):
    x.append(i)

plt.figure(figsize=(10,6))
plt.xlim(0,600)
plt.ylim(0,1000)
plt.grid(True)

plt.xlabel('Channels')
plt.ylabel('Counts')

def gaussian(x, amp, cen, wid):
    "1-d gaussian: gaussian(x, amp, cen, wid)"
    return (amp/(sqrt(2*pi)*wid)) * exp(-(x-cen)**2 /(2*wid**2))

gmod = Model(gaussian)
pars = gmod.make_params(amp=15000, wid=14, cen=380)
result = gmod.fit(lines, pars, x=x)

print(result.fit_report())
fwhm=result.params['wid'].value*2.35
ER=100*fwhm/result.params['cen'].value
center=result.params['cen']

plt.title('Gaussian fit\n Center=%.1f FWHM=%.1f ER=%.1f%%' % (center, fwhm, ER))
plt.plot(x, lines,'bo', markersize=4)
plt.plot(x, result.best_fit, 'r-')

plt.show()

Version 2.1: Ver 2.1

Version 2.0.2: Ver 2.0.2

The top figure is plotted using 2.1 and the bottom figure is using 2.0.2. Having some trouble understanding why this is.

1 Answer 1

1

It is not showing a log scale. The axis just has a label for each point.

In matplotlib 2.0 strings were automatically converted to floats internally. This was never the intended usage, but people tended to misuse this to circumvent converting the strings themselves.

From matplotlib 2.1 on, strings are interpreted as strings, meaning they are categories and there will be one label per category.

In both matplotlib 2.0 and 2.1 you should actually convert your strings to floats if they are meant to be interpreted as numbers before plotting them.

lines = np.array(lines).astype(float)
Sign up to request clarification or add additional context in comments.

1 Comment

I was not aware of this (obviously). Thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.