Plotting 1:8 attributes on altair
Question:
How to plot graph for 1:8 attributes using altair? Here is the link to the dataset. I want to plot an interactive mark_point() graph for various attributes like fresh, frozen, etc, considering the region and channel as filters. The x-axis should have attributes, and the y-axis will have the count.
The interaction is based on region and channel to show the values of user buying from distributor.
I am not able to plot the 8 attributes on a single graph. I tried transforming the df into a dictionary and then using the same to plot the graph. but unsuccessful.
Answers:
I guess this is the limitation of altair. you cannot club all the attributes together. You will need matplotlib for doing so. Hope this helps. !!
-
You can create two filters: one for the region and one for the channel, and combine them with an AND operator to filter the data.
This gives you a subset of the dataframe which satisfies your filters.
-
After the filter, you can apply transform_fold
by passing the column names of the attributes. You can think of it as a new dataframe with two columns: key
will contain the column names, and value
will have the value corresponding to it.
-
Now, you can simply use the key
as your X-axis variable and value
as your Y-axis variable.
reg_dropdown = alt.binding_select(options=list(df.Region.unique()))
reg_selection = alt.selection_single(fields=['Region'], bind=reg_dropdown, name='Select Region')
chn_dropdown = alt.binding_select(options=list(df.Channel.unique()))
chn_selection = alt.selection_single(fields=['Channel'], bind=chn_dropdown, name='Select Channel')
alt.Chart(df).transform_fold(
['Fresh', 'Milk', 'Grocery','Frozen', 'Detergents_Paper','Delicassen'],
).mark_point().encode(
x='key:N',
y='value:Q',
detail ='key:N',
).add_selection(
reg_selection, chn_selection
).transform_filter(
reg_selection & chn_selection
).properties(width=300)
How to plot graph for 1:8 attributes using altair? Here is the link to the dataset. I want to plot an interactive mark_point() graph for various attributes like fresh, frozen, etc, considering the region and channel as filters. The x-axis should have attributes, and the y-axis will have the count.
The interaction is based on region and channel to show the values of user buying from distributor.
I am not able to plot the 8 attributes on a single graph. I tried transforming the df into a dictionary and then using the same to plot the graph. but unsuccessful.
I guess this is the limitation of altair. you cannot club all the attributes together. You will need matplotlib for doing so. Hope this helps. !!
-
You can create two filters: one for the region and one for the channel, and combine them with an AND operator to filter the data.
This gives you a subset of the dataframe which satisfies your filters. -
After the filter, you can apply
transform_fold
by passing the column names of the attributes. You can think of it as a new dataframe with two columns:key
will contain the column names, andvalue
will have the value corresponding to it. -
Now, you can simply use the
key
as your X-axis variable andvalue
as your Y-axis variable.
reg_dropdown = alt.binding_select(options=list(df.Region.unique()))
reg_selection = alt.selection_single(fields=['Region'], bind=reg_dropdown, name='Select Region')
chn_dropdown = alt.binding_select(options=list(df.Channel.unique()))
chn_selection = alt.selection_single(fields=['Channel'], bind=chn_dropdown, name='Select Channel')
alt.Chart(df).transform_fold(
['Fresh', 'Milk', 'Grocery','Frozen', 'Detergents_Paper','Delicassen'],
).mark_point().encode(
x='key:N',
y='value:Q',
detail ='key:N',
).add_selection(
reg_selection, chn_selection
).transform_filter(
reg_selection & chn_selection
).properties(width=300)