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

玩笑

我尝试只在一个窗口中显示 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 个窗口。我不知道如何编码。

你能帮忙吗?

r-初学者

设置一个包含三个参数的列表作为函数的参数并使用 ,因此它多次绘制列表的长度(三)。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章