python - plt.figure() 与 Matplotlib 中的子图

标签 python graph matplotlib plot

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/

相关文章:

python - 将不同数据帧中的列添加到 PySpark 中的目标数据帧

editor - GraphViz可以用于图形编辑GUI吗?

optimization - neo4j广度优先遍历内存问题

python - matplotlib fill_ Between 显示点

python - 从 pypcapfile 中的 TCP header 获取 ByteArray

python - 类语法和 type() 有什么区别?

生成自定义类集合的 Pythonic 多重继承

graph - Highcharts -指定堆积时间序列的顺序

python - 时间序列图显示人口的百分位数和其他统计数据

python - 重绘后 Matplotlib 3D 散点颜色丢失