如何使用matplotlib从外部功能生成的添加_subplot图
当前,我有三个函数来创建图形,即get_image_1
,get_image_2
和get_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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据@bigben的建议。
修改函数以接受
axes
参数,然后传递
axes
每个function>函数
调用参数As per @BigBen suggestion in the comment.
Modify the function to accept
axes
parameterThen, pass the
axes
parameter for eachfunction
calling