从对数轴上有选择地删除刻度标签(主要和/或次要)

时间:2019-04-15 13:32:07

标签: 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_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_xaxis().set_minor_formatter(matplotlib.ticker.ScalarFormatter())

plt.show()

给出

enter image description here

现在,我希望有选择地从x轴上删除/更改刻度线标签,以便仅保留['0.02', '0.10', '0.20'],同时保留刻度线标记。我该怎么做?

修改

我最初想要遵循的策略似乎遇到了一个错误。我考虑过

fig.canvas.draw()

labels = [item.get_text() for item in ax.get_xticklabels()]

# ...

ax.set_xticklabels(labels)

但是在我的Mac上,它只能打印

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

1 个答案:

答案 0 :(得分:1)

在这里,您希望在对数底数(10)的整数次幂的1和2的倍数上具有刻度。可以使用junit.framework.AssertionFailedError: Should have thrown NoActivityResumedException at junit.framework.Assert.fail(Assert.java:50) at com.myproject.activity.AddTraderActivityTest.backButton_press(AddTraderActivityTest.kt:142) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531) at org.junit.rules.RunRules.evaluate(RunRules.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:389) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879) Tests ran to completion. 完成。然后,您可以使用matplotlib.ticker.LogLocator(subs=(1,2,))来关闭次要标签。

NullLocator()

enter image description here