简体   繁体   English

matplotlib plot 创建并显示后,有什么方法可以更改数据吗?

[英]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.

相关问题 更改已经创建的matplotlib图形上的字体和线条粗细 - Change fonts and line thicknesses on a matplotlib figure that has already been created 如何在创建matplotlib图例后对其进行修改? - How to modify matplotlib legend after it has been created? 使用matplotlib从JSON绘制数据的最简单方法? - Easiest way to plot data from JSON with matplotlib? matplotlib中的plot表情有什么办法吗? - Is there any way to plot emojis in matplotlib? nltkdispersion_plot()函数不起作用。 线型“ |”是否已从matplotlib中删除? - nltk dispersion_plot() function not working. Has the line-style “|” been removed from matplotlib? 有什么办法可以将 plot 面嵌入 matplotlib Python - Is there any way to plot face embedding on matplotlib Python 有什么办法可以在matplotlib中为整个图块定界? - Is there any way to border entire plot in matplotlib? 如何让 Spyder 等到 matplotlib plot 构建完成 - How to make Spyder wait until matplotlib plot has been constructed 有没有办法从终端关闭使用 matplotlib 创建的 plot(无需用鼠标关闭 window) - Is there a way to close a plot created with matplotlib from terminal (without having to close the window with mouse) 从TCP数据使用Matplotlib进行绘图 - plot with Matplotlib from tcp data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM