如何复制一个matplotlib ax对象

问题描述 投票:0回答:1

代替以下内容:

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

哪个看起来像enter image description here

我想拥有:


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替换,并且仍然可以正常工作。

python matplotlib plot data-visualization
1个回答
0
投票

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

def plot(ax_c, x, y):
    ax_c.scatter(x, y, s=100)
    return ax_c
© www.soinside.com 2019 - 2024. All rights reserved.