Python - matplotlib 修改 "loglog" 图的 xticks
Python - matplotlib modify xticks for a "loglog" plot
我正在为每个轴绘制对数刻度,但我不需要 x 轴的科学记数法,所以我修改如下:
from matplotlib import pyplot as plt
from matplotlib.ticker import FormatStrFormatter
a = np.array([10.**(-2), 10.**(-3), 5.*10**(-3),10.**(-4), 10.**(-6), 10.**(-7)])
b = np.array([16, 12.5, 14.5, 9.5, 8., 7.5])
axes = plt.subplot(111)
plt.loglog(a,b, 'k.',markersize=10,markerfacecolor='None',label='')
plt.ylim(10**(-6),10**(-1))
plt.xlim(5,30)
plt.subplots_adjust(left=0.15)
plt.legend(loc=2)
axes.xaxis.set_minor_formatter(FormatStrFormatter("%.0f"))
plt.show()
但是,如下图所示,x 轴标签中有 10 个科学计数法...我不知道如何抑制它,只有 10
...
您可以使用 matplotlib.ticker
中的 ScalarFormatter
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
fig = plt.figure()
ax = fig.add_subplot(111)
ax.semilogx(range(100))
ax.set_xscale('log')
ax.set_yscale('log')
ax.xaxis.set_major_formatter(ScalarFormatter())
#ax.yaxis.set_major_formatter(ScalarFormatter()) # for the y axis
fig.show()
我正在为每个轴绘制对数刻度,但我不需要 x 轴的科学记数法,所以我修改如下:
from matplotlib import pyplot as plt
from matplotlib.ticker import FormatStrFormatter
a = np.array([10.**(-2), 10.**(-3), 5.*10**(-3),10.**(-4), 10.**(-6), 10.**(-7)])
b = np.array([16, 12.5, 14.5, 9.5, 8., 7.5])
axes = plt.subplot(111)
plt.loglog(a,b, 'k.',markersize=10,markerfacecolor='None',label='')
plt.ylim(10**(-6),10**(-1))
plt.xlim(5,30)
plt.subplots_adjust(left=0.15)
plt.legend(loc=2)
axes.xaxis.set_minor_formatter(FormatStrFormatter("%.0f"))
plt.show()
但是,如下图所示,x 轴标签中有 10 个科学计数法...我不知道如何抑制它,只有 10
...
您可以使用 matplotlib.ticker
ScalarFormatter
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
fig = plt.figure()
ax = fig.add_subplot(111)
ax.semilogx(range(100))
ax.set_xscale('log')
ax.set_yscale('log')
ax.xaxis.set_major_formatter(ScalarFormatter())
#ax.yaxis.set_major_formatter(ScalarFormatter()) # for the y axis
fig.show()