Rochak Gupta
Rochak Gupta

Reputation: 150

How to continue plotting in the same window in python?

I am having a problem figuring out how to keep plotting in the same plot window (I want my plots after a single plot to be done in the same window, i.e. I don't want to close the window to get to other plot).Could I use the arrows at the bottom of the plot window to switch to next plot? Here is my code :

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)

Here, data is an mXn+1 matrix and the nth column has values ranging from 1 to K, centroids is a kXn matrix and idx is an mX1 matrix

Upvotes: 1

Views: 7449

Answers (1)

Paul H
Paul H

Reputation: 68186

Never use pyplot to draw anything. The only thing it's really good for is creating figures, axes, and some artists.

Without running your example, I do:

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

Upvotes: 1

Related Questions