[Python] Second independent axis in Altair

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I would like to add a second (independent) x-axis to my figure, demonstrating a month for a given week.

    Here is my snippet:

    import pandas as pd
    import numpy as np
    from datetime import datetime, timedelta

    weeks = list(range(0, 54))
    start_date = datetime(1979, 1, 1)
    week_dates = [start_date + timedelta(weeks=w) for w in weeks]

    years = list(range(1979, 2024))
    data = {
    "week": weeks,
    "date": week_dates,
    "month": [date.strftime("%b") for date in week_dates],
    }
    # Precipitation
    data.update({str(year): np.random.uniform(0, 100, size=len(weeks)) for year in years})
    df = pd.DataFrame(data)
    df["Mean_Precipitation"] = df[[str(year) for year in years]].mean(axis=1)

    rain_calendar = alt.Chart(df, title=f"Weekly precipitation volumes").mark_bar().encode(
    alt.X('week:O', title='Week in calendar year', axis=alt.Axis(labelAlign="right", labelAngle=-45)),
    alt.Y(f'1979:Q', title='Rain'),
    alt.Color(f"1979:Q", scale=alt.Scale(scheme='redyellowgreen')),
    alt.Order(f"1979:Q", sort="ascending")).properties(width=850, height=350)

    line = alt.Chart(df).mark_line(interpolate='basis', strokeWidth=2, color='#FB0000').encode(
    alt.X('week:O'),
    alt.Y('Mean_Precipitation:Q'))

    month_labels = alt.Chart(df).mark_text(
    align='center', baseline='top', dy=30
    ).encode(
    alt.X('week:O', title=None), text='month:N')

    combined_chart = alt.layer(rain_calendar, line, month_labels).resolve_scale(x='shared').configure_axisX(
    labelAlign="right", labelAngle=-45, titlePadding=10, titleFontSize=14)\
    .configure_axisY(titlePadding=10, titleFontSize=14).configure_bar(binSpacing=0)


    So far, this is the best outcome that I've got:

    [​IMG]

    It is a topic related to discussed one there: Second x axis or x labels

    Continue reading...

Compartilhe esta Página