fig.gca() 与 fig.add_subplot() [英] fig.gca() vs. fig.add_subplot()
问题描述
大多数面向对象的matplotlib的示例都会获得带有类似东西的Axis对象
Most examples of object-oriented matplotlib get an Axis object with something like
import matplotlib.pyplot as plt
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(...... etc.
我一直发现这并不明显,尤其是从 matlab 的角度来看.我最近发现可以通过
Which I've always found to be non-obvious, especially from a matlab-perspective. I recently found that equivalent results can be obtained via
ax1 = fig1.gca() # "GetCurrentAxis"
这对我来说更有意义(可能只是因为之前使用过 Matlab).为什么 add_subplot() 带有令人困惑的 111 参数被选为获取轴对象的首选方式?有什么功能上的区别吗?
Which makes way more sense to me (possibly only due to prior Matlab use). Why is add_subplot() with a confusing 111 argument chosen as the preferred way to get an axis object? Is there any functional difference?
谢谢!
推荐答案
plt.gca
获取当前轴,并在需要时创建一个轴.它仅在最简单的 1 轴情况下等效.
plt.gca
gets the current axes, creating one if needed. It is only equivalent in the simplest 1 axes case.
首选方法是使用 plt.subplots
(而且文档/示例确实有点滞后,如果你想开始贡献,更新文档是一个很好的开始):
The preferred way is to use plt.subplots
(and the docs/examples are indeed lagging a bit, if you want to start contributing, updating the docs is a great place to start):
fig, ax = plt.subplots(1, 1)
或
fig, (ax1, ax2) = plt.subplots(2, 1)
以此类推.
这篇关于fig.gca() 与 fig.add_subplot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!