Altair setting constant label color for bar chart












0















An example of setting labels for a bar chart in Altair is provided in official website here: https://altair-viz.github.io/gallery/bar_chart_with_labels.html



However, once you want to set the "color" parameter in the bar chart conditioned to a variable, the label colours automatically match the color of the bar which is illustrated below. However, my intention is to have constant label color, like black all the time. This is especially desirable for stacked bar charts if you want to show the label as percentage. It seems like setting "color='black'" in the mark_text does not work here; probably because it is based on "bars" which uses "color" parameter as "year". But I could not find an intuitive way to decouple this parameter.



import altair as alt
from vega_datasets import data

source = data.wheat()

bars = alt.Chart(source).mark_bar().encode(
x='wheat:Q',
y="year:O",
color='year:O'

)

text = bars.mark_text(
align='left',
baseline='middle',
color='black',
dx=3 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='wheat:Q'

)

(bars + text).properties(height=900)


Bar chart with Variable Label Colors



Stacked Bar Chart Example with coloured labels










share|improve this question

























  • Hello, I am not sure if I understand what you want correctly : the whole point of setting an encoding color is to map the variable to a color, which will be different for all the levels of your variable. If you want a constant color, just drop the color=... in the encoding. Is that what you want or am I missing something?

    – FlorianGD
    Jan 3 at 9:48











  • I added a stacked bar chart example to illustrate the problem. I would like to use "color" encoding in the stacked bar chart to illustrate different components that ends up to 100%. In this scheme, visually it is preferable to have single Label color like black rather than different colors that might blend and lost in the stacked bar chart.

    – oekici
    Jan 3 at 11:10











  • If I understand correctly, you want a stacked bar chart with all the colors the same? Something like that but with all the colors the same?

    – FlorianGD
    Jan 3 at 14:07
















0















An example of setting labels for a bar chart in Altair is provided in official website here: https://altair-viz.github.io/gallery/bar_chart_with_labels.html



However, once you want to set the "color" parameter in the bar chart conditioned to a variable, the label colours automatically match the color of the bar which is illustrated below. However, my intention is to have constant label color, like black all the time. This is especially desirable for stacked bar charts if you want to show the label as percentage. It seems like setting "color='black'" in the mark_text does not work here; probably because it is based on "bars" which uses "color" parameter as "year". But I could not find an intuitive way to decouple this parameter.



import altair as alt
from vega_datasets import data

source = data.wheat()

bars = alt.Chart(source).mark_bar().encode(
x='wheat:Q',
y="year:O",
color='year:O'

)

text = bars.mark_text(
align='left',
baseline='middle',
color='black',
dx=3 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='wheat:Q'

)

(bars + text).properties(height=900)


Bar chart with Variable Label Colors



Stacked Bar Chart Example with coloured labels










share|improve this question

























  • Hello, I am not sure if I understand what you want correctly : the whole point of setting an encoding color is to map the variable to a color, which will be different for all the levels of your variable. If you want a constant color, just drop the color=... in the encoding. Is that what you want or am I missing something?

    – FlorianGD
    Jan 3 at 9:48











  • I added a stacked bar chart example to illustrate the problem. I would like to use "color" encoding in the stacked bar chart to illustrate different components that ends up to 100%. In this scheme, visually it is preferable to have single Label color like black rather than different colors that might blend and lost in the stacked bar chart.

    – oekici
    Jan 3 at 11:10











  • If I understand correctly, you want a stacked bar chart with all the colors the same? Something like that but with all the colors the same?

    – FlorianGD
    Jan 3 at 14:07














0












0








0








An example of setting labels for a bar chart in Altair is provided in official website here: https://altair-viz.github.io/gallery/bar_chart_with_labels.html



However, once you want to set the "color" parameter in the bar chart conditioned to a variable, the label colours automatically match the color of the bar which is illustrated below. However, my intention is to have constant label color, like black all the time. This is especially desirable for stacked bar charts if you want to show the label as percentage. It seems like setting "color='black'" in the mark_text does not work here; probably because it is based on "bars" which uses "color" parameter as "year". But I could not find an intuitive way to decouple this parameter.



import altair as alt
from vega_datasets import data

source = data.wheat()

bars = alt.Chart(source).mark_bar().encode(
x='wheat:Q',
y="year:O",
color='year:O'

)

text = bars.mark_text(
align='left',
baseline='middle',
color='black',
dx=3 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='wheat:Q'

)

(bars + text).properties(height=900)


Bar chart with Variable Label Colors



Stacked Bar Chart Example with coloured labels










share|improve this question
















An example of setting labels for a bar chart in Altair is provided in official website here: https://altair-viz.github.io/gallery/bar_chart_with_labels.html



However, once you want to set the "color" parameter in the bar chart conditioned to a variable, the label colours automatically match the color of the bar which is illustrated below. However, my intention is to have constant label color, like black all the time. This is especially desirable for stacked bar charts if you want to show the label as percentage. It seems like setting "color='black'" in the mark_text does not work here; probably because it is based on "bars" which uses "color" parameter as "year". But I could not find an intuitive way to decouple this parameter.



import altair as alt
from vega_datasets import data

source = data.wheat()

bars = alt.Chart(source).mark_bar().encode(
x='wheat:Q',
y="year:O",
color='year:O'

)

text = bars.mark_text(
align='left',
baseline='middle',
color='black',
dx=3 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='wheat:Q'

)

(bars + text).properties(height=900)


Bar chart with Variable Label Colors



Stacked Bar Chart Example with coloured labels







python data-visualization altair






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 3:38









jakevdp

20.1k33657




20.1k33657










asked Jan 3 at 1:27









oekicioekici

143




143













  • Hello, I am not sure if I understand what you want correctly : the whole point of setting an encoding color is to map the variable to a color, which will be different for all the levels of your variable. If you want a constant color, just drop the color=... in the encoding. Is that what you want or am I missing something?

    – FlorianGD
    Jan 3 at 9:48











  • I added a stacked bar chart example to illustrate the problem. I would like to use "color" encoding in the stacked bar chart to illustrate different components that ends up to 100%. In this scheme, visually it is preferable to have single Label color like black rather than different colors that might blend and lost in the stacked bar chart.

    – oekici
    Jan 3 at 11:10











  • If I understand correctly, you want a stacked bar chart with all the colors the same? Something like that but with all the colors the same?

    – FlorianGD
    Jan 3 at 14:07



















  • Hello, I am not sure if I understand what you want correctly : the whole point of setting an encoding color is to map the variable to a color, which will be different for all the levels of your variable. If you want a constant color, just drop the color=... in the encoding. Is that what you want or am I missing something?

    – FlorianGD
    Jan 3 at 9:48











  • I added a stacked bar chart example to illustrate the problem. I would like to use "color" encoding in the stacked bar chart to illustrate different components that ends up to 100%. In this scheme, visually it is preferable to have single Label color like black rather than different colors that might blend and lost in the stacked bar chart.

    – oekici
    Jan 3 at 11:10











  • If I understand correctly, you want a stacked bar chart with all the colors the same? Something like that but with all the colors the same?

    – FlorianGD
    Jan 3 at 14:07

















Hello, I am not sure if I understand what you want correctly : the whole point of setting an encoding color is to map the variable to a color, which will be different for all the levels of your variable. If you want a constant color, just drop the color=... in the encoding. Is that what you want or am I missing something?

– FlorianGD
Jan 3 at 9:48





Hello, I am not sure if I understand what you want correctly : the whole point of setting an encoding color is to map the variable to a color, which will be different for all the levels of your variable. If you want a constant color, just drop the color=... in the encoding. Is that what you want or am I missing something?

– FlorianGD
Jan 3 at 9:48













I added a stacked bar chart example to illustrate the problem. I would like to use "color" encoding in the stacked bar chart to illustrate different components that ends up to 100%. In this scheme, visually it is preferable to have single Label color like black rather than different colors that might blend and lost in the stacked bar chart.

– oekici
Jan 3 at 11:10





I added a stacked bar chart example to illustrate the problem. I would like to use "color" encoding in the stacked bar chart to illustrate different components that ends up to 100%. In this scheme, visually it is preferable to have single Label color like black rather than different colors that might blend and lost in the stacked bar chart.

– oekici
Jan 3 at 11:10













If I understand correctly, you want a stacked bar chart with all the colors the same? Something like that but with all the colors the same?

– FlorianGD
Jan 3 at 14:07





If I understand correctly, you want a stacked bar chart with all the colors the same? Something like that but with all the colors the same?

– FlorianGD
Jan 3 at 14:07












1 Answer
1






active

oldest

votes


















0














When you do bars.mark_text() the resulting chart inherits everything you specified within the bars chart, including the color encoding. To avoid having a color encoding for the text, the best approach is to make sure it doesn't inherit a color encoding.



For example:



import altair as alt
from vega_datasets import data

source = data.wheat()

base = alt.Chart(source).encode(
x='wheat:Q',
y="year:O"
)

bars = base.mark_bar().encode(
color='year:O'
)

text = base.mark_text(
align='left',
baseline='middle',
dx=3 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='wheat:Q'
)

(bars + text).properties(height=900)


The reason mark_text(color='black') didn't override the encoding in your snippet is because the color encoding takes precedence over mark properties, as described in Global Config vs. Local Config vs. Encoding.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015250%2faltair-setting-constant-label-color-for-bar-chart%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    When you do bars.mark_text() the resulting chart inherits everything you specified within the bars chart, including the color encoding. To avoid having a color encoding for the text, the best approach is to make sure it doesn't inherit a color encoding.



    For example:



    import altair as alt
    from vega_datasets import data

    source = data.wheat()

    base = alt.Chart(source).encode(
    x='wheat:Q',
    y="year:O"
    )

    bars = base.mark_bar().encode(
    color='year:O'
    )

    text = base.mark_text(
    align='left',
    baseline='middle',
    dx=3 # Nudges text to right so it doesn't appear on top of the bar
    ).encode(
    text='wheat:Q'
    )

    (bars + text).properties(height=900)


    The reason mark_text(color='black') didn't override the encoding in your snippet is because the color encoding takes precedence over mark properties, as described in Global Config vs. Local Config vs. Encoding.






    share|improve this answer




























      0














      When you do bars.mark_text() the resulting chart inherits everything you specified within the bars chart, including the color encoding. To avoid having a color encoding for the text, the best approach is to make sure it doesn't inherit a color encoding.



      For example:



      import altair as alt
      from vega_datasets import data

      source = data.wheat()

      base = alt.Chart(source).encode(
      x='wheat:Q',
      y="year:O"
      )

      bars = base.mark_bar().encode(
      color='year:O'
      )

      text = base.mark_text(
      align='left',
      baseline='middle',
      dx=3 # Nudges text to right so it doesn't appear on top of the bar
      ).encode(
      text='wheat:Q'
      )

      (bars + text).properties(height=900)


      The reason mark_text(color='black') didn't override the encoding in your snippet is because the color encoding takes precedence over mark properties, as described in Global Config vs. Local Config vs. Encoding.






      share|improve this answer


























        0












        0








        0







        When you do bars.mark_text() the resulting chart inherits everything you specified within the bars chart, including the color encoding. To avoid having a color encoding for the text, the best approach is to make sure it doesn't inherit a color encoding.



        For example:



        import altair as alt
        from vega_datasets import data

        source = data.wheat()

        base = alt.Chart(source).encode(
        x='wheat:Q',
        y="year:O"
        )

        bars = base.mark_bar().encode(
        color='year:O'
        )

        text = base.mark_text(
        align='left',
        baseline='middle',
        dx=3 # Nudges text to right so it doesn't appear on top of the bar
        ).encode(
        text='wheat:Q'
        )

        (bars + text).properties(height=900)


        The reason mark_text(color='black') didn't override the encoding in your snippet is because the color encoding takes precedence over mark properties, as described in Global Config vs. Local Config vs. Encoding.






        share|improve this answer













        When you do bars.mark_text() the resulting chart inherits everything you specified within the bars chart, including the color encoding. To avoid having a color encoding for the text, the best approach is to make sure it doesn't inherit a color encoding.



        For example:



        import altair as alt
        from vega_datasets import data

        source = data.wheat()

        base = alt.Chart(source).encode(
        x='wheat:Q',
        y="year:O"
        )

        bars = base.mark_bar().encode(
        color='year:O'
        )

        text = base.mark_text(
        align='left',
        baseline='middle',
        dx=3 # Nudges text to right so it doesn't appear on top of the bar
        ).encode(
        text='wheat:Q'
        )

        (bars + text).properties(height=900)


        The reason mark_text(color='black') didn't override the encoding in your snippet is because the color encoding takes precedence over mark properties, as described in Global Config vs. Local Config vs. Encoding.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 21:45









        jakevdpjakevdp

        20.1k33657




        20.1k33657
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015250%2faltair-setting-constant-label-color-for-bar-chart%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith