1

I'm new to matplotlib and I'm learning how to work with it. With my code I'm trying to plot multiple scatterplots, but there are always unwanted lines that I can't get rid off.

See image attached to this post.

Here is my Python code:

import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import cluster

iris = datasets.load_iris()

for i in range(1,7):
    kmeans = cluster.KMeans(n_clusters=i).fit(iris.data)
    Centers = kmeans.cluster_centers_
    plot_nr = 230+i
    plt.subplot(plot_nr)
    text = str(i) + ' Cluster'
    plt.scatter(iris.data[:,0], iris.data[:,1], c=kmeans.labels_)
    plt.scatter(Centers[:,0], Centers[:,1], c='r', s=150, marker='*')
    plt.title(text)

plt.savefig('Iris.png')
plt.show()

I'm thankful for every suggestion!

Thanks, Thomas

Plot with unwanted lines

Plot with unwanted lines

3
  • 3
    Since you also seem to be new to Stackoverflow, please read How to Ask and minimal reproducible example. The code you show is not runnable, so one cannot reproduce this funky behaviour and hence cannot suggest a solution. Creating a minimal reproducible example may be a little bit of work, finding out which minimal set of data would produce this, but it's worth doing in case you want an answer here and also for yourself to be able to debug things. Commented Mar 28, 2018 at 18:54
  • 1
    i don't know if i can edit his post to make the code runnable, but it's pretty easy to make it so Commented Mar 28, 2018 at 19:11
  • @Thomas_ to satisfy ImportantOfBeingErnest's request, add import matplotlib.pyplot as plt; from sklearn import datasets; from sklearn import cluster; iris = datasets.load_iris() to the top of your code and it should be runnable for all. thanks. Commented Mar 28, 2018 at 19:18

1 Answer 1

2

edit first line plt.subplots(231, figsize=(18,12)) and replace 231 by 2, 3, 231 creates a cell between the two rows. probably repeated 6 times

(and it should be way faster)

Sign up to request clarification or add additional context in comments.

2 Comments

good catch. it's not clear what you mean "edit" and "type 231", but using your recommendation I commented out (or deleted) that line, and noticed it does resolve the issue, thus upvoted +1
Thank you bobrobbob, you helped me a lot! Kind regards, Thomas

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.