The default plot puts scientific notation in the x axis labels. These labels are 2,3,4, and 6, so that doesn't make much sense. Traditional methods seem to rely on the 'ScalarFormatter' which, is not used in log space...
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
nu = np.array([34., 10., 3., 2., 1.])
bins = np.array([1.73619534, 2.6928761 , 3.64955685, 4.6062376 , 6.5195991 ])
x_err = (bins[2]-bins[1])/2*(nu/nu)
def func_powerlaw(x, m, c):
return x**m * c
target_func = func_powerlaw
popt, pcov = curve_fit(target_func, bins, nu, maxfev=2000, p0 = np.asarray([-1,34]))
perr = np.sqrt(np.diag(pcov))
x_line = np.linspace(1.7,6.7,41)
fit_curve = target_func(x_line, *popt)
fit_curve_u = target_func(x_line, popt[0]+perr[0],popt[1]+perr[1])
fit_curve_l = target_func(x_line, popt[0]-perr[0],popt[1]-perr[1])
fig, ax = plt.subplots()
# plt.loglog(x,f_x, 'b')
ax.errorbar(bins,nu, yerr=None, xerr=x_err, marker='.',linewidth = 0, elinewidth=2, color = 'k')
ax.loglog(x_line, fit_curve, '-')
ax.fill_between(x_line, fit_curve_l, fit_curve_u,alpha = 0.2)
# labels = ['high', 'low', 37337]
ax.set_xscale('log') # yes this is redundant
ax.set_xticks([2,3,4,5]) #this line ends up not working ):
ax.set_xlim([1.5,8.5])
This question is a dup, found the answer here: How to change log-scale tick labels in matplotlib
Answer:
from matplotlib.ticker import StrMethodFormatter, NullFormatter
ax.xaxis.set_major_formatter(StrMethodFormatter('{x:.1f}'))
ax.xaxis.set_minor_formatter(NullFormatter())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.