在 Matplotlib 中,许多示例以 ax = subplot(111)
的形式出现,然后函数应用于 ax
,例如 ax.xaxis.set_major_formatter(FuncFormatter(myfunc))
。 (发现here)
或者,当我不需要子图时,我可以只做 plt.figure()
然后用 plt.plot()
或类似的方法绘制我需要的任何东西功能。
现在,我正好处于第二种情况,但我想在 X 轴上调用函数 set_major_formatter
。在 plt
上调用当然不行:
>>> plt.xaxis.set_major_formatter(FuncFormatter(myfunc))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'xaxis'
我应该在这里做什么?
最佳答案
如果选择了你想要的图形,只需使用 gca()
获取当前轴实例:
ax = gca()
ax.xaxis.set_major_formatter(FuncFormatter(myfunc))
关于python - plt.figure() 与 Matplotlib 中的子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14441493/