Search code examples
pythonaltair

Make bar chart's x-axis markers horizontal or 45 degree readable in Python Altair


I am trying to create a bar chart with year data on x-axis. It works but the year marker on x-xais are all in vertical direction and I want to make them more readable - either horizontal or 45 degree. I tried using the year:T in datatime format but it gave me the year and month markers (I just wanted to have the year markers on xais). How do I make the year markers on x-axis either horizontal or 45 degree?

import altair as alt
from vega_datasets import data

source = data.wheat()
source.info()

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

enter image description here


Solution

  • Set labelAngle:

    alt.Chart(source).mark_bar().encode(
        x=alt.X('year:O', axis=alt.Axis(labelAngle=-45)),
        y="wheat:Q"
    )
    

    enter image description here