带有对数轴的 matplotlib 图形,但没有科学/指数符号 [英] matplotlib figure with logarithmic axis but ticks without scientific/exponential notation
问题描述
我正在制作一个图形,其中 x 轴应以对数间隔排列,但我想手动设置刻度标签,并且我希望刻度标签以普通的%.2f"表示法出现,而不是指数表示法.以下解决方案基于
ScalarFormatter
的使用肯定是可能的.然后,您需要确保未显示此问题中所示的次要刻度标签:
I am making a figure where the x-axis should be logarithmically spaced, but I want to manually set the tick labels, and I want the tick labels to appear in ordinary '%.2f' notation, not exponential notation. The following solution based on Matplotlib - logarithmic scale, but require non-logarithmic labels suggests to use ScalarFormatter, but does not work with matplotlib 2.0:
x = np.logspace(2, 3, 100)
y = x
fig, ax = plt.subplots(1, 1)
xscale = ax.set_xscale('log')
ax.set_xticks((100, 200, 300, 500))
xlim = ax.set_xlim(100, 1000)
from matplotlib.ticker import ScalarFormatter
ax.get_xaxis().set_major_formatter(ScalarFormatter())
__=ax.plot(x, y)
The use of a ScalarFormatter
is sure possible. You would then need to make sure that no minor ticklabels are shown as seen in this question: Matplotlib: setting x-limits also forces tick labels?
In your case the code would then look like:
import matplotlib.pyplot as plt
import numpy as np
x = np.logspace(2, 3, 100)
y = x
fig, ax = plt.subplots(1, 1)
xscale = ax.set_xscale('log')
ax.set_xticks((100, 200, 300, 500))
xlim = ax.set_xlim(100, 1000)
import matplotlib.ticker
ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_xaxis().set_minor_formatter(matplotlib.ticker.NullFormatter())
__=ax.plot(x, y)
plt.show()
这篇关于带有对数轴的 matplotlib 图形,但没有科学/指数符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!