Jump to content
  • 0

Fixed Color-coding to legends categories


sanza

Question

Hello Caspians

I need some assistance on the issue i cannot get a resolve on

I wish to have fixed specific colors on a legend for three categories (Negative=Red; Positive=Green and Neutral=Orange)

The main objective is to have the above colors fixed on the legend and charts/bars even after search queries are conducted. However, what I have encountered is that after entering search, queries, one finds that negative now becomes green and so forth.

I have so far managed to style them according to these colors on default - problem only emerges when a search function is conducted.

Is there a way one can assist or perhaps with some configurations or code/script that one can utilize?

I have attached images with illustrations of what im referring to

I would so much appreciate any form of solution

Thanks and regards

Sanza

 

 

forum.jpg

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

You can set specific colors for categories on a legend by modifying the color map used in the plotting library you are using.

For example, in matplotlib library, you can use the ListedColormap class to create a custom color map, then pass it to the cmap parameter of the plotting function.


import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

# Define custom color map
custom_cmap = ListedColormap(['red', 'green', 'orange'])

# Plot data using custom color map
plt.scatter(x, y, c=categories, cmap=custom_cmap)

# Add legend
plt.legend(['Negative','Positive','Neutral'])

plt.show()


You can also use similar method in other plotting libraries such as seaborn, plotly or bokeh

Please note that for the above example to work, you need to have three different categories and the 'categories' variable should have the same length as x and y and match the order of the categories in the legend.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...