Python-matplotlib-使用对数刻度时使用科学记数法的自定义刻度标签?

Python-matplotlib-使用对数刻度时使用科学记数法的自定义刻度标签?,python,matplotlib,logarithm,scientific-notation,ticker,Python,Matplotlib,Logarithm,Scientific Notation,Ticker,matplotlib(版本3.1.3)有问题:我想在保留科学符号的同时,在对数刻度轴上添加自定义刻度和刻度标签 换言之:我想在对数刻度轴上添加自定义刻度,并使用老式的'%1.1e'(或任何数字)格式对其进行标记,但是,例如,我希望使用'2.5e-02'而不是'2.5 x 10^-2'(或乳胶中的'2.5\乘以10^{-2}' 因此,我从没有自定义记号的最小工作代码开始: import matplotlib as mpl import matplotlib.pyplot as plt print(

matplotlib(版本3.1.3)有问题:我想在保留科学符号的同时,在对数刻度轴上添加自定义刻度和刻度标签

换言之:我想在对数刻度轴上添加自定义刻度,并使用老式的'%1.1e'(或任何数字)格式对其进行标记,但是,例如,我希望使用'2.5e-02'而不是'2.5 x 10^-2'(或乳胶中的'2.5\乘以10^{-2}'

因此,我从没有自定义记号的最小工作代码开始:

import matplotlib as mpl
import matplotlib.pyplot as plt
print('MATPLOTLIB  VERSION : %s' % mpl.__version__)

plt.style.use("default")

# DATA
x = [0.1, 0.075, 0.05, 0.025, 0.01, 0.0075, 0.005, 0.0025, 0.001, 0.00075, 0.0005, 0.00025, 0.0001, 7.5e-05, 5e-05, 2.5e-05, 1e-05, 1e-06, 1e-07, 1e-08, 1e-09, 1e-10]
y = x

fig = plt.figure()
ax = plt.axes()
plt.loglog()
plt.minorticks_off()
path = ax.plot(x, y)

plt.savefig('test.png')
其中:

很好,但是,正如我所说,我想在xaxis上添加自定义标记。更准确地说,我想对轴进行限制,并在这些限制之间定义等间距的标签。假设我想要4个刻度;它给出了以下代码:

import matplotlib as mpl
import matplotlib.pyplot as plt
print('MATPLOTLIB  VERSION : %s' % mpl.__version__)

plt.style.use("default")

# DATA
x = [0.1, 0.075, 0.05, 0.025, 0.01, 0.0075, 0.005, 0.0025, 0.001, 0.00075, 0.0005, 0.00025, 0.0001, 7.5e-05, 5e-05, 2.5e-05, 1e-05, 1e-06, 1e-07, 1e-08, 1e-09, 1e-10]
y = x

xmin = min(x)
xmax = max(x)
ymin = min(y)
ymax = max(y)

# XTICKS
nbdiv = 4
xTicks = []
k = pow((xmin/xmax),1./(nbdiv-1.))
for i in range(0,nbdiv):
  xTicks.append(xmax*pow(k,i))

# PLOT
fig = plt.figure()
ax = plt.axes()
plt.loglog()
plt.minorticks_off()
plt.axis([xmin,xmax,ymin,ymax])
plt.xticks(xTicks)
path = ax.plot(x, y)

plt.savefig('test_working_4.png')
for i in range(0,nbdiv):
  xTicksStr.append(convert_e_format_to_latex('%1.1e'%xTicks[i]))
它提供了以下绘图:

这就是我想要得到的结果。但是,如果滴答数(nbdiv)发生变化,例如变为等于5,我会得到:

这一次,只标记第一个和最后一个记号。似乎只有等于(或至少接近)十(10^n)整数次幂的数字才会被标记。我试图用matplot.ticker.ScalarFormatter更改默认的勾号格式,但没有对其进行调整以解决此问题。我还尝试了LogFormatterMathText和LogFormatterScinonotation,但效果并不好

这个问题本身对我来说并不难,所以我不明白为什么我会有这么多麻烦。。。我做错了什么?有人有钥匙让我明白我的错误吗

无论如何,我感谢您的阅读,并提前感谢您的回复


注:对于可能出现的英语错误,我深表歉意,因为这不是我的母语。

对于双重发布,我感到抱歉,但我提出了一个解决方案,即使不令人满意,也可能对其他人有用

我发现我的解决方案不令人满意,因为它涉及到使用我的以下函数“手动”转换格式(我不是Python专家,因此我相信它可以简化/优化):

我改变了绘图中的xticks指令,变成:

plt.xticks(xTicks,xTicksStr)
这将提供所需的输出:


这是可行的,但它太复杂了。。。我很确定我错过了一个更简单的功能。你觉得怎么样

已解决,基于上述代码。这个要简单得多。您需要使用Xticklabel

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
from sympy import pretty_print as pp, latex
print('MATPLOTLIB  VERSION : %s' % mpl.__version__)

plt.style.use("default")

# DATA
x = [0.1, 0.075, 0.05, 0.025, 0.01, 0.0075, 0.005, 0.0025, 0.001, 0.00075, 0.0005, 0.00025, 0.0001, 7.5e-05, 5e-05, 2.5e-05, 1e-05, 1e-06, 1e-07, 1e-08, 1e-09, 1e-10]
y = x

xmin = min(x)
xmax = max(x)
ymin = min(y)
ymax = max(y)

# XTICKS
nbdiv = 5
xTicks = []
xticklabels = []
k = pow((xmin/xmax),1./(nbdiv-1.))
for i in range(0,nbdiv):
  xTicks.append(xmax*pow(k,i))
  printstr = '{:.2e}'.format(xmax*pow(k,i))
  ls = printstr.split('e')
  xticklabels.append((ls[0]+' x $10^{'  +ls[1] + '}$'))

# PLOT
fig = plt.figure()
ax = plt.axes()
plt.loglog()
plt.minorticks_off()
plt.axis([xmin,xmax,ymin,ymax])
plt.xticks(xTicks)
path = ax.plot(x, y)

plt.savefig('test_working_4.png')
ax.set_xticklabels(xticklabels)

首先,感谢您的回答。然而,无论你的解决方案是否符合我的期望,无论我是否理解,我都很抱歉。事实上,我想要的是使用“十的指数幂”而不是“%X.Ye”格式保留科学记数法。我编辑了我自己的答案,并放了一张想要的结果的图片。更新到你想要的确切形式。我想你会发现这个解决方案简单得多。
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
from sympy import pretty_print as pp, latex
print('MATPLOTLIB  VERSION : %s' % mpl.__version__)

plt.style.use("default")

# DATA
x = [0.1, 0.075, 0.05, 0.025, 0.01, 0.0075, 0.005, 0.0025, 0.001, 0.00075, 0.0005, 0.00025, 0.0001, 7.5e-05, 5e-05, 2.5e-05, 1e-05, 1e-06, 1e-07, 1e-08, 1e-09, 1e-10]
y = x

xmin = min(x)
xmax = max(x)
ymin = min(y)
ymax = max(y)

# XTICKS
nbdiv = 5
xTicks = []
xticklabels = []
k = pow((xmin/xmax),1./(nbdiv-1.))
for i in range(0,nbdiv):
  xTicks.append(xmax*pow(k,i))
  printstr = '{:.2e}'.format(xmax*pow(k,i))
  ls = printstr.split('e')
  xticklabels.append((ls[0]+' x $10^{'  +ls[1] + '}$'))

# PLOT
fig = plt.figure()
ax = plt.axes()
plt.loglog()
plt.minorticks_off()
plt.axis([xmin,xmax,ymin,ymax])
plt.xticks(xTicks)
path = ax.plot(x, y)

plt.savefig('test_working_4.png')
ax.set_xticklabels(xticklabels)