如何在matplotlib中叠加数字 [英] How to superimpose figures in matplotlib

查看:149
本文介绍了如何在matplotlib中叠加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个模块中创建了多个图形,我想将它们叠加在我的main.py中.

I create several figures in multiple modules, and i would like to superimpose them in mymain.py.

我可以返回可以在main.py中重用以创建组合图的可用图形/图吗?

Can i return a usable figure/plot that could be reused in main.py to create a combined figure?

谢谢!

推荐答案

解决方案是在main.py中设置图形/轴,然后将轴手柄传递给每个模块.作为一个最小的例子,

The solution is to setup a figure/axis in main.py and then pass the axis handle to each module. As a minimal example,

import matplotlib.pyplot as plt
import numpy as np
#from mod import plotsomefunction
#from diffrentmod import plotsomeotherfunction

def plotsomefunction(ax, x):

    return ax.plot(x, np.sin(x))

def plotsomeotherfunction(ax, x):

    return ax.plot(x,np.cos(x))


fig, ax = plt.subplots(1,1)
x = np.linspace(0,np.pi,1000)
l1 = plotsomefunction(ax, x)
l2 = plotsomeotherfunction(ax, x)
plt.show()

其中的功能代表模块.

或者,您可以只在main中创建一个图形,然后使用plt.sca将其添加到每个模块的当前轴中.这似乎是一个不太健壮的解决方案.

Alternatively, you could just create a figure in main and add it to the current axis in each module with plt.sca. This seems like a much less robust solution.

这篇关于如何在matplotlib中叠加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆