[英]Is there any way to change data from a matplotlib plot after it has been created and showed?
So I have a function that prints me some plot once called, and returns some other data.所以我有一个 function,它在调用后打印一些 plot,并返回一些其他数据。 The code for the plot is this one
plot 的代码是这个
def somefunction(input):
x = np.linspace(-5,5,100)
fig, axs = plt.subplots(2,sharex=True)
fig.suptitle("Some plots")
axs[0].plot(x, x**2, "-b", label="square")
axs[1].plot(x, x**3, "-y", label="cube")
axs[0].set(ylabel="values")
axs[1].set(xlabel="Timestamp (common)", ylabel="values")
axs[0].legend()
axs[1].legend()
plt.show()
return [1,2,3]
Now, what I want to do is to print this plot later again but with additional information.现在,我想做的是稍后再次打印此 plot,但包含其他信息。 I thought about saving the figure created here as the output of the function. I tried to do this by adding this to the code:
我考虑过将此处创建的图形保存为 function 的 output。我尝试通过将其添加到代码中来实现:
def somefunction(input):
x = np.linspace(-5,5,100)
fig, axs = plt.subplots(2,sharex=True)
fig.suptitle("Some plots")
axs[0].plot(x, x**2, "-b", label="square")
axs[1].plot(x, x**3, "-y", label="cube")
axs[0].set(ylabel="values")
axs[1].set(xlabel="Timestamp (common)", ylabel="values")
axs[0].legend()
axs[1].legend()
plt.show()
fig_out = fig
return [1,2,3], fig_out
and then later I can just obtain the figure in the second component of the output of the function and change it as I want.然后我可以在 function 的 output 的第二个组件中获取数字并根据需要更改它。 Like:
像:
figure = somefunction(input)[1]
#now perform any wanted changes in the plot and plot again
ax0 = figure.axes[0]
ax0.text(3, 8, 'New text updated in the figure', style='italic',
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
plt.show()
This doesn't work.这是行不通的。 The figure is indeed, saved in the second component of the output, but it doesn't let me change anything about it.
该图确实保存在 output 的第二个组件中,但它不允许我更改任何内容。 It's just there, and I can't change it, nor plot any changes made to the figure.
它就在那里,我无法更改它,plot 也无法对图形进行任何更改。
I also tried saving the axes instead of the figure, but same story.我也尝试保存轴而不是图形,但同样的故事。 I can't seem to find a way to edit this plot after it was created.
创建后,我似乎找不到编辑此 plot 的方法。 Is it even possible?
有可能吗?
Your code is fine, if you don't try to show both plots at once.如果您不尝试同时显示两个图,您的代码就可以了。 There are multiple options to solve it, for instance:
有多种选择可以解决它,例如:
Option 1: show the plot at the end选项1:最后显示plot
from matplotlib import pyplot as plt
def somefunction(input):
x = np.linspace(-5,5,100)
fig, axs = plt.subplots(2,sharex=True)
fig.suptitle("Some plots")
axs[0].plot(x, x**2, "-b", label="square")
axs[1].plot(x, x**3, "-y", label="cube")
axs[0].set(ylabel="values")
axs[1].set(xlabel="Timestamp (common)", ylabel="values")
axs[0].legend()
axs[1].legend()
#plt.show() #<- DO NOT USE IT NOW
return [1,2,3], fig
my_fig = somefunction(input)[1]
ax0 = my_fig.axes[0]
ax0.text(3, 8, 'New text updated in the figure', style='italic', bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
plt.show()
Option 2: use block=False
to indicate to wait until all figures are returned选项 2:使用
block=False
表示等待直到返回所有数字
def somefunction(input):
x = np.linspace(-5,5,100)
fig, axs = plt.subplots(2,sharex=True)
fig.suptitle("Some plots")
axs[0].plot(x, x**2, "-b", label="square")
axs[1].plot(x, x**3, "-y", label="cube")
axs[0].set(ylabel="values")
axs[1].set(xlabel="Timestamp (common)", ylabel="values")
axs[0].legend()
axs[1].legend()
plt.show(block=False) #<- USE BLOCK=FALSE
return [1,2,3], fig
my_fig = somefunction(input)[1]
ax0 = my_fig.axes[0]
ax0.text(3, 8, 'New text updated in the figure', style='italic', bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.