0

I have a dataframe that is divided into 27 clusters. in order to graph all these clusters in only a plot, I use the following for loop:

list_of_clusters= list(set(klabels))
    
fig=plt.figure(figsize=(13,9),frameon=True,facecolor='lightgrey', edgecolor='black')
    ax = fig.add_subplot(1,1,1)
    plt.axis()
    plt.xlim([-2.5, 0.2])
    plt.ylim([-0.7, 3.3])
    plt.xlabel("log PhiZ")
    plt.ylabel("log RQI")
    
    for i in list_of_clusters:
        
        plt.scatter(
        logphizlogrqi[klabels == i, 0], logphizlogrqi[klabels == i, 1],
        s=10, cmap='hsv',
        marker='8',
        label=i+1
        )
    
    
    
    ax.yaxis.tick_right()
    ax.yaxis.set_ticks_position('both')
    plt.legend(scatterpoints=1, loc='center left',bbox_to_anchor=(-0.4, 0.5))
    plt.grid()
    plt.show()

but the resulting graph uses each color more than once as you can see in the plot below:

Ideally, the graph I'm looking for should look like the following (although the colors are close to each other, they are used only once):

I'd really appreciate it if you could help me fixing my problem

5
  • You can look at some of existing colormaps and see if any of them work in your case. Commented Oct 24, 2020 at 6:51
  • @hilberts_drinking_problem I have. all of them result in the same plot. I've tried Qualitative, Miscellaneous, Diverging etc. but non of them worked. Commented Oct 24, 2020 at 7:02
  • https://stackoverflow.com/questions/8389636/creating-over-20-unique-legend-colors-using-matplotlib/44937195#44937195I find this answer to be very helpful Commented Oct 24, 2020 at 7:16
  • @r-beginners does not work unfortunately. i receive the following error: (AttributeError: 'AxesSubplot' object has no attribute 'set_color') .the other answers fail to work as well Commented Oct 24, 2020 at 8:16
  • I usually use something like this solution with a non-repetitive colormap. But in general, 27 unique colors that are easily distinguishable is ambitious. Commented Oct 24, 2020 at 9:36

2 Answers 2

1

Please add and fix the following code in the example of seaborn in response to a comment.

Add.

import seaborn as sns

NUM_COLORS = 27

sns.reset_orig() 
colors = sns.color_palette('husl', n_colors=NUM_COLORS)

editing

for i in list_of_clusters:
    plt.scatter(
    logphizlogrqi[klabels == i, 0], logphizlogrqi[klabels == i, 1],
    s=10, cmap='hsv',
    marker='8',
    label=i+1,
    color=colors[i] # update
    )

enter image description here

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

Comments

0

so I came up with this method and it worked fine:

from matplotlib import colors as mcolors
import random

list_of_clusters= list(set(klabels))

colors = list(mcolors.CSS4_COLORS.keys())
random.shuffle(colors)

fig=plt.figure(figsize=(13,9),frameon=True,facecolor='lightgrey', edgecolor='black')
ax = fig.add_subplot(1,1,1)
plt.axis()
plt.xlim([-2.5, 0.2])
plt.ylim([-0.7, 3.3])
plt.xlabel("log PhiZ")
plt.ylabel("log RQI")

for i in list_of_clusters:
    plt.scatter(
    logphizlogrqi[klabels == i, 0], logphizlogrqi[klabels == i, 1],
    s=10,
    marker='8',
    label=i+1,
    color=colors[i]
    )

ax.yaxis.tick_right()
ax.yaxis.set_label_position('right')
ax.yaxis.set_ticks_position('right')
plt.legend(scatterpoints=1, loc='center left',bbox_to_anchor=(-0.4, 0.5))
plt.show()

The result looks like below:

Comments

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.