Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib如何读取轴参数_Python_Python 3.x_Matplotlib - Fatal编程技术网

Python Matplotlib如何读取轴参数

Python Matplotlib如何读取轴参数,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我来自java世界,目前正试图学习python以学习matplotlib。作为一名java程序员,下面的代码片段看起来非常奇怪 x = np.linspace(0, 2*np.pi, 400) y = np.sin(x**2) f, (ax1, ax2) = plt.subplots(1, 2, sharex='all', sharey='all') ax1.plot(x, y) ax1.set_title('Sharing Y axis') ax2.scatter(x, y) plt.sh

我来自java世界,目前正试图学习python以学习matplotlib。作为一名java程序员,下面的代码片段看起来非常奇怪

x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)

f, (ax1, ax2) = plt.subplots(1, 2, sharex='all', sharey='all')
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)

plt.show()
我想知道plt.show如何读取轴上设置的配置,因为我们没有向它传递任何参数。有人能解释一下这种行为是如何实现的吗

编辑

既然@ImportanceOfBeingErnest问我,我很可能会澄清我的意图。我的问题不是pyplot如何创建图像,我的问题更多的是pyplot如何看到轴配置。约翰·兹温克(John Zwinck)在回答我的问题时尝试了正确的方向,我自己得出的结论是,这一定与全球国家有关。但只要看一下子地块的内部实现,我就会感到困惑

让我们看一下pyplot的内部实现:

def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
             subplot_kw=None, gridspec_kw=None, **fig_kw):
    fig = figure(**fig_kw)
    axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
                       squeeze=squeeze, subplot_kw=subplot_kw,
                       gridspec_kw=gridspec_kw)
    return fig, axs

def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
                 squeeze=True, subplot_kw=None, gridspec_kw=None):
            if isinstance(sharex, bool):
            sharex = "all" if sharex else "none"
        if isinstance(sharey, bool):
            sharey = "all" if sharey else "none"
        share_values = ["all", "row", "col", "none"]
        if sharex not in share_values:
            if isinstance(sharex, int):
                warnings.warn(
                    "sharex argument to subplots() was an integer. "
                    "Did you intend to use subplot() (without 's')?")

            raise ValueError("sharex [%s] must be one of %s" %
                             (sharex, share_values))
        if sharey not in share_values:
            raise ValueError("sharey [%s] must be one of %s" %
                             (sharey, share_values))
        if subplot_kw is None:
            subplot_kw = {}
        if gridspec_kw is None:
            gridspec_kw = {}

        if self.get_constrained_layout():
            gs = GridSpec(nrows, ncols, figure=self, **gridspec_kw)
        else:
            # this should turn constrained_layout off if we don't want it
            gs = GridSpec(nrows, ncols, figure=None, **gridspec_kw)

        # Create array to hold all axes.
        axarr = np.empty((nrows, ncols), dtype=object)
        for row in range(nrows):
            for col in range(ncols):
                shared_with = {"none": None, "all": axarr[0, 0],
                               "row": axarr[row, 0], "col": axarr[0, col]}
                subplot_kw["sharex"] = shared_with[sharex]
                subplot_kw["sharey"] = shared_with[sharey]
                axarr[row, col] = self.add_subplot(gs[row, col], **subplot_kw)

        # turn off redundant tick labeling
        if sharex in ["col", "all"]:
            # turn off all but the bottom row
            for ax in axarr[:-1, :].flat:
                ax.xaxis.set_tick_params(which='both',
                                         labelbottom=False, labeltop=False)
                ax.xaxis.offsetText.set_visible(False)
        if sharey in ["row", "all"]:
            # turn off all but the first column
            for ax in axarr[:, 1:].flat:
                ax.yaxis.set_tick_params(which='both',
                                         labelleft=False, labelright=False)
                ax.yaxis.offsetText.set_visible(False)

        if squeeze:
            # Discarding unneeded dimensions that equal 1.  If we only have one
            # subplot, just return it instead of a 1-element array.
            return axarr.item() if axarr.size == 1 else axarr.squeeze()
        else:
            # Returned axis array will be always 2-d, even if nrows=ncols=1.
            return axarr
就我所知,在这条直线中,轴被创建为一个numpy数组

axarr = np.empty((nrows, ncols), dtype=object)
但这是一个局部变量,并不表示主程序上已更改的轴的状态如何对pyplot可见。这是我真正的问题。

Matplotlib的pyplot是作为plt导入的,它是一个奇怪的、有状态的东西,旨在模拟Matlab。它实际上是一个全局变量或一组全局变量的包装器,例如plt.gca,用于获取当前轴

很多人发现它更易于使用,但这与您期望的Java带来的结果完全相反。如果您愿意,完全可以在不使用pyplot/plt的情况下使用Matplotlib,在这种情况下,对于那些来自面向对象编程背景的人来说,代码可能看起来更为传统。

首先,fig=figure**fig_kw调用pyplot.figure函数。这将在pyplot状态机中注册图形

fig是matplotlib.figure.figure实例。接下来,调用子图方法。这将基本上创建一个数组,并将其填充到matplotlib.axes.axes实例中。这些是使用self.add_子批创建的

add_子图将初始化轴并将其存储为地物的fig.axes数组的一部分

因此,总的来说,你有pyplot,它存储图形,每个图形都存储其中的轴。当调用plt.show时,它将基本上遍历所有图形并显示它们。对于每个图形,将绘制fig.轴内的所有轴。 如果您之前通过调用任何轴的方法来操纵任何轴,那么这些方法当然会被考虑在内,因为您操纵的是稍后也绘制的轴对象。

这与。也许你可以用你想知道的更具体的细节来更新这个问题。