在一个图形上绘制两组圆数据

时间:2019-10-23 16:29:32

标签: python plot graph figure circle-pack

我想生成一个代码,将两个图相互叠加,但是我却得到了两个单独的图: 我的代码如下:

fig, ax = plt.subplots()
for i in range(len(list_of_disks)):
    circle1 = plt.Circle((list_of_disks[i].x, list_of_disks[i].y), radius)
    plt.xlim(0,1)
    plt.ylim(0,1)
    plt.grid(linestyle='--')
    ax.add_artist(circle1)
plt.show()

if len(percolated_cluster) != 0:
    fig, ax = plt.subplots()
    for i in range(len(percolated_cluster[0])):
        circle1 = plt.Circle((percolated_cluster[0][i].x, percolated_cluster[0][i].y), radius, color = 'red')
        plt.xlim(0,1)
        plt.ylim(0,1)
        plt.grid(linestyle='--')
        ax.add_artist(circle1)
    plt.show()

但是当我运行该程序时,我收到的是两个图形而不是一个图形,上面绘制了两组数据。 我如何确保将两组数据都绘制在一张图上。 作为参考,我在两个单独的屏幕中获得了以下内容:

enter image description here

但是woudl喜欢将两组数据绘制在一起。

1 个答案:

答案 0 :(得分:0)

您必须在子图图中指定列数:

fig, ax = plt.subplots()
for i in range(len(list_of_disks)):
    circle1 = plt.Circle((list_of_disks[i].x, list_of_disks[i].y), radius)
    ax.xlim(0,1)
    ax.ylim(0,1)
    ax.grid(linestyle='--')
    ax.add_artist(circle1)

if len(percolated_cluster) != 0:
    for i in range(len(percolated_cluster[0])):
        circle1 = plt.Circle((percolated_cluster[0][i].x, percolated_cluster[0][i].y), radius, color = 'red')
        ax.grid(linestyle='--')
        ax.add_artist(circle1)
plt.show()

有用的参考资料:https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.subplots.html