【问题标题】:Can not put single artist in more than one figure不能将单个艺术家放在一个以上的数字中
【发布时间】:2017-11-29 14:09:46
【问题描述】:

这是我的问题。

我创建了一个绘制圆圈列表的函数。 我需要先用圆 C2 绘制我的圆 C1,然后用 C3....直到 C40。

import matplotlib.pyplot as plt 
from matplotlib.patches import Circle

def plot_circle(Liste_circles):


    fig = plt.figure(figsize=(5,5))   
    ax = fig.add_subplot(111)

    # On définie un fond blanc
    ax.set_facecolor((1, 1, 1))

    ax.set_xlim(-5, 15)
    ax.set_ylim(-6, 12)    

    for c in Liste_circles:
        ax.add_patch(c)

    plt.show()

现在我创建 C1:

C1=Circle(xy=(3, 4), radius=2, fill=False, color='g')

最后我尝试绘制它。
第一个情节奏效了:

C2=Circle(xy=(6, 3), radius=4, fill=False, color='b')
plot_circle([C1,C2])

第二个失败:

C3=Circle(xy=(7, 2), radius=4, fill=False, color='b')
plot_circle([C1,C3])

出现错误:

RuntimeError: Can not put single artist in multiple figure

我可以这样做:

C1=Circle(xy=(3, 4), radius=2, fill=False, color='g')
C3=Circle(xy=(7, 2), radius=4, fill=False, color='b')
plot_circle([C1,C3])

如何在不必每次都重新创建 C1 的情况下将我的圆圈 C1 与 40 个其他圆圈一起绘制? (我的程序花了 10 分钟通过一个复杂的算法创建 C1,我无法在 40 个情节中的每一个情节中重新创建它......)。

【问题讨论】:

  • 我认为这个错误是不言自明的。它告诉您不能将圆圈 (C1) 放在一个以上的数字中。因此,要么将它们全部放在一个单元格中,要么在运行使用它们的单元格之前运行定义圆圈的单元格。
  • 我无法重现此内容。这一切都按预期工作。见video
  • 用一个新问题来回答你的问题是没有用的。这将 (1) 阅读问题的人不清楚,(2) 看起来好像您已经找到了答案,这样人们会继续阅读而不详细阅读。总之,这将阻止你得到答案。如果您认为答案中的内容比您的问题更清楚,请在您的问题中替换它。
  • 谢谢。我编辑我的第一个问题并将删除我的答案:-)
  • 我可能有一个解决方案,我只需要在我的绘图函数中创建 C1 的副本并绘制副本而不是 C1 自己。但我不知道如何制作 C1 的副本.....请参阅我的另一个问题:“如何复制 python 对象/如何获取 python 对象的元素”

标签: matplotlib plot python-3.6


【解决方案1】:

下面是如何让它工作:只需复制一个圆圈并绘制副本:

第一个导入副本:

from copy import copy

然后,而不是这样做:

for c in Liste_circles:
    ax.add_patch(c)

我们必须做的:

for c in Liste_circles:
   new_c=copy(c)
   ax.add_patch(new_c)

这样我们不会绘制相同的圆圈(= 相同的艺术家),而是绘制它的副本

【讨论】:

  • copy() 适用于矩形。可能有些“艺术家”需要 deepcopy() 代替?
【解决方案2】:

此外,如果您有兴趣,可以根据圆圈的值设置 x 和 y 轴:

import matplotlib.pyplot as plt 
from matplotlib.patches import Circle
from copy import copy


def plot_circles (Liste_circles):

    if len(Liste_circles) == 0:
        print('No circles to display')
    else:

        fig = plt.figure(figsize=(5,5))   
        ax = fig.add_subplot(111)


        # On définie un fond blanc
        ax.set_facecolor((1, 1, 1))


        # On définie les limites des axes pour que nos cercles soient au centre de notre graphique
        # --> xmin sera le plus petit des x de tous les cercles - 2 fois le plus grand des rayons
        # --> xmax sera le plus grand des x de tous les cercles + 2 fois le plus grand des rayons
        # --> ymin sera le plus petit des y de tous les cercles - 2 fois le plus grand des rayons
        # --> ymax sera le plus grand des y de tous les cercles + 2 fois le plus grand des rayons
        L_x = [c.center[0] for c in Liste_circles]
        L_y = [c.center[1] for c in Liste_circles]
        L_r = [c.radius for c in Liste_circles]

        min_x=min(L_x); max_x=max(L_x);min_y=min(L_y); max_y=max(L_y);max_r=max(L_r)

        xmin = min_x - 2*max_r
        xmax = max_x + 2*max_r

        ymin = min_y - 2*max_r
        ymax = max_y + 2*max_r

        ax.set_xlim(xmin, xmax)
        ax.set_ylim(ymin, ymax)


        for c in Liste_circles:
            new_c=copy(c)
            ax.add_patch(new_c) 

        plt.show()

C1=Circle(xy=(3, 4), radius=2, fill=False, color='g')
C2=Circle(xy=(6, 3), radius=4, fill=False, color='b')
C3=Circle(xy=(7, 2), radius=4, fill=False, color='r')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多