Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有选择地从对数轴上删除标签(长轴和/或短轴)_Python_Python 3.x_Matplotlib - Fatal编程技术网

Python 有选择地从对数轴上删除标签(长轴和/或短轴)

Python 有选择地从对数轴上删除标签(长轴和/或短轴),python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,考虑以下示例: import matplotlib import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.set_xscale('log') ax.set_yscale('log') ax.set_xlim([2e-2, 2e-1]) ax.set_ylim([2e+1, 2e+3]) ax.plot([0.02, 0.1, 0.2], [20, 1000, 2000]) ax.get_xaxis().set_major

考虑以下示例:

import matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.set_xscale('log')
ax.set_yscale('log')

ax.set_xlim([2e-2, 2e-1])
ax.set_ylim([2e+1, 2e+3])

ax.plot([0.02, 0.1, 0.2], [20, 1000, 2000])

ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_xaxis().set_minor_formatter(matplotlib.ticker.ScalarFormatter())

plt.show()

现在,我希望有选择地删除/更改x轴上的记号标签,在保留记号标记的同时,仅保留
['0.02'、'0.10'、'0.20']
。我该怎么做

编辑

我最初想要遵循的策略似乎有一个bug。我考虑过这个

fig.canvas.draw()
labels=[ax.get\uxticklabels()中项目的item.get\uText()
# ...
ax.set_xticklabel(标签)
但在我的mac电脑上它只打印

>>> print(labels)
['', '', '', '', '']

在这里,您需要对数底(10)的整数幂的1和2的倍数。这可以通过
matplotlib.ticker.LogLocator(subs=(1,2,)
完成。然后可以使用
NullLocator()
关闭次要标签

import matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.set_xscale('log')
ax.set_yscale('log')

ax.set_xlim([2e-2, 2e-1])
ax.set_ylim([2e+1, 2e+3])

ax.plot([0.02, 0.1, 0.2], [20, 1000, 2000])

ax.xaxis.set_major_locator(matplotlib.ticker.LogLocator(subs=(1,2,)))
ax.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

plt.show()

或者,您可以将x标签旋转90度@非常感谢你的建议。我同意这是一个很好的解决办法。但我仍然热衷于我的目标。另外,我也尝试过这么做,但我必须承认,我对如何控制主/次滴答标签感到非常困惑。如果你想坚持你的方法并在某个特定位置有滴答声,你需要将次滴答声打开为
ax.get_xticklabels(minor=True)
,在这之前,放
fig.canvas.draw()
在您的代码中。阅读可能吧help@Sheldore谢谢但是,即使使用
fig.canvas.draw()
ax.get_xticklabels(minor=True)
ax.get_xaxis().get_majorticklabels()
ax.get_xaxis().get_minorticklabels(),我也会得到空字符串的列表。谢谢!这很好。出于好奇,例如,
print([item.get_xaxis().get_xaxis().get_minorticklabels()])的输出是什么
?标签是在绘制时确定的,而不是在绘制之前(因为在绘制轴之前标签是未知的)。您可以根据需要手动绘制它,将
fig.canvas.draw()
放在打印语句之前。我在这里谈论的是您的原始代码。在这个答案的代码中,您当然仍然会得到
[“”,'','','',]
,因为次要标签通过
NullFormatter设置为空字符串另外,在那里我只得到空字符串的列表;打印([item.get_text(),用于ax.get_xaxis().get_minorticklabels()]中的项目)
,输出为
[0.00、0.00、0.00、0.01、0.01、0.01、0.01、0.01、0.01、0.02、0.03、0.04、0.05、0.06、0.07、0.08、0.09、0.20、0.30、0.40、0.50、0.60、0.70、0.90、2.00、3.00、4.00、5.00、6.00、7.00、8.00、9.00、20.00、30.00、40.00、50.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00、0.00