1

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

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the tip! But is it possible to plot the resultant plot for each outer loop iteration in the same window? The given code just gives a single (final) plot.
sure, just move the plot creating into the first loop (I'll edit)
It did the trick! Thanks! However, the windows all opened without a delay and even closed before I could look at them.
Hmm, I never look at interactive figures (I just save them to PNGs). What happens if you feed num=iteration to plt.subplots?
It worked the same way it did before I added num=iteration to plt.subplots.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.