【问题标题】:How to continue plotting in the same window in python?如何继续在python的同一窗口中绘图?
【发布时间】:2015-10-16 17:22:21
【问题描述】:

我在弄清楚如何在同一个绘图窗口中继续绘图时遇到问题(我希望在同一个窗口中完成单个绘图后的绘图,即我不想关闭窗口以获取其他情节)。我可以使用情节窗口底部的箭头切换到下一个情节吗? 这是我的代码:

for iteration in range(0, max_iters):
    idx = findClosestCentroids(X, centroids)
    centroids = computeCentroids(X, idx, K)

    if plot is True:
        data = c_[X, idx]
        for i in range(1, K + 1):
            data_1 = data[data[:, n] == i]
            pyplot.plot(data_1[:, 0], data_1[:, 1], linestyle=' ', color=dict[i - 1], marker='o', markersize=3)

        pyplot.plot(centroids[:, 0], centroids[:, 1], 'k*', markersize=15)
        pyplot.show(block=True)
        pyplot.hold(True)

这里, data 是一个 mXn+1 矩阵,第 n 列的值从 1 到 K, centroids 是一个 kXn 矩阵,idx 是一个 mX1 矩阵

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    永远不要使用 pyplot 来绘制任何东西。它唯一真正有用的是创建人物、轴和一些艺术家。

    不运行您的示例,我会这样做:

    for iteration in range(0, max_iters):
        fig, ax = plt.subplots()
        idx = findClosestCentroids(X, centroids)
        centroids = computeCentroids(X, idx, K)
    
        if plot is True:
            data = c_[X, idx]
            for i in range(1, K + 1):
                data_1 = data[data[:, n] == i]
                ax.plot(data_1[:, 0], data_1[:, 1], linestyle=' ', color=dict[i - 1], marker='o', markersize=3)
    
            ax.plot(centroids[:, 0], centroids[:, 1], 'k*', markersize=15)
    
        fig.show()
    

    【讨论】:

    • 感谢您的提示!但是是否可以在同一个窗口中为每个外循环迭代绘制结果图?给定的代码只给出了一个(最终的)图。
    • 当然,只需将创建的情节移动到第一个循环中(我会编辑)
    • 成功了!谢谢!然而,窗户都打开了,甚至在我看到它们之前就关闭了。
    • 嗯,我从不看交互式图形(我只是将它们保存为 PNG)。如果您将 num=iteration 提供给 plt.subplots 会发生什么?
    • 它的工作方式与我将 num=iteration 添加到 plt.subplots 之前的方式相同。
    猜你喜欢
    • 2019-01-16
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    相关资源
    最近更新 更多