Program Club

matplotlib : AxesSubplot 객체를 생성 한 다음 Figure 인스턴스에 추가 할 수 있습니까?

proclub 2020. 12. 5. 10:49
반응형

matplotlib : AxesSubplot 객체를 생성 한 다음 Figure 인스턴스에 추가 할 수 있습니까?


상기 찾고 matplotlib문서, 그것은 추가 할 수있는 표준 방법 것 같다 AxesSubplotA와이 Figure사용하는 것입니다 Figure.add_subplot:

from matplotlib import pyplot

fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )

AxesSubPlot그림과는 별개로 같은 물체 를 만들 수 있기 때문에 다른 그림에서 사용할 수 있기를 바랍니다. 같은 것

fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)

이것이 가능 matplotlib합니까? 그렇다면 어떻게 할 수 있습니까?

업데이트 : Axes 및 Figure 생성을 분리하지 못했지만 아래 답변의 예를 따르면 이전에 만든 축을 새 또는 olf Figure 인스턴스에서 쉽게 재사용 할 수 있습니다. 이것은 간단한 함수로 설명 할 수 있습니다.

def plot_axes(ax, fig=None, geometry=(1,1,1)):
    if fig is None:
        fig = plt.figure()
    if ax.get_geometry() != geometry :
        ax.change_geometry(*geometry)
    ax = fig.axes.append(ax)
    return fig

일반적으로 axes 인스턴스를 함수에 전달하기 만하면됩니다.

예를 들면 :

import matplotlib.pyplot as plt
import numpy as np

def main():
    x = np.linspace(0, 6 * np.pi, 100)

    fig1, (ax1, ax2) = plt.subplots(nrows=2)
    plot(x, np.sin(x), ax1)
    plot(x, np.random.random(100), ax2)

    fig2 = plt.figure()
    plot(x, np.cos(x))

    plt.show()

def plot(x, y, ax=None):
    if ax is None:
        ax = plt.gca()
    line, = ax.plot(x, y, 'go')
    ax.set_ylabel('Yabba dabba do!')
    return line

if __name__ == '__main__':
    main()

질문에 응답하려면 다음과 같이 할 수 있습니다.

def subplot(data, fig=None, index=111):
    if fig is None:
        fig = plt.figure()
    ax = fig.add_subplot(index)
    ax.plot(data)

Also, you can simply add an axes instance to another figure:

import matplotlib.pyplot as plt

fig1, ax = plt.subplots()
ax.plot(range(10))

fig2 = plt.figure()
fig2.axes.append(ax)

plt.show()

Resizing it to match other subplot "shapes" is also possible, but it's going to quickly become more trouble than it's worth. The approach of just passing around a figure or axes instance (or list of instances) is much simpler for complex cases, in my experience...


The following shows how to "move" an axes from one figure to another. This is the intended functionality of @JoeKington's last example, which in newer matplotlib versions is not working anymore, because axes cannot live in several figures at once.

You would first need to remove the axes from the first figure, then append it to the next figure and give it some position to live in.

import matplotlib.pyplot as plt

fig1, ax = plt.subplots()
ax.plot(range(10))
ax.remove()

fig2 = plt.figure()
ax.figure=fig2
fig2.axes.append(ax)
fig2.add_axes(ax)

dummy = fig2.add_subplot(111)
ax.set_position(dummy.get_position())
dummy.remove()
plt.close(fig1)

plt.show()

For line plots, you can deal with the Line2D objects themselves:

fig1 = pylab.figure()
ax1 = fig1.add_subplot(111)
lines = ax1.plot(scipy.randn(10))

fig2 = pylab.figure()
ax2 = fig2.add_subplot(111)
ax2.add_line(lines[0])

참고URL : https://stackoverflow.com/questions/6309472/matplotlib-can-i-create-axessubplot-objects-then-add-them-to-a-figure-instance

반응형