如何使用 Matplotlib 在对数刻度上显示次要刻度标签 [英] How to show minor tick labels on log-scale with Matplotlib
本文介绍了如何使用 Matplotlib 在对数刻度上显示次要刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道如何使用Python/Matplotlib以对数刻度显示次要刻度的标签吗?
Does anyone know how to show the labels of the minor ticks on a logarithmic scale with Python/Matplotlib?
推荐答案
您可以使用 plt.tick_params(axis='y', which='minor')
来设置小刻度和使用 matplotlib.ticker
FormatStrFormatter
对其进行格式化.例如,
You can use plt.tick_params(axis='y', which='minor')
to set the minor ticks on and format them with the matplotlib.ticker
FormatStrFormatter
. For example,
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 在对数刻度上显示次要刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文