如何使用matplotlib从外部功能生成的添加_subplot图

发布于 2025-01-28 07:46:51 字数 1441 浏览 1 评论 0原文

当前,我有三个函数来创建图形,即get_image_1get_image_2get_image_3,如下所示。

def get_image_1():
    fig, ax = plt.subplots(figsize=(8, 6))
    plt.plot(range(10))
    fig.savefig('image_1.png')
    # return fig

def get_image_2():
    fig, ax = plt.subplots(figsize=(8, 6))
    plt.plot([0,5,8,9,3,6])
    fig.savefig('image_2.png')
    # return fig

def get_image_3():
    fig, ax = plt.subplots(figsize=(8, 6))
    plt.plot([100,5,8,9,3,6])
    fig.savefig('image_3.png')
    # return fig

为简单起见,我们只使用Simple plt.plot()

上述函数通过

get_image_1()
get_image_2()
get_image_3()

我想显示这三个图,如本图所示。

当前,我必须在本地保存每个图像。例如,在函数get_image_1中,请注意行fig.savefig('image_1.png')

并重新上传保存的图像,然后根据下面的代码将它们组合在一起,

import matplotlib.pyplot as plt
import cv2 as cv
fig = plt.figure(figsize=(9, 10))
for idx,dpath in enumerate(['image_1.png','image_2.png','image_3.png']):
    tt=1
    if idx!=2:
        sub1 = fig.add_subplot(2, 2, idx + 1)
    else:
        sub1 = fig.add_subplot(2, 2, (3,4))

    image2 = cv.imread(dpath)
    sub1.imshow(image2, 'gray')

plt.show()

我想知道如何更有效地进行此活动。这样,要完全跳过保存的并重新启动图像。

Currently, I have THREE function to create the graphic, namely get_image_1, get_image_2, and get_image_3 as shown in the function below.

def get_image_1():
    fig, ax = plt.subplots(figsize=(8, 6))
    plt.plot(range(10))
    fig.savefig('image_1.png')
    # return fig

def get_image_2():
    fig, ax = plt.subplots(figsize=(8, 6))
    plt.plot([0,5,8,9,3,6])
    fig.savefig('image_2.png')
    # return fig

def get_image_3():
    fig, ax = plt.subplots(figsize=(8, 6))
    plt.plot([100,5,8,9,3,6])
    fig.savefig('image_3.png')
    # return fig

For simplicity, we just use simple plt.plot().

The above function are called via

get_image_1()
get_image_2()
get_image_3()

I would like to display these THREE plot as shown in this figure.

enter image description here

Currently, I had to save each of the image locally. For example in the function get_image_1, notice the line fig.savefig('image_1.png').

and re-upload the saved images and subsequently combine them as per the code below

import matplotlib.pyplot as plt
import cv2 as cv
fig = plt.figure(figsize=(9, 10))
for idx,dpath in enumerate(['image_1.png','image_2.png','image_3.png']):
    tt=1
    if idx!=2:
        sub1 = fig.add_subplot(2, 2, idx + 1)
    else:
        sub1 = fig.add_subplot(2, 2, (3,4))

    image2 = cv.imread(dpath)
    sub1.imshow(image2, 'gray')

plt.show()

I wonder how to do this activity more efficiently. Such that, to skip entirely the saved and re-upload the image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2025-02-04 07:46:51

根据@bigben的建议。

修改函数以接受axes参数

  def get_image_1(ax):
    ax.plot(range(10))


def get_image_2(ax):
    ax.plot([0,5,8,9,3,6])

def get_image_3(ax):
    ax.plot([100,5,8,9,3,6])

,然后传递axes每个function>函数调用参数

fig = plt.figure(figsize=(9, 10))

ax1 = fig.add_subplot(2, 2, 1)
get_image_1(ax1)

get_image_2(fig.add_subplot(2, 2, 2))

get_image_3(fig.add_subplot(2, 2, (3,4)))

plt.show()

As per @BigBen suggestion in the comment.

Modify the function to accept axes parameter

  def get_image_1(ax):
    ax.plot(range(10))


def get_image_2(ax):
    ax.plot([0,5,8,9,3,6])

def get_image_3(ax):
    ax.plot([100,5,8,9,3,6])

Then, pass the axes parameter for each function calling

fig = plt.figure(figsize=(9, 10))

ax1 = fig.add_subplot(2, 2, 1)
get_image_1(ax1)

get_image_2(fig.add_subplot(2, 2, 2))

get_image_3(fig.add_subplot(2, 2, (3,4)))

plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文