首页 > 解决方案 > 仅在某些图表上缺少次要刻度

问题描述

我的代码提取 csv 文件,对数据进行子集化,然后对每个数据框进行交互以生成输出和一些图表。该代码适用于我的所有测试数据,但一个 csv 文件的两个子集数据帧除外。问题是从 0 到 100 的次要刻度没有显示在两个输出图上。我查看了数据,我不明白为什么小刻度不会显示。

我尝试使用此代码设置主轴,

locmaj = matplotlib.ticker.LogLocator(base=10.0, numticks = 6)
ax.yaxis.set_major_locator(locmaj)

但即使我在次要 loglocator 行中将 numticks 设置为相等,这也无济于事。它确实产生了一个有趣的行为,其中 0 和 1 根本不显示在 y 轴上。

fig, ax = plt.subplots()
ax.scatter(df['cunnanes'],df['turbidity'], s = 4)
ax.scatter(exceed['exceedance'], exceed['turbidity'])

ax.set_title(names[i] + ' Exceedance graph')
ax.set_xlabel('Exceedance Probability (%)')
ax.set_ylabel('Turbidity (FNU)')
ax.set_yscale('symlog')
ax.set_ylim([-0.5, 10000])

ax.minorticks_on
ax.tick_params(axis = 'x', which = 'both', direction  = 'in', top = True, bottom = True)
ax.tick_params(axis = 'y', which = 'both', direction  = 'in', left = True, right = True)
locmin = matplotlib.ticker.LogLocator(base=10.0,  subs=(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9))
ax.yaxis.set_minor_locator(locmin)
ax.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

plt.show()

有什么想法可以触发这种行为吗?

不工作图:
不工作图

工作图:
工作图

更新:这是有问题的 csv 文件和重现问题的最小代码。

数据

脚本

更新 2 Jody Klymak 指出 matplotlib.ticker.SymmetricalLogLocator() 而不是 LogLocator()。

改变了

locmin = matplotlib.ticker.LogLocator(base=10, subs=(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9), numticks = 10)

locmin = matplotlib.ticker.SymmetricalLogLocator(linthresh=1, base=10, subs = (0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9))

标签: python-3.xdataframematplotlib

解决方案


推荐阅读