I created a DataFrame with stacked data, i.e., tested \in total.
import pandas as pd
import altair as alt
df = pd.DataFrame({
'date': ['2021-01-01', '2021-02-01', '2021-03-01'],
'total': [10, 15, 20],
'tested': [0, 5, 10]
})
dfm = df.melt(id_vars=['date'])
I would like to plot a stacked bar plot with Altair. Since the tested
column is already contained in the total
column, I would expect a chart with the max values of the total
, but the result shows the sum.
alt.Chart(dfm).mark_bar().encode(
x='date:O',
y='value:Q',
color='variable:O'
)
I know I can use pandas to create an untested
column and generate the plot using tested
and untested
columns, but I would like to know if I can achieve this result without transforming the data.
CodePudding user response:
To create an unstacked bar chart you can set stack=False
:
alt.Chart(dfm).mark_bar().encode(
x='date:O',
y=alt.Y('value:Q', stack=False),
color='variable:O'
)
Note that it will always show the rightmost column on top (tested
in the image above).