首页 > 解决方案 > matplotlib 中 twinx() 期间的 RuntimeError

问题描述

执行 plt.twinx() 时出现以下错误

RuntimeError: Locator attempting to generate 1993 ticks from 0.075257498916 to 199.95917462: exceeds Locator.MAXTICKS

对应的代码是:

x1 = np.ones(100)*np.median(a)
y1 = np.linspace(1,500,100)
position_x = (np.around(np.linspace(0.0, np.max(a),6),2)).astype(int)

def tick_function(x):
    """Specify tick format"""
    return ["%2.f" % i for i in x]

def func(x):
    """This can be anything you like"""
    funcx=np.log10(1.0+x)
    return funcx


fig=plt.figure(figsize=(7,6),dpi=100)
plt.hist(log_z, bins=40, histtype='step')
y_hist = plt.twiny()
y_hist.set_yticks(func(position_x))
y_hist.set_yticklabels(tick_function(position_x))
y_hist.set_ylabel('z')
plt.show()

我无法弄清楚为什么会这样。对此错误的来源和修复它的任何建议表示赞赏。PS 我使用的是 Python 2.7 和 matplotlib 版本 2.2.4。

标签: pythonpython-2.7matplotlib

解决方案


推荐阅读