【问题标题】:Plotting two sets of circle data on one figure在一个图形上绘制两组圆形数据
【发布时间】:2019-10-23 16:29:32
【问题描述】:

我想生成一个代码,将两个图放在另一个之上,但是我得到了两个单独的图: 我的代码如下:

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

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

但希望将两组数据绘制在一起。

【问题讨论】:

    标签: python plot graph figure circle-pack


    【解决方案1】:

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

    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

    【讨论】:

    • 嗨,这仍然会产生两个图像,我希望有一个带有红色和蓝色圆盘的图,其中红色圆盘显示接触两侧的集群,蓝色表示不接触的集群。
    猜你喜欢
    • 1970-01-01
    • 2020-09-15
    • 2014-01-12
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 2014-10-23
    • 2017-05-28
    • 1970-01-01
    相关资源
    最近更新 更多