Preserve color mapping across variables #2993
-
QuestionIs there a way to preserve color mapping across variables even when removing data? MWEYou have a response that outshines all others, you want to show the scale, but then want to move in to a more zoomed in view to better compare the rest. In the example below, it would be less disorienting to have variable 'D' always be light blue. import altair as alt
import pandas as pd
source = pd.DataFrame({
"variable": ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
"response": [28, 55, 2043, 91, 81, 53, 19, 87, 52]
})
alt.Chart(source).mark_bar().encode(
x="variable:N",
y="response:Q",
color="variable:N"
).save("color.png", webdriver="firefox") After filtering out variable C to get a better view: source = source[source["variable"] != 'C'] After masking variable C (I thought this would reserve a spot for it in the mapping but does the same as above): source = source.mask(source["variable"] == 'C') |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can set the color encodings scale domain to a list of the values to include. On my phone right now so it's hard to type, but something like |
Beta Was this translation helpful? Give feedback.
You can set the color encodings scale domain to a list of the values to include. On my phone right now so it's hard to type, but something like
alt.Scale(domain=[A, B, C... ])
inside the color encoding