Issue
I am trying to make some plots using plotly and matplotlib, and the data that I am using usually is sorted into around 4 to 8 different groups, each of which I set to one discrete color within my plot. I found a good resource here (https://plotly.com/python/discrete-color/) that allows me to get one color per group. However, recently I have had to plot up to thirty different groups, which is a problem, because if you look at the resource I just linked to, the maximum number of discrete colors per series (e.g. plotly.colors.qualitative.Light24, plotly.colors.qualitative.Antique, etc.) is only 24.
In order to plot my data, I really would like to have the ability to get the correct number of colors for the number of groups in my data (that are also visually distinct from each other). But I also have the additional issue that the matplotlib plotting tool that I am using only takes colors in rgb format (e.g. rgb(158,185,243)) and not hexadecimal format (e.g. #00B5F7).
I have solved this problem so far by just getting the discrete color series that are in rgb format (e.g. plotly.colors.qualitative.Dark1, plotly.colors.qualitative.Set1) as multiple lists and then adding the lists together until I have enough discrete colors, but sometimes this results in some colors being too similar to each other. It also means that it is not dynamic to input with different numbers of groups.
Does anybody have a better strategy for getting the correct number of discrete colors from plotly?
Solution
Ok so I found an approach that works best for me. Granted, it's not the most elegant, but given that my matplotlib only works with rgb values that are formatted as a list of lists of integers between 0 and 255, here is the code I made:
import random
NUM_COLORS = 30
color_set = set()
while len(color_set)<NUM_COLORS:
random_integers = [random.randint(0, 255) for _ in range(3)]
color_set.add(tuple(random_integers))
color_set = list(color_set)
color_set = [list(n) for n in color_set]
color_set
Granted, it does not always yield a color scheme that I find to be the most "appealing" or "cohesive", but I usually just rerun the function until the color scheme looks better.
Answered By - Bob McBobson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.