Python MatPlotLib中LogLocator的参数

Python MatPlotLib中LogLocator的参数,python,matplotlib,Python,Matplotlib,在MatPlotLib中,我想绘制一个具有线性x轴和对数y轴的图形。对于x轴,标签的倍数应为4,次刻度的倍数应为1。我已经能够使用MultipleLocator类来实现这一点 然而,我很难对对数y轴做类似的事情。我希望在0.1、0.2、0.3等处有标签,在0.11、0.12、0.13等处有小刻度。我已经用LogLocator类尝试过这样做,但我不确定正确的参数是什么 以下是我迄今为止所做的努力: x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] y = [0

在MatPlotLib中,我想绘制一个具有线性x轴和对数y轴的图形。对于x轴,标签的倍数应为4,次刻度的倍数应为1。我已经能够使用
MultipleLocator
类来实现这一点

然而,我很难对对数y轴做类似的事情。我希望在0.1、0.2、0.3等处有标签,在0.11、0.12、0.13等处有小刻度。我已经用
LogLocator
类尝试过这样做,但我不确定正确的参数是什么

以下是我迄今为止所做的努力:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = [0.32, 0.30, 0.28, 0.26, 0.24, 0.22, 0.20, 0.18, 0.16, 0.14, 0.12, 0.10]
fig = plt.figure()
ax1 = fig.add_subplot(111)
x_major = MultipleLocator(4)
x_minor = MultipleLocator(1)
ax1.xaxis.set_major_locator(x_major)
ax1.xaxis.set_minor_locator(x_minor)
ax1.set_yscale("log")
y_major = LogLocator(base=10)
y_minor = LogLocator(base=10)
ax1.yaxis.set_major_locator(y_major)
ax1.yaxis.set_minor_locator(y_minor)
ax1.plot(x, y)
plt.show()
这显示了以下曲线图:

x轴是我想要的,但不是y轴。y轴上0.1处有标签,但0.2和0.3处没有标签。此外,在0.11、0.12、0.13等处没有滴答声

我为
LogLocator
构造函数尝试了一些不同的值,例如
subs
numdecs
numticks
,但我无法获得正确的绘图。上的文档并没有很好地解释这些参数


我应该使用什么参数值?

我认为您仍然需要
MultipleLocator
而不是
LogLocator
,因为您所需的勾号位置仍然是“在视图间隔内的每一个基数倍数的整数上”,而不是“subs[j]*base**I”。例如:

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = [0.32, 0.30, 0.28, 0.26, 0.24, 0.22, 0.20, 0.18, 0.16, 0.14, 0.12, 0.10]
fig = plt.figure(figsize=(8, 12))
ax1 = fig.add_subplot(111)
x_major = MultipleLocator(4)
x_minor = MultipleLocator(1)
ax1.xaxis.set_major_locator(x_major)
ax1.xaxis.set_minor_locator(x_minor)
ax1.set_yscale("log")
# You would need to erase default major ticklabels
ax1.set_yticklabels(['']*len(ax1.get_yticklabels()))
y_major = MultipleLocator(0.1)
y_minor = MultipleLocator(0.01)
ax1.yaxis.set_major_locator(y_major)
ax1.yaxis.set_minor_locator(y_minor)
ax1.plot(x, y)
plt.show()

LogLocator
始终在“每个基准**i”处放置主刻度标签。因此,不可能将其用于所需的主刻度标签。您可以将参数
subs
用于小刻度标签,如下所示:

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, LogLocator

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = [0.32, 0.30, 0.28, 0.26, 0.24, 0.22, 0.20, 0.18, 0.16, 0.14, 0.12, 0.10]
fig = plt.figure()
ax1 = fig.add_subplot(111)
x_major = MultipleLocator(4)
x_minor = MultipleLocator(1)
ax1.xaxis.set_major_locator(x_major)
ax1.xaxis.set_minor_locator(x_minor)
ax1.set_yscale("log")
y_major = LogLocator(base=10)
y_minor = LogLocator(base=10, subs=[1.1, 1.2, 1.3])
ax1.yaxis.set_major_locator(y_major)
ax1.yaxis.set_minor_locator(y_minor)
ax1.plot(x, y)
plt.show()