代替以下内容:
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
替换,并且仍然可以正常工作。
您可以在函数中简单地使用以下方式
def plot(ax_c, x, y):
ax_c.scatter(x, y, s=100)
return ax_c