How can I order or filter hierarchically this plot from Altair?

364 views Asked by At

I've looking for info on how to order or filter hierarchically this viz on Altair without luck. I mean, order the bar, from the value with more counts to the one with less. Also, filter the first ten so I get a better graph. Do you guys have any idea of how can I get that outcome?

Here's my code:

   alt.Chart(df).mark_bar().encode(
    x = alt.X('count()'),
    y = alt.Y('proveedor_nombre:N')
)

And here's my outcome (I cut it since it's too big for this):

enter image description here

I thank you guys in advance.

1

There are 1 answers

2
jakevdp On BEST ANSWER

You can do this following the Top K Items example in the Altair documentation, with a Window Transform followed by a Filter Transform:

alt.Chart(df).transform_aggregate(
  count='count()',
  groupby=['proveedor_nombre']
).transform_window(
  rank='rank(count)',
  sort=[alt.SortField('count', order='descending')]
).transform_filter(
  alt.datum.rank <= 10
).mark_bar().encode(
  x = alt.X('count:Q'),
  y = alt.Y('proveedor_nombre:N', sort='-x')
)