Top X items of y axis
See original GitHub issueThank you for an amazing product first of!
I have a question in-regards to getting the Top 10 of Y-axis. Have not found a good example of how to deal with this.
My Question is: Is there preferred way to sort an aggregated y-axis and display the Top X items by y?
Below is two examples:
- Original data, that works to take 10 items
- Transformed data in pandas but with same types just different values, but does not sort the items
- My current way of making pandas handle the sorting of top 10 items for me.
codes = ['38-10-01', '32-49-01', '38-30-01', '25-20-01', '25-25-02']
percent = [0.006995515695067265,
0.002466367713004484,
0.0016591928251121076,
0.0016143497757847534,
0.001569506726457399]
df = pd.DataFrame({'codes':codes, 'percentage': percent}, columns=['codes', 'percentage'])
alt.Chart(
df,
).mark_bar().encode(
x=alt.X('codes:N', sort=alt.EncodingSortField(field="codes", op="count", order='ascending')),
y=alt.Y('percentage'),
tooltip='percentage'
).transform_window(
rank='rank(percentage)',
sort=[alt.SortField('percentage', order='descending')]
).transform_filter(
(alt.datum.rank < 10)
)
This above example works, but after I make some transformation to the data.
This below is a example output. But I can’t make it work to get the Top 10 items of something.
new_codes = ['05-41-04', '12-13-03', '12-15-01', '20-00-00', '21-27-02']
new_percentages = [0.5, 0.25, 0.0, 0.0, 0.0]
df2 = pd.DataFrame({'codes': new_codes, 'percentage': new_percentages}, columns=['codes', 'percentage'])
alt.Chart(
df2,
).mark_bar().encode(
x=alt.X('codes:N', sort=alt.EncodingSortField(field="codes", op="count", order='ascending')),
y=alt.Y('percentage'),
tooltip='percentage'
).transform_window(
rank='rank(percentage)',
sort=[alt.SortField('percentage', order='descending')]
).transform_filter(
(alt.datum.rank < 10)
)
My solution currently
nr = 15
top = data.sort_values(by='percentage', ascending=False).head(nr)
alt.Chart(
top,
title='Top {} Probablity of getting Mel Code of a Flight'.format(nr)
).mark_bar().encode(
x=alt.X('mel_code_3_lvl:N', sort=alt.EncodingSortField(field="percentage", op="sum", order='descending')),
y=alt.Y('percentage'),
)
My Question again: Is there preferred way to sort an aggregated y-axis and display the Top X items by y?
Thank you again!
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (8 by maintainers)
Top Results From Across the Web
X-Axix and Y-Axis Elements - Logi Analytics
The X-Axis and Y-Axis elements are children of the Chart Canvas and can be used to control a variety of visual elements in...
Read more >Axis - Concepts - Handbook - Apache ECharts
The x-axis is usually used to declare the number of categories which was also called the aspects of observing the data: "Sales Time",...
Read more >Chart Elements
On XY and bubble charts, both the X and Y axes are value axes. The X axis is horizontal on most charts (except...
Read more >Move x axis to top of graph and flip y axis geom_tile
You can set limits = rev to reverse a discrete y-axis. For the x-axis, position = "top" was working, but you had axis.text.x.top...
Read more >Customize X-axis and Y-axis properties - Power BI
In this article. Prerequisites; Add a new visualization; Customize the X-axis; Customize the Y-axis; Customizing visualizations with dual Y axes ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi there,
If you don’t need access to the non-aggregated data and you only want the top ten records, then I like your third approach; letting Pandas handle the aggregation and selection.
On a related note, sometimes when you want to cross filter charts with Altair’s interactions, you need access to the full set of data from within the spec. This is because the aggregations and top records may be different depending on selections made by clicking/dragging etc. Aside from that, I like to use Pandas when possible and pass only what I need to Altair.
Take care, Al
@Alcampopiano Thank you!
Ah that makes sense.
@Alcampopiano are you part of the team? I have tried to search for a solution which involves 1. where you aggregate y-axis but then sorting of the Top 10 of the aggregated values only.