Search code examples
pythonpandasbokehholoviews

Bokeh plot sort bar plot by values not by index


I was going through bokeh tutorial from their official website: https://hub.mybinder.org/user/bokeh-bokeh-notebooks-tkmnntgc/notebooks/tutorial/00%20-%20Introduction%20and%20Setup.ipynb and attempted to get a similar plot for crime rates in USA cities from wikipedia data of 2009. But, I stuck with some problems.

First of all I searched related questions and they quite did not meet my problems.

Related questions: Choosing order of bars in Bokeh bar chart

Questions
1. How go get values at top of the vertical bars?
2. How to get the values sorted by y-axis values instead of x-label indices?

enter image description here

The code is given here:

import pandas as pd
import requests
from bokeh.io import output_notebook, show
output_notebook()

from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure
from bokeh.transform import factor_cmap

url = "https://en.wikipedia.org/wiki/List_of_United_States_cities_by_crime_rate"
response = requests.get(url)

df = pd.read_html(response.content)[1]
df = df.iloc[2:]
df.columns = ['State', 'City', 'Population', 'Total_violent',
              'Murder', 'Rape', 'Robbery', 'Assault',
              'Total_property', 'Burglary', 'Larceny', 'Motor_theft',
              'Arson']

df.index = df.index - 2  # Reset index numbers
df.index = df.City


# rename index
df.index.name = 'index'

# Change data type and sort
df['Murder'] = df['Murder'].apply(pd.to_numeric, errors='coerce')
df = df.sort_values(by='Murder', ascending=True)

# first and last 10
df = pd.concat([df.head(10), df.tail(10)])
df.index = range(20)

# create low_high column
df['low_high'] = ['low']*10 + ['high']*10

# create group of two x-axes
group = df.groupby(by=['low_high', 'City'])

# from group get source
source = ColumnDataSource(group)

# from group get figure
p = figure(plot_width=800, plot_height=300, 
           title="Murder in US city per 100,000 people in 2009",
           x_range=group, 
           toolbar_location=None, 
           tools="")

# plot labels
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "Cities"
p.yaxis.axis_label = "Murder"
p.xaxis.major_label_orientation = 1.2

# index_cmap will be used for fill_color
index_cmap = factor_cmap('low_high_City', 
                         palette=['#2b83ba', '#abdda4', '#ffffbf', '#fdae61', '#d7191c'], 
                         factors=df['low_high'].unique(), 
                         end=1)

p.vbar(x='low_high_City', 
       top='Murder_mean', 
       width=1, 
       source=source,
       line_color="white", 
       fill_color=index_cmap, 
       hover_line_color="darkgrey", 
       hover_fill_color=index_cmap)


hover_cols = ['Murder','Rape','Robbery','Assault','Burglary','Larceny','Motor_theft','Arson']
for col in hover_cols:
    df[col] = df[col].apply(pd.to_numeric, errors='coerce')


tooltips = [(c,"@"+c+"_mean") for c in hover_cols]
tooltips = [("City","@City")] + tooltips
p.add_tools(HoverTool(tooltips=tooltips))

show(p)

Solution

  • The order on the axis is determined entirely by the order of factors in the plot range:

    enter image description here

    If you want the factors to appear on the axis in a different order, you must simply sort this list to whatever order you want, and re-assign to the range:

    p.x_range.factors = sorted_factors
    

    For your case, here is one way to do it (I'm not a Pandas expert, there are probably much better ways):

    desc = group.describe()
    
    low_cities = desc.Murder['mean']['low'].index
    low_sorted = [('low', city) for city in sorted(low_cities, key=lambda x: desc.Murder['mean'][('low',)][x])]
    
    high_cities = desc.Murder['mean']['high'].index
    high_sorted = [('high', city) for city in sorted(high_cities, key=lambda x: desc.Murder['mean'][('high',)][x])]
    

    enter image description here