1

I want to plot 20 newsgroup dataset topics using matplotlib. I have writted the code below:

markers = ["o", "v", "8", "s", "p", "*", "h", "H", "+", "x", "D"]
    plt.rc('legend',**{'fontsize':10})
    classes_to_visual = list(set(classes_to_visual))
    C = len(classes_to_visual)
    while True:
        if C <= len(markers):
            break
        markers += markers

    class_ids = dict(zip(classes_to_visual, range(C)))

    if isinstance(doc_codes, dict) and isinstance(doc_labels, dict):
        codes, labels = zip(*[(code, doc_labels[doc]) for doc, code in doc_codes.items() if doc_labels[doc] in classes_to_visual])
    else:
        codes, labels = doc_codes, doc_labels

    X = np.r_[list(codes)]
    tsne = TSNE(perplexity=40, n_components=2, init='pca', n_iter=5000)
    np.set_printoptions(suppress=True)
    X = tsne.fit_transform(X)
    # NUM_COLORS = 20
    plt.figure(figsize=(10, 10), facecolor='white')

    for c in classes_to_visual:
        idx = np.array(labels) == c
        plt.plot(X[idx, 0], X[idx, 1], linestyle='None', alpha=1, marker=markers[class_ids[c]],
                 markersize=10, label=c)
    legend = plt.legend(loc='upper right', shadow=True)
    plt.savefig(save_file, format='eps', dpi=2000)
    plt.show()

My only problem is that it plotted the 20 news group using 11 main colors and for distinguishing others it has markers. but I want to have different distinguishable colors.

I tried various ways like defining colors:

ax.set_color_cycle([cm(1. * i / NUM_COLORS) for i in range(NUM_COLORS)])

though in some cases the colors was only different shade and did not differ that much!!

What can I do to get it work properly? so far this is the result: enter image description here

sariii
  • 2,020
  • 6
  • 29
  • 57
  • This might be of help https://stackoverflow.com/questions/44806598/matplotlib-set-color-cycle-versus-set-prop-cycle – anushka Oct 09 '19 at 06:52
  • Please separate: Do you need help getting 20 colors or do you need help getting those colors into the color cycle? – ImportanceOfBeingErnest Oct 09 '19 at 15:33
  • @ImportanceOfBeingErnest Actually I need to get 20 different colors, if you look at my figure, for example, blue has been repeated for two different categories but by different markers. what I need is only have differently distinguishable colors – sariii Oct 10 '19 at 02:56
  • @anushka the problem with that link is that it follows the order. for example, in some case, it might show the figure with different shades of blue, green, pink which those shades are very similar such that hardly can be differentiated – sariii Oct 10 '19 at 02:57
  • 1
    Some reading [this](https://stackoverflow.com/questions/8389636/creating-over-20-unique-legend-colors-using-matplotlib), [this](https://stackoverflow.com/questions/876853/generating-color-ranges-in-python), , or [this](https://stackoverflow.com/questions/42697933/colormap-with-maximum-distinguishable-colours) or more generally [this](https://stackoverflow.com/questions/470690/how-to-automatically-generate-n-distinct-colors) – ImportanceOfBeingErnest Oct 10 '19 at 03:02
  • Thank you so much, I am trying the third link. do you know how can I incorporate the `ax` in the code above? – sariii Oct 10 '19 at 03:13
  • @ImportanceOfBeingErnest Actually I spent some time though not able to get it run. the first link does not work on my case as it will assign colors in order. for example all shades of green red .. so that the figure I get again the colors are not that much distinctable. the third link I could not incorporate in my code :! – sariii Oct 13 '19 at 15:23
  • I mean there is no magic function that gives you the colors you want. You need to create a list/array of those colors. If unhappy with existing solutions, just type them up like `["red", "green", "salmon", "navy", ....]` – ImportanceOfBeingErnest Oct 13 '19 at 15:31
  • But I did not find anywhere in matplotlib that you can specify a list of coulurs manually? Do you mind expaining according to the code I have where can I put this list of colors? – sariii Oct 13 '19 at 18:54
  • Still did not find any solution :(. I tried to give a list of colours to `c arument` in matplotlib byt it throws an error `ValueError: Invalid RGBA argument:` . Do you have any ide of this? I appreciate if you could help me fix this:) – sariii Oct 13 '19 at 22:00
  • The aim is to change the property cycle to use those colors. Check the links provided in the comment from 4 days ago. – ImportanceOfBeingErnest Oct 14 '19 at 02:01
  • Well, fixed :), any idea how can we have the legend but making sure not overlapped with the figure? I put legend of 20 categories name upper righ but still overlap with figure. – sariii Oct 14 '19 at 03:48

0 Answers0