【问题标题】:matplotlib missing minor ticks on y axis because of log range >10 decades [duplicate]由于日志范围> 10个十年,matplotlib在y轴上缺少次要刻度[重复]
【发布时间】:2018-02-04 21:43:16
【问题描述】:

首先,这个问题与Matplotlib semi-log plot: minor tick marks are gone when range is large直接相关。
对于 1E-15 到 1E-4 范围内的值,我试图在 y 轴上获得相同的结果。
我还没有足够的声誉来评论所引用的问题 - 抱歉加倍!

测试设置:我使用 Jupyter 4.3.0 和 matplotlib 2.0.2 和 numpy 1.13.1

以下代码(来自引用的问题)在我的 Jupyter 安装中的 x 轴 上没有显示小刻度。我需要 y 轴,但应该是相同的程序。

感谢任何将我推向正确方向的意见。

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

#Used this to reset any changes to the rc params
#plt.rcParams.update(plt.rcParamsDefault)

x = np.arange(10) # np.arange(9) is working as expected - well documented issue with >10 range for ticker.LocLocator
y = 10.0**x

fig, ax=plt.subplots()
ax.plot(y,x)
ax.set_xscale("log")

locmaj = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,1.0, ))
ax.xaxis.set_major_locator(locmaj)

locmin = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,0.2,0.4,0.6,0.8,1,2,4,6,8,10 )) 
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

plt.show()

结果图 -

【问题讨论】:

  • 好的,我看到了问题。链接问题的解决方案适用于 matplotlib 2.0.0(或更低版本?),但似乎不适用于 matplotlib 2.0.2。原因目前尚不清楚,正在调查中。
  • 我打开了一个功能请求,以使 10-dec 的限制不可推翻github.com/matplotlib/matplotlib/issues/9107
  • 我认为问题已经解决了,因此我将其标记为与原帖重复。
  • 是的,从现在开始重复。
  • 感谢 ImportanceOfBeingErnest 和 tacaswell 提供非常快速的解决方案!

标签: python python-3.x matplotlib jupyter-notebook


【解决方案1】:

需要更改的参数 ind numticks 限制将发出的刻度数。

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

#Used this to reset any changes to the rc params
#plt.rcParams.update(plt.rcParamsDefault)

x = np.arange(10) 
y = 10.0**x

fig, ax=plt.subplots()
ax.plot(y,x)
ax.set_xscale("log")

locmaj = matplotlib.ticker.LogLocator(base=10.0, subs=(1.0, ), numticks=100)
ax.xaxis.set_major_locator(locmaj)

locmin = matplotlib.ticker.LogLocator(base=10.0, subs=np.arange(2, 10) * .1,
                                      numticks=100)
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

plt.show()

一旦定位器决定开始跳过几十年,如果 subs 不是 (1,) 所有刻度都将被抑制以防止一些非常混乱的刻度标签。

也不是刻度会在[s * base ** i for s in subs for i in range(min_decade, max_decade, decade_stride],因此将仅与base 的倍数不同的值放入subs 只需重新标记相同的点。

【讨论】:

猜你喜欢
  • 2014-05-14
  • 2017-03-11
  • 1970-01-01
  • 2012-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 2011-03-29
  • 1970-01-01
相关资源
最近更新 更多