【问题标题】:Is there any way to change data from a matplotlib plot after it has been created and showed?在创建和显示 matplotlib 图后,有什么方法可以更改数据吗?
【发布时间】:2022-12-31 06:37:35
【问题描述】:

所以我有一个函数,它在调用后打印一些图,并返回一些其他数据。情节的代码是这个

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]

现在,我想做的是稍后再次打印此图,但包含其他信息。我考虑过将此处创建的图形保存为函数的输出。我试图通过将其添加到代码中来做到这一点:

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

然后我可以在函数输出的第二个组件中获取数字并根据需要更改它。像:

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()

这是行不通的。该图确实保存在输出的第二部分中,但我无法对其进行任何更改。它就在那里,我无法更改它,也无法绘制对图形所做的任何更改。

我也尝试保存轴而不是图形,但同样的故事。创建此图后,我似乎找不到编辑它的方法。有可能吗?

【问题讨论】:

  • 您的代码示例不完整(未按原样运行)。请提供可重现的代码,以便人们快速了解您的问题并立即处理。
  • 可以更新一些数据,但正如其他人指出的那样,您应该提供最少的代码来运行。
  • 首先,您的somefunction 返回output 这是什么?您需要具有相同的对象、图形和轴才能应用更改。
  • @KotaMori 抱歉,我没有放置可重现的代码。现在代码是可重现的。无论如何,可复制的部分并不重要。函数的数字输出/输入并没有真正参与到问题中。
  • matplotlib.pyplot.draw() 可能就是您要查找的内容 (matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.draw.html)。 This is used to update a figure that has been altered, but not automatically re-drawn.

标签: python function matplotlib


【解决方案1】:

如果您不尝试同时显示两个图,您的代码就可以了。有多种选择可以解决它,例如:

选项1:在最后显示情节

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()

选项 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多