-
Hi and a big thank you for altair! I have a table which I want to visualize as a bar chart. On the X axis, I want to bin the values into the given intervals (they are strings). On the Y axis, I want to give the percentage of positive values with respect to the individual bin. As an example for the example table below, for the
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
There is |
Beta Was this translation helpful? Give feedback.
-
You can do this by roughly following the Percent of Total example, along with some pre-processing of your data in a calculate transform. For example: import pandas as pd
import io
import altair as alt
data = """\
ind value bins
0 Negative 0 - 50
1 Negative 0 - 50
2 Negative 0 - 50
3 Negative 0 - 50
4 Negative 0 - 50
5 Negative 0 - 50
6 Positive 51 - 100
7 Positive 51 - 100
8 Negative 51 - 100
9 Negative 51 - 100
10 Positive 0 - 50
11 Positive 0 - 50
12 Positive 0 - 50
13 Positive 0 - 50
14 Positive 0 - 50
15 Positive 0 - 50
"""
source = pd.read_csv(io.StringIO(data), delimiter='\s{2,}', engine='python')
alt.Chart(source).transform_calculate(
is_positive=(alt.datum.value == "Positive")
).transform_joinaggregate(
total_pos='sum(is_positive)',
total='count()',
groupby=['bins']
).transform_calculate(
percent=alt.datum.total_pos / alt.datum.total
).mark_bar().encode(
y='bins:N',
x=alt.X('percent:Q', axis=alt.Axis(format='%'))
) |
Beta Was this translation helpful? Give feedback.
You can do this by roughly following the Percent of Total example, along with some pre-processing of your data in a calculate transform. For example: