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.

Asked By: Tanmay Mittal

||

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. !!

Answered By: Suraj Nagare
  1. 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.

  2. 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.

  3. 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)

enter image description here

Answered By: LazyClown
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.