Reputation: 11
I am struggling to interpret a month and day (MMDD, or MM_DD in my case) info as "Time" in my Altair graph.
The data I am working with contains the daily level of a lake from year 1850 until today. I calculated the min, max, mean value for every individual day of the year (mm_dd). Then, I converted my data frame to a long format, which now looks like this:
[] | mm_dd | value_type | levelcm | month |
---|---|---|---|---|
0 | 01_01 | value_min | 242 | 01 |
1 | 01_02 | value_min | 243 | 01 |
2 | 01_01 | value_mean | 301 | 01 |
3 | 01_02 | value_mean | 300 | 01 |
4 | 01_01 | value_max | 380 | 01 |
5 | 01_02 | value_max | 380 | 01 |
With
alt.Chart(df_mmm_long).mark_line().encode(
x='mm_dd',
y='levelcm',
color='value_type'
)
I am getting
Ideally, I would like the line chart to be based on the mm_dd information, but have a monthly scale on the X-axis.
Upvotes: 1
Views: 159
Reputation: 231
If you change a type of the column with dates to datetime, Altair will nicely group labels for you. Here in my example I took Rhein data for the last 25 years or so... :
a=df_.groupby(["Month", "Tag"], as_index="False").agg(minimum = ('Level', 'min'), \
average = ('Level', 'mean'),
maximum = ('Level', 'max')).reset_index()#.transpose()
a.Month=a.Month.replace({'Januar':1, 'Februar':2, 'Maerz':3, 'April':4, 'Mai':5, 'Juni':6, 'Juli':7,
'August':8, 'September':9, 'Oktober':10, 'November':11, 'Dezember':12})
#combine day and month to date column
a['date']=a.Tag.astype(str)+"/"+a.Month.astype(str)+"/2020"
a.date=pd.to_datetime(a['date'], errors='ignore')
alt.Chart(a).transform_fold(
['minimum', 'average', 'maximum']).mark_line().encode(
x=alt.X("date:T"),
y=alt.Y("value:Q",),
color='key:N'
).properties(width=600)
still not the nicest chart and requires some work, but as a start)
I guess you will add a curent level for 2022, so it's fine if the column date is 2022. In your example - they also have a current year at the axes labels
link to the data - Rhein data if someone wants to help
P.S. Or you want a chart by month like that one (source:datawrapper)
Upvotes: 1