【发布时间】:2016-11-10 07:59:04
【问题描述】:
我正在尝试减少子图中的轴刻度数(每个轴的值不同,因此我无法手动设置刻度),但其他答案(例如 this 或 this)不起作用。 我创建图形的语法是标准的,如下:
fig = plt.figure(figsize=(7,9))
ax = fig.add_subplot(8,2,i+1) # I am plotting in a much larger loop, but I don't think there is anything wrong with the loop, because everything else (axis limits, labels, plotting itself...) works fine.
为了减少 yticks 的数量,我尝试了
ax = plt.locator_params(nbins=4, axis='y')
引发错误TypeError: set_params() got an unexpected keyword argument 'nbins'
我试过了
ax.yaxis.set_major_locator(plt.MaxNLocator(4))
这给出了错误AttributeError: 'NoneType' object has no attribute 'yaxis'
我不明白为什么我的子图被认为是 NoneType。我怀疑这是问题的核心,但我看到的所有示例都具有相同的结构,即
fig = plt.figure()
ax = fig.add_subplot(111)
ax.yaxis.set_major_locator(plt.MaxNLocator(4))
它应该可以工作。那为什么我的斧头是 NoneType 呢?
【问题讨论】:
-
是的,
fig = plt.figure(); ax = fig.add_subplot(111); ax.yaxis.set_major_locator(plt.MaxNLocator(3))应该可以工作,而且在我的情况下也可以。这些是脚本中唯一的行吗?你是如何导入 pyplot 的? -
很奇怪,在这种情况下,我实际绘制的东西肯定有什么不为人知的错误,它弄乱了 ax 对象。以标准方式导入 pyplot,
import matplotlib.pyplot as plt但不,这些不是脚本中唯一的行。为了绘制它们,需要读取和处理大量数据。但即使我将其他所有内容都注释掉,我仍然会收到同样的错误。 -
可能是我在循环中创建这些子图的问题吗?
for i,col in enumerate(column_list): ax = fig.add_subplot(8,2,i+1)
标签: python matplotlib