Luckasino
Luckasino

Reputation: 424

Second x axis or x labels

I would like to ask you if there is a way how to create a second x-axis in Altair (put the daytime on top)?

bar_cumm_TAGP = alt.Chart(df_TAGP_2[df_TAGP_2['simulation_date'] == '2020-10- 
15']).mark_bar().encode(
        alt.X('DVS:O'),
        alt.X2('day:O'),
        alt.Y(f'TAGP:Q', axis=alt.Axis(titlePadding=10, titleFontSize=14), title=""),
        alt.Color(f'TAGP:Q', scale=alt.Scale(scheme='greens'))).properties(width=850, height=350)

line = alt.Chart(df_TAGP_2).mark_line(interpolate='basis', strokeWidth=2, color='#FB0000').encode(
alt.X('DVS'),
alt.Y('mean(TAGP)'))

alt.layer(bar_cumm_TAGP, line).resolve_scale(
x = 'independent'
)

My code snippet:

condition = [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]
df_TAGP_2 = pd.DataFrame({
     'day': pd.date_range('1990-01-01', freq='D', periods=10000),
     'DVS': np.random.choice(condition , 10000,),
     'TAGP': np.random.randint(4000, size=(10000)),
     'simulation_date': '2020-10-15'})

My result:

enter image description here

What I would like to see enter image description here

Many thanks!

Upvotes: 1

Views: 250

Answers (1)

joelostblom
joelostblom

Reputation: 48919

It looks like you already have a dual x-axis in the picture. Some of the weirdness you are seeing with the different length of the top and bottom axis have been resolved in the current development version of Altair, in which you chart would look like this:

enter image description here

You can try it out by downloading it directly from the Altair GitHub page.

Update after question edit:

import altair as alt
import numpy as np
import pandas as pd

condition = [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]
df_TAGP_2 = pd.DataFrame({
     'day': pd.date_range('1990-01-01', freq='D', periods=20),
     'DVS': np.random.choice(condition , 20,),
     'TAGP': np.random.randint(10, size=(20)),
     'simulation_date': '2020-10-15'})

bar_cumm_TAGP = alt.Chart(df_TAGP_2[df_TAGP_2['simulation_date'] == '2020-10-15']).mark_bar().encode(
        alt.X('DVS:O'),
        alt.Y(f'TAGP:Q', title=""),
        alt.Color(f'TAGP:Q', scale=alt.Scale(scheme='greens'))).properties(width=850, height=350)

line = alt.Chart(df_TAGP_2).mark_line(interpolate='basis', strokeWidth=2, color='#FB0000').encode(
alt.X('day:O', axis=alt.Axis(labelPadding=100)),
alt.Y('mean(TAGP)'))

alt.layer(bar_cumm_TAGP, line).resolve_scale(
x = 'independent'
)

enter image description here

Upvotes: 1

Related Questions