Expanded sorted bar chart example with docs and a dataset
"""
-Simple Sorted Bar Chart
-============================
-This example shows a basic sorted bar chart.
+Sorted Bar Chart
+================
+This example shows a bar chart sorted by a calculated value.
"""
# category: bar charts
import altair as alt
-import pandas as pd
+from vega_datasets import data
-source = pd.DataFrame({
- 'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
- 'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
-})
+source = data.barley()
alt.Chart(source).mark_bar().encode(
- x=alt.X('a', sort=alt.EncodingSortField(field='b', op='sum', order='descending')),
- y='b'
+ x='sum(yield):Q',
+ y=alt.Y(
+ 'site:N',
+ sort=alt.EncodingSortField(
+ field="yield", # The field to use for the sort
+ op="sum", # The operation to run on the field prior to sorting
+ order="descending" # The order to sort in
+ )
+ )
)
