How to plot 2 subplots from different functions in the same window(figure)?

你。 提交于 2019-12-11 13:01:30

问题


for specific reasons I have two functions, each of them creates a plot in two different windows. Is it possible to unify this two plots in one window, without unifying the functions? thanks!

edit: I have 2 involved functions and a database: function 1 in file1.py plots a 2d-line plot:

plt.figure("TEST12") 
ax=plt.subplot(111)
ax.plot(array[:,10])

In file2.py theres my other function, which plots a filled contour:

plt.figure("TEST13")
ax = plt.subplot(111)
ax.contourf(x,y,data)
plt.gca().set_aspect('equal')

If I use plt.showas usual, the result are 2 different windows.


回答1:


Re-factor your function to take an Axes object to draw to as an argument:

def fun1(ax):
    ax.plot(range(5))

def fun2(ax):
    ax.plot(range(5)[::-1])


fig, ax = plt.subplots(1, 1)

fun1(ax)
fun2(ax)

plt.draw()


来源:https://stackoverflow.com/questions/22606665/how-to-plot-2-subplots-from-different-functions-in-the-same-windowfigure

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