简体   繁体   中英

Altair: How to color when there are more categories than colors available

Here is the code taken from the Altair documentation on color schemes.

alt.Chart(iris).mark_point().encode(
    x='petalWidth',
    y='petalLength',
    color=alt.Color('species', scale=alt.Scale(scheme='dark2'))
)

Here, to color based on species, they used the dark2 color scheme. There are 8 colors in the dark2 vega scheme. I have noticed that when there more categories than colors available, it appears that the colors are recycled again, which means some categories have the same color. In this example, suppose species had more than 8 categories. This means two species will be colored in the same way.

Is there anyway to within Altair to automatically 'extend' the dark2 color scheme to support more categories? I was thinking something along the lines of interpolation or mixing colors in the scheme.

As a side note, I'm aware that there are other color schemes with more colors available, but I would like to have a solution which I mentioned above with dark2.

It is not possible to extend a color scheme automatically via Altair/VegaLite. You would need to manually define the colors you would want to use, or switch to another color scheme, as you mentioned.

Note that when you are using this many categorical colors, it can become quite difficult for the reader to parse the information in the plot and it is usually better to break down the plot into multiple plots via faceting, or use labels/hovering to encode some of the information instead of relying solely on color. There are some good pointers on this topic in this book chapter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM