gpt4 book ai didi

python - matplotlib:如何返回一个 matplotlib 对象然后作为子图绘制?

转载 作者:行者123 更新时间:2023-12-03 18:35:17 25 4
gpt4 key购买 nike

我检查了这个 SO Matplotlib returning a plot object但它并不真正适合我的问题。

我想做的是:

def func1():
fig1 = plt.plot (np.arange(0.0, 5.0, 0.1))
return fig1

def func2()
return plt.plot (np.arange(0.0, 5.0, 0.02))


fig1 = func1()
fig2 = func2()
plt.figure()
plt.add_subplot(fig1)
plt.add_subplot(fig2)
plt.show()

上面的代码只是一个主要思想。你能建议我怎么做吗?

谢谢

最佳答案

这个想法是让你的函数绘制到一个轴上。要么将此坐标轴作为函数的参数提供,要么让它采用当前坐标轴。

import matplotlib.pyplot as plt
import numpy as np

def func1(ax=None):
ax = ax or plt.gca()
line, = ax.plot (np.arange(0.0, 5.0, 0.1))
return line

def func2(ax=None):
ax = ax or plt.gca()
line, = ax.plot (np.arange(0.0, 5.0, 0.02))
return line


fig, (ax1,ax2) = plt.subplots(ncols=2)
func1(ax1)
func2(ax2)

plt.show()

关于python - matplotlib:如何返回一个 matplotlib 对象然后作为子图绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52054491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com