用对数s设置刻度

2024-04-30 04:15:05 发布

您现在位置:Python中文网/ 问答频道 /正文

似乎set_xticks在日志级别中不起作用:

from matplotlib import pyplot as plt
fig1, ax1 = plt.subplots()
ax1.plot([10, 100, 1000], [1,2,3])
ax1.set_xscale('log')
ax1.set_xticks([20, 200, 500])
plt.show()

有可能吗?


Tags: fromimportlogplotmatplotlibasplt级别
2条回答
import matplotlib
from matplotlib import pyplot as plt
fig1, ax1 = plt.subplots()
ax1.plot([10, 100, 1000], [1,2,3])
ax1.set_xscale('log')
ax1.set_xticks([20, 200, 500])
ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())

或者

ax1.get_xaxis().get_major_formatter().labelOnlyBase = False
plt.show()

resulting plot

我将添加一些绘图并演示如何删除次要刻度:

手术室:

from matplotlib import pyplot as plt

fig1, ax1 = plt.subplots()
ax1.plot([10, 100, 1000], [1,2,3])
ax1.set_xscale('log')
ax1.set_xticks([20, 300, 500])
plt.show()

enter image description here

要添加一些特定的记号,如tcaswell所指出的,可以使用^{}

from matplotlib import pyplot as plt
import matplotlib.ticker

fig1, ax1 = plt.subplots()
ax1.plot([10, 100, 1000], [1,2,3])
ax1.set_xscale('log')
ax1.set_xticks([20, 300, 500])
ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
plt.show()

enter image description here

要删除次要记号,可以使用^{}

from matplotlib import pyplot as plt
import matplotlib.ticker

matplotlib.rcParams['xtick.minor.size'] = 0
matplotlib.rcParams['xtick.minor.width'] = 0

fig1, ax1 = plt.subplots()
ax1.plot([10, 100, 1000], [1,2,3])
ax1.set_xscale('log')
ax1.set_xticks([20, 300, 500])
ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())

plt.show()

enter image description here

您可以使用^{},它有相同的效果(但只修改当前轴,而不是所有未来的图形不同于matplotlib.rcParams):

from matplotlib import pyplot as plt
import matplotlib.ticker

fig1, ax1 = plt.subplots()
ax1.plot([10, 100, 1000], [1,2,3])
ax1.set_xscale('log')
ax1.set_xticks([20, 300, 500])
ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())

ax1.get_xaxis().set_tick_params(which='minor', size=0)
ax1.get_xaxis().set_tick_params(which='minor', width=0) 

plt.show()

enter image description here

相关问题 更多 >