How to superimpose figures in matplotlib

前提是你 提交于 2019-11-29 11:42:24

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()

where the functions represent modules.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!