Wak*_*nka 2 python plot matplotlib
根据这篇文章,所有内容都按matplotlib层次结构组织.层次结构的顶部matplotlib是matplotlib.pyplot模块提供的"状态机环境" .在此级别,简单函数用于将绘图元素(线条,图像,文本等)添加到当前图形中的当前轴.层次结构中的下一级是面向对象的接口的第一级,其中pyplot仅用于诸如图形创建之类的一些功能,并且用户明确地创建并跟踪图形和轴对象.在此级别,用户使用pyplot创建图形,通过这些图形,可以创建一个或多个轴对象.然后,这些轴对象用于大多数绘图操作.还有其他术语,如图,轴,轴,艺术家(有很好的图片解释了所有这些,在提到的页面上).综上所述:
matplotlib.pyplot模块创建新图的最简单方法是使用pyplot:
fig = plt.figure() # an empty figure with no axes
fig, ax_lst = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes
Run Code Online (Sandbox Code Playgroud)
我经常看到这两种方法可以互换使用,我希望它们基本上是等价的.但是我无法fig, ax = plt.subplots()使用fig = plt.figure()和得到的结果相同ax = fig.add_subplot(111, projection='3d')
以下是我的实验,用于plt.figure():
In [1]: from mpl_toolkits.mplot3d import Axes3D
In [2]: import matplotlib.pyplot as plt
In [3]: fig = plt.figure()
In [4]: ax = fig.add_subplot(111, projection='3d')
In [5]: fig
Out[5]: <matplotlib.figure.Figure at 0x7f8377ca0610>
In [6]: ax
Out[6]: <matplotlib.axes._subplots.Axes3DSubplot at 0x7f8377bfb310>
In [7]:
Run Code Online (Sandbox Code Playgroud)
以下是我的实验,用于plt.subplots():
In [1]: from mpl_toolkits.mplot3d import Axes3D
In [2]: import matplotlib.pyplot as plt
In [3]: fig, ax = plt.subplots()
In [4]: fig
Out[4]: <matplotlib.figure.Figure at 0x7f3dcf96e710>
In [5]: ax
Out[5]: <matplotlib.axes._subplots.AxesSubplot at 0x7f3dced564d0>
In [6]:
Run Code Online (Sandbox Code Playgroud)
如您所见,第一个创建matplotlib.axes._subplots.Axes3DSubplot对象,而第二个创建matplotlib.axes._subplots.AxesSubplot对象.我一直在寻找通过help(plt.subplots)为projection关键字,但我没有发现任何东西.所以我试图使用相同的参数plt.subplots,fig.add_subplot但我收到以下错误:
In [7]: fig, ax = plt.subplots(111, projection='3d')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-a905adad48f5> in <module>()
----> 1 fig, ax = plt.subplots(111, projection='3d')
/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in subplots(nrows, ncols, sharex, sharey, squeeze, subplot_kw, gridspec_kw, **fig_kw)
1076 gridspec_kw = {}
1077
-> 1078 fig = figure(**fig_kw)
1079 gs = GridSpec(nrows, ncols, **gridspec_kw)
1080
/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
433 frameon=frameon,
434 FigureClass=FigureClass,
--> 435 **kwargs)
436
437 if figLabel:
/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)
78 """
79 FigureClass = kwargs.pop('FigureClass', Figure)
---> 80 figure = FigureClass(*args, **kwargs)
81 return new_figure_manager_given_figure(num, figure)
82
TypeError: __init__() got an unexpected keyword argument 'projection'
Run Code Online (Sandbox Code Playgroud)
题:
是fig, ax = plt.subplots()和fig = plt.figure(); ax = fig.add_subplot(111, projection='3d')等价物,如果是fig, ax = plt.subplots(),我如何在我的例子中使用?
在提到的页面上还有以下代码:
#!/bin/env python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 100)
# The first call to plt.plot will automatically create the necessary figure and axes to achieve the desired plot.
plt.plot(x, x, label='linear')
# Subsequent calls to plt.plot re-use the current axes and each add another line.
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')
# Setting the title, legend, and axis labels also automatically use the current axes and set the title, create the legend, and label the axis respectively.
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,没有这样的功能fig = plt.figure()也没有fig, ax_lst = plt.subplots(2, 2)
题:
如何在此示例中维护层次结构,默认情况下是创建的图形还是正在进行的操作?
我想你已经证明了这些命令并不是完全等同的,只是想要一些保证.
要做你想做的事情 - 你可以通过设置一个subplot参数字典并传入它们来传入'在封面下'使用projection的add_subplot()调用.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
subplot_args = {'projection':'3d'}
fig, ax = plt.subplots(subplot_kw=subplot_args)
Run Code Online (Sandbox Code Playgroud)
在这里的文档中subplot_kw描述了命名参数的使用.
数字轴等都是由第一行开始"在封面下"创建的plt.plot.该pyplot模块正在维护状态并重新使用相同的图形实例和轴实例,直到您调用plt.show().
注意 - 就目前而言,您没有这些实例的句柄.如果你想要,你可以随时获得一个句柄,例如通过电话
fig = plt.gcf()
Run Code Online (Sandbox Code Playgroud)
和
ax = plt.gca()
Run Code Online (Sandbox Code Playgroud)
其中gcf()和gca()得到当前数字并分别得到当前轴.这是基于matplotlib最初所基于的matlab功能建模的.
如果你真的很想知道自动创建是如何完成的 - 它都是开源的.你可以看到呼叫plot()通过调用创建一个轴实例gca()在这里的代码.这反过来调用gcf(),它寻找一个FigureManager(实际上是维持状态).如果存在,则返回它正在管理的数字,否则使用创建新数字plt.figure().同样,这个过程在某种程度上继承自matlab,其中初始调用通常figure在任何绘图操作之前.
我认为你可能正在推动的是如何使用matplotlib.pyplot像plt.plot()etc 这样的 函数来访问文档中描述的层次结构.答案是,如果你想要非常细粒度的控制,它有时不会.这就是人们使用的原因
fig, ax = plt.subplots()
Run Code Online (Sandbox Code Playgroud)
图案或类似的,以便他们直接拥有图形和轴对象的句柄,并可以随意操纵它们.
| 归档时间: |
|
| 查看次数: |
1669 次 |
| 最近记录: |