Python 如何使用Matplotlib在日志比例上显示小刻度标签
有人知道如何使用Python/Matplotlib以对数比例显示次要记号的标签吗?您可以使用Python 如何使用Matplotlib在日志比例上显示小刻度标签,python,matplotlib,Python,Matplotlib,有人知道如何使用Python/Matplotlib以对数比例显示次要记号的标签吗?您可以使用plt.tick_参数(axis='y',which='minor')设置次要记号,并使用Matplotlib.tickerFormatterFormatter设置它们的格式。比如说, import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import FormatStrFormatter x = np.lins
plt.tick_参数(axis='y',which='minor')
设置次要记号,并使用Matplotlib.ticker
FormatterFormatter
设置它们的格式。比如说,
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
x = np.linspace(0,4,1000)
y = np.exp(x)
plt.plot(x, y)
ax = plt.gca()
ax.set_yscale('log')
plt.tick_params(axis='y', which='minor')
ax.yaxis.set_minor_formatter(FormatStrFormatter("%.1f"))
plt.show()
一个选项是使用
matplotlib.ticker.LogLocator
导入numpy
进口派拉布
导入matplotlib.pyplot
导入matplotlib.ticker
##设置样式
从matplotlib导入rc
rc('font',**{'family':'sans serif','sans serif':['Times-Roman']})
rc('text',usetex=True)
matplotlib.rcParams['text.latex.preamble']=[r”\usepackage{amsmath}]
##算计
图,ax=matplotlib.pyplot.subplot(1,sharex=True,squence=True)
x=numpy.linspace(0.0,20.0,1000)
y=numpy.exp(x)
轴图(x,y)
ax.set_yscale('log'))
##设置y刻度
y_major=matplotlib.ticker.LogLocator(基数=10.0,numticks=5)
ax.yaxis.set\u major\u定位器(y\u major)
y_minor=matplotlib.ticker.LogLocator(base=10.0,subs=numpy.arange(1.0,10.0)*0.1,numticks=10)
ax.yaxis.set_minor_定位器(y_minor)
ax.yaxis.set\u minor\u格式化程序(matplotlib.ticker.NullFormatter())
##保存数字
pylab.tight_布局()
pylab.savefig('./test.png',dpi=200)
你会得到
您唯一需要手动调整的是主刻度和次刻度的
numticks
输入,它们都必须是主刻度总数的一小部分。您看过函数了吗?doc说:设置记号和记号标签的外观参数。这个问题似乎是重复的,以防有人正在寻找一个解决方案,在一个超过10年的对数轴上显示小记号,下面的解决方案行不通,你可以看一看。我认为OP要求的更多,因为他想在小刻度上贴上标签。就像这个相关的答案一样:@prodev_paris——你说得对:我编辑了我的答案,使之更完整。@xnx有没有办法标记每一个第n个小记号?例如,类似于plt.tick_参数(axis='x',which='minor',every=6)
更好的是,打开网格!这对我有效,只是这里有类似的问题?