【问题标题】:How to reuse and plot more than once the same plot如何重复使用和多次绘制同一个情节
【发布时间】:2019-10-12 16:12:18
【问题描述】:

使用 matplotlib,在 Jupyter 书籍上,我想用一些图制作一个图形,显示它,添加更多图,然后再次显示(旧图和新图)

相反,它只向我显示新的第二张图片上的新图

这是我的代码:

import numpy as np
%matplotlib inline  
import matplotlib.pyplot as plt

a=np.random.rand(10,)
b=np.random.rand(10,)

fig1 = plt.figure(1)
plt.plot(a,'b')
#plt.draw();
plt.show();

plt.figure(1)
plt.plot(b,'g--')
plt.show();

左边是我拥有的,右边是我想要的:

enter image description here


上面的问题已经简化为最简单的形式,因此我可能没有解释我不想每次都重新创建图形(因为它有大约 15 行可以根据我的需要进行配置)

这是我不想要的代码示例:

import numpy as np
%matplotlib inline  
import matplotlib.pyplot as plt

a=np.random.rand(10,)
b=np.random.rand(10,)
c=np.random.rand(10,)

plt.plot(a, 'b')
plt.grid(True)

dig, ax = plt.subplots(1)
ax.plot(a,'b')
ax.plot(b,'g--')

dig, bx = plt.subplots(1)
bx.plot(a,'b')
bx.plot(b,'g--')
bx.plot(c,'r.')

plt.show()

这是我期望的一种伪代码:

a=np.random.rand(10,)
b=np.random.rand(10,)
c=np.random.rand(10,)

my_plot = plt.figure()
my_plot.grid(True)

my_plot.addplot(a,'b')
my_plot.show()

my_plot.addplot(a,'g--')
my_plot.show()

my_plot.addplot(a,'r.')
my_plot.show()

(我知道,这不是 phyton/matplotlib,但我相信像这样优雅的东西应该是可能的)

【问题讨论】:

    标签: python python-3.x jupyter-notebook


    【解决方案1】:
    import numpy as np
    %matplotlib inline  
    import matplotlib.pyplot as plt
    
    a=np.random.rand(10,)
    b=np.random.rand(10,)
    c=np.random.rand(10,)
    d=np.random.rand(10,)
    
    p = [a,b,c,d]
    colors = ['r','g','b', 'g--']
    for i in range(len(p)):
        fig, ax = plt.subplots(1)
        for j in range(i + 1):
            ax.plot(p[j], colors[j])
    

    【讨论】:

    • 很好,但这是我尽量避免的,每次我需要情节时都必须重新绘制所有情节
    • 我在原始提示中添加了更多示例
    • 抱歉,原图在哪里??
    • 真实的东西,我有15张图片,每张都是在前一张的基础上添加新的一行/几行
    • @widot 检查更新的答案,我希望这就是你要找的。​​span>
    猜你喜欢
    • 2014-10-11
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2022-01-19
    相关资源
    最近更新 更多