如何使用matplotlib显示多个图形?

问题描述 投票:-1回答:1

我尝试仅在一个窗口中显示3个数字。

因此,我有一个函数make_graph(x),可将2个子图绘制成图形:

%matplotlib
import matplotlib.pyplot as plt
import numpy as np

def make_graph(x):  
    fig = plt.figure()
    ax = fig.add_subplot(2,1,1)
    ax.scatter(x, [x ** 2], s = 50, color = 'blue')
    ax = fig.add_subplot(2,1,2)
    ax.scatter(x, [x ** 3], s = 50, color = 'red')


make_graph(np.arange(10)) #figure_1
make_graph(np.arange(20)) #figure_2
make_graph(np.arange(30)) #figure_3

我想在一个窗口中显示这3个数字,但是实际上,我有3个窗口正在打开。我不知道该如何编码。

您能帮忙吗?

python matplotlib figure
1个回答
0
投票
import matplotlib.pyplot as plt import numpy as np def make_graph(x): fig = plt.figure() pos = [[1,2,3],[4,5,6]] for i,val in enumerate(x): x = np.arange(val) ax = fig.add_subplot(2,3,pos[0][i]) ax.scatter(x, [x ** 2], s = 50, color = 'blue') ax = fig.add_subplot(2,3,pos[1][i]) ax.scatter(x, [x ** 3], s = 50, color = 'red') make_graph([10,20,30])
© www.soinside.com 2019 - 2024. All rights reserved.