【问题标题】:How can one copy a matplotlib ax object如何复制 matplotlib ax 对象
【发布时间】:2020-09-18 10:18:36
【问题描述】:

而不是以下:

fig, ax = plt.subplots()
x = [1, 2]
y = [4, 5]
ax.scatter(x, y, s=100)

看起来像

我想要:


def plot(ax, x, y):
    ax_c = copy.deepcopy(ax)
    ax_c.scatter(x, y, s=100)
    return ax_c



fig, ax = plt.subplots()
x = [1, 2]
y = [4, 5]
ax = plot(ax, x, y)

但是,这不起作用,出现以下错误:

NotImplementedError: TransformNode instances can not be copied. Consider using frozen() instead.

所以,我想知道如何编写一个函数,该函数将 matplotlib ax 作为输入,创建它的副本,更改副本并返回它。我想这样做的原因是我可以拥有独立的功能。

编辑

以下不是合适的解决方案:

def plot(ax_c, x, y):
    ax_c.scatter(x, y, s=100)
    return ax_c

因为这里的 return 语句可以替换为 None 并且它仍然可以正常工作。

【问题讨论】:

  • 试试解决方案here

标签: python matplotlib plot data-visualization


【解决方案1】:

您可以在函数中简单地使用以下方式

def plot(ax_c, x, y):
    ax_c.scatter(x, y, s=100)
    return ax_c

【讨论】:

  • 我明白 - 但这里 return 是不必要的,你可以让这个函数返回 None,它会做同样的事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
相关资源
最近更新 更多