使用Matplotlib,Python使用多种功能绘制到1个图形 [英] Plotting to 1 figure using multiple functions with Matplotlib, Python

查看:34
本文介绍了使用Matplotlib,Python使用多种功能绘制到1个图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主程序 main.py,我在其中调用了各种函数,其想法是每个函数都将一些东西绘制成 1 个图形.即所有功能图都将细节附加到1个主图上.

I have a main program main.py in which I call various functions with the idea that each function plots something to 1 figure. i.e. all the function plots append detail to the 1 main plot.

目前我已将其设置为例如:

Currently I have it set up as, for example:

main.py:

import matplotlib.pylab as plt    

a,b,c = 1,2,3

fig = func1(a,b,c)

d,e,f = 4,5,6

fig = func2(d,e,f)

plt.show()

func1:

def func1(a,b,c):
    import matplotlib.pylab as plt
    ## Do stuff with a,b and c ##
    fig = plt.figure()    
    plt.plot()
    return fig

func2:

def func2(d,e,f):
    import matplotlib.pylab as plt
    ## Do stuff with d,e and f ##
    fig = plt.figure()    
    plt.plot()
    return fig

这种方法已经完成了一半,但是它为每个功能绘制了单独的图形,而不是覆盖它们.

This approach is halfway there but it plots separate figures for each function instead of overlaying them.

如何获得 1 个图形,其中所有图的结果相互叠加?

How can I obtain 1 figure with the results of all plots overlaid on top of each other?

推荐答案

这应该有效.请注意,我只创建了一个图形并使用 pyplot 接口对其进行绘图,而从未明确获得对图形对象的引用.

This should work. Note that I only create one figure and use the pyplot interface to plot to it without ever explicitly obtaining a reference to the figure object.

import matplotlib.pyplot as plt    

a = [1,2,3]
b = [3,2,1]

def func1(x):
    plt.plot(x)

def func2(x):
    plt.plot(x)

fig = plt.figure()
func1(a)
func2(b)

这篇关于使用Matplotlib,Python使用多种功能绘制到1个图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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