How to superimpose figures in matplotlib

后端 未结 1 658
无人共我
无人共我 2020-12-19 19:37

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

Can i return a usable figure/plot that could be reused

相关标签:
1条回答
  • 2020-12-19 20:11

    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.

    0 讨论(0)
提交回复
热议问题