Development Tip

matplotlib에서 플롯, 축 또는 그림을 사용하여 플롯을 그리는 것의 차이점은 무엇입니까?

yourdevel 2020. 11. 15. 11:51
반응형

matplotlib에서 플롯, 축 또는 그림을 사용하여 플롯을 그리는 것의 차이점은 무엇입니까?


나는 matplotlib, tbh에서 플롯을 그릴 때 백엔드에서 무슨 일이 벌어지고 있는지 혼란 스럽습니다. 플롯, 축 및 그림의 계층 구조가 명확하지 않습니다. 문서를 읽고 도움이되었지만 여전히 혼란 스럽습니다 ...

아래 코드는 세 가지 다른 방법으로 동일한 플롯을 그립니다.

#creating the arrays for testing
x = np.arange(1, 100)
y = np.sqrt(x)
#1st way
plt.plot(x, y)
#2nd way
ax = plt.subplot()
ax.plot(x, y)
#3rd way
figure = plt.figure()
new_plot = figure.add_subplot(111)
new_plot.plot(x, y)

이제 내 질문은-

  1. 세 가지 모두의 차이점은 무엇입니까? 세 가지 메서드 중 하나가 호출 될 때 내부적으로 무슨 일이 벌어지는 지 의미합니까?

  2. 어떤 방법을 사용해야하며 어떤 방법을 사용하는 장단점은 무엇입니까?


방법 1

plt.plot(x, y)

이렇게하면 (x, y) 좌표로 하나의 그림 만 그릴 수 있습니다. 하나의 그래픽 만 얻으려면이 방법을 사용할 수 있습니다.

방법 2

ax = plt.subplot()
ax.plot(x, y)

이렇게하면 동일한 창에 하나 또는 여러 그림을 그릴 수 있습니다. 당신이 그것을 쓸 때, 당신은 단지 하나의 그림을 그릴 것이지만, 다음과 같이 만들 수 있습니다 :

fig1, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

ax1, ax2, ax3 및 ax4라는 이름의 그림 4 개를 각각 같은 창에 플로팅합니다. 이 창은 예를 들어 4 부분으로 나뉩니다.

방법 3

fig = plt.figure()
new_plot = fig.add_subplot(111)
new_plot.plot(x, y)

나는 그것을 사용하지 않았지만 문서를 찾을 수 있습니다.

예:

import numpy as np
import matplotlib.pyplot as plt

# Method 1 #

x = np.random.rand(10)
y = np.random.rand(10)

figure1 = plt.plot(x,y)

# Method 2 #

x1 = np.random.rand(10)
x2 = np.random.rand(10)
x3 = np.random.rand(10)
x4 = np.random.rand(10)
y1 = np.random.rand(10)
y2 = np.random.rand(10)
y3 = np.random.rand(10)
y4 = np.random.rand(10)

figure2, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
ax1.plot(x1,y1)
ax2.plot(x2,y2)
ax3.plot(x3,y3)
ax4.plot(x4,y4)

plt.show()

여기에 이미지 설명 입력 여기에 이미지 설명 입력

다른 예 :

여기에 이미지 설명 입력


개체의 이름

Matplotlib는 강력한 객체 지향적이며 주요 객체는 그림축입니다 (이름 axes이 약간 오해의 소지가 있지만 아마도 나일 것입니다).

You can think of the figure as a canvas, of which you typically specify the dimensions and possibly e.g., the background color etc etc. You use the canvas, the figure, essentially in two ways, placing other objects on it (mostly axes, but also text labels etc) and saving its contents with savefig.

You can think of an axes as a sort of Swiss Army knife, a handy object that offers a tool (e.g. .plot, .scatter, .hist etc) for everything, mostly. You can place one, two, ... many axes inside a figure using one of many different methods.

The plt interface

The plt procedural interface was originally developed to mimic the MATLAB™ interface but is not really different from the object oriented interface, even if you don't make a direct reference to the main objects (i.e., a figure and an axes) these objects are automatically instantiated and each plt method is, essentially, translated to a call of one of the methods of the underlying fundamental objects: e.g., a plt.plot() is a hidden_axes.plot and a plt.savefig is a hidden_figure.savefig.

In every moment you can have an handle on these hidden objects using plt.gcf and plt.gca, and this is sometimes necessary when one of the object methods has not been ported to a method in the plt namespace.

I'd like to add that the plt namespace contains also a number of convenience methods to instantiate, in different ways, figure and axes.

Your examples

1st way

plt.plot(x, y)

Here you use only the plt interface, you can only use a single axes in each figure, but this is what you want when you are doing an exploration of your data, a quick recipe that gets the work done...

2nd way

ax = plt.subplot()
ax.plot(x, y)

Here you use a convenience method in the plt namespace to give a name (and a handle) to your axes object, but btw there is also an hidden figure. You can later use the axes object to plot, to make an histogram etc, all things that you can do with the plt interface, but you can also access all its attributes and modify them with greater freedom.

3rd way

figure = plt.figure()
new_plot = figure.add_subplot(111)
new_plot.plot(x, y)

Here you start instantiating a figure using a convenience method in the plt namespace and later you use only the object oriented interface.

It is possible to bypass the plt convenience method (matplotlib.figure.Figure) but you then have to tweak the figure for a better interactive experience (after all, it's a convenience method).

Personal recommendations

I suggest bare plt.plot, plt.scatter in the context of an interactive session, possibly using IPython with its %matplotlib magic command, and also in the context of an exploratory Jupyter notebook.

On the other hand the object oriented approach, plus a few plt convenience methods, is the way to go

  • if you have a permanent issue to solve once for all with a customized arrangement of finely tuned subplots,
  • if you want to embed Matplotlib in the UI of a program you write.

이 극단 사이에는 큰 회색 영역이 있으며 어떻게해야하는지 물어 보면 "상황에 따라 다름" 이라고 말하면됩니다 .

참고 URL : https://stackoverflow.com/questions/37970424/what-is-the-difference-between-drawing-plots-using-plot-axes-or-figure-in-matpl

반응형