【问题标题】:How can I plot graphs in Spyder using Matplotlib?如何使用 Matplotlib 在 Spyder 中绘制图形?
【发布时间】:2016-09-28 01:55:09
【问题描述】:

Python 3,Spyder 2。

当我运行以下代码时,我希望在输入 float 'a' + Enter 时显示绘图。如果我然后输入一个新的“a”,我希望图表用新的“a”更新。但是 Spyder 直到我点击 Enter 才显示图表,这打破了循环.. 我尝试过 Inline 和 Automatic,同样的问题..

import matplotlib.pyplot as plt
L1 = [10.1, 11.2, 12.3, 13.4, 14.5, 13.4, 12.3, 11.1, 10.0]
done = False
while not done:
    a = input("Please enter alpha (between 0 and 1), Enter to exit:")
    if a == "":
        done = True
    else:
        a = float(a)
        L2 = [x * a for x in L1]
        plt.plot(L1)
        plt.plot(L2)

【问题讨论】:

    标签: matplotlib while-loop spyder


    【解决方案1】:

    很难说为什么这个数字不会显示;尝试添加plt.show()?

    这个例子在我的系统上运行流畅。请注意,如果您确实想要更新图表(而不是每次输入新的a 时都添加新行,您需要更改其中一行的ydata,例如:

    import matplotlib.pyplot as plt
    import numpy as np
    
    L1 = np.array([10.1, 11.2, 12.3, 13.4, 14.5, 13.4, 12.3, 11.1, 10.0])
    p1 = plt.plot(L1, color='k')
    p2 = plt.plot(L1, color='r', dashes=[4,2])[0]
    plt.show()
    
    done = False
    while not done:
        a = input("Please enter alpha (between 0 and 1), Enter to exit:")
        if a == "":
            done = True
        else:
            L2 = L1.copy() * float(a)
            p2.set_ydata(L2)
    
            # Zoom to new data extend
            ax = plt.gca()
            ax.relim()
            ax.autoscale_view()
    
            # Redraw
            plt.draw()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2023-02-01
      • 2018-05-18
      • 2022-08-06
      • 2019-05-03
      • 2016-08-17
      相关资源
      最近更新 更多