plt.figure()vs Matplotlib中的子图

Ric*_*son 7 python plot graph matplotlib

Matplotlib中,很多示例都出现在表单中ax = subplot(111),然后应用了函数ax,例如ax.xaxis.set_major_formatter(FuncFormatter(myfunc)).(在这里找到)

或者,当我不需要子图时,我可以做plt.figure(),然后绘制我需要的任何东西plt.plot()或类似的功能.

现在,我正好在第二种情况下,但我想set_major_formatter在X轴上调用该函数.调用它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'
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

tia*_*ago 7

如果选择了您想要的数字,只需使用gca()获取当前轴实例:

ax = gca()
ax.xaxis.set_major_formatter(FuncFormatter(myfunc)) 
Run Code Online (Sandbox Code Playgroud)

  • 如果您收到错误 `NameError: name 'gca' is not defined` 尝试使用 `ax = plt.gca()` 而不是 `ax = gca()` (2认同)