Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify the color #921

Closed
robalvsan opened this issue Jun 7, 2018 · 10 comments
Closed

Specify the color #921

robalvsan opened this issue Jun 7, 2018 · 10 comments

Comments

@robalvsan
Copy link

mv_dict = {'type': ['values', 'values'], 'quantity': [5, 10], 'category': ['mv', 'no_mv']}
df = pd.DataFrame(mv_dict)
alt.Chart(df).mark_bar().encode(
    y='type',
    x='sum(quantity)',
    color='category',
)

With this piece of code, the graph is generated automatically using blue for 'mv' and orange for 'no_mv'

image

Since in some contexts, the color has an implicit connotation...could it be possible to specify what color I want to use for 'mv' (e.g. red) and what for 'no_mv' (e.g. green)?

@afonit
Copy link
Contributor

afonit commented Jun 7, 2018

Here you go:

import altair as alt
import pandas as pd
mv_dict = {'type': ['values', 'values'], 'quantity': [5, 10], 'category': ['mv', 'no_mv']}
df = pd.DataFrame(mv_dict)
alt.Chart(df).mark_bar().encode(
    y='type',
    x='sum(quantity)',
    color=alt.Color('category',
                   scale=alt.Scale(
            domain=['mv', 'no_mv'],
            range=['red', 'green'])))

visualization 2

@robalvsan
Copy link
Author

Awesome, thanks!

@jakevdp
Copy link
Collaborator

jakevdp commented Jun 7, 2018

You can also use alt.Color('category', scale=alt.Scale(scheme=scheme_name)) where scheme_name is a string that matches any of the available Vega color schemes: https://vega.github.io/vega/docs/schemes/#reference

Edit: scheme should be a scale argument

@afonit
Copy link
Contributor

afonit commented Jun 7, 2018

@jakevdp , I tried what you posted:
alt.Color('category', scheme='accent')
I got a schema validation error:
Additional properties are not allowed ('scheme' was unexpected)

however doing:
color=alt.Color('category', scale=alt.Scale(scheme='accent'))
did work.
I updated to 2.1 to test what you had posted - perhaps I am doing something wrong.

@jakevdp
Copy link
Collaborator

jakevdp commented Jun 7, 2018

sorry, yes, scheme is a Scale argument. glad you figured it out 😄

@sophiamaedler
Copy link

@jakevdp Do you have any tips on using vega schemes with log scales? I tried
alt.Color('Name', type = 'quantitative', scale=alt.Scale(scheme=scheme_name, type='log'))
but this resulted in the scheme command simply being ignored to generate the log scale and the standard colors being utilized.

@lightalchemist
Copy link

@Artemis-R I have the same issue as you but for the case where I set type="log". Do you have a fix? Thanks.

@SuperShinyEyes
Copy link

Here you go:

import altair as alt
import pandas as pd
mv_dict = {'type': ['values', 'values'], 'quantity': [5, 10], 'category': ['mv', 'no_mv']}
df = pd.DataFrame(mv_dict)
alt.Chart(df).mark_bar().encode(
    y='type',
    x='sum(quantity)',
    color=alt.Color('category',
                   scale=alt.Scale(
            domain=['mv', 'no_mv'],
            range=['red', 'green'])))

visualization 2

The specified colors break if you concatenate charts. :\

@fredgi123
Copy link

Is there a way to specifically have the 0 value returns a white color and the rest in the normal scheme?
If for instance, I use color=("variable:Q", scale=alt.Scale(scheme="blues")) my 0 values are still displaying blue values not white. thank you!

@tiaaburton
Copy link

@fredgi123 this answer on another issue was helpful. I also tried blues in an attempt to get 0 to display as white. I just used this discrete method to describe the scale I'd like to see. If you don't mind what the middle values are, you only have to specify 0, the cutoff for white coloring, and max. I added in the clamp=True param to make sure they weren't interacting with one another. #2258 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants