Panagiotis Zarkadas
Panagiotis Zarkadas

Reputation: 25

Building a sorted Bar-chart with pandas & bokeh

As part of some basic data analysis I am trying to use bokeh to display some charts in HTML format. With the following code I created a very simple bar chart using a pandas data frame with 26 columns and 594 rows. My question is: How can I sort it descending by scrap value? Right now the graph is sorted by alphabetical order of the 'MATERIAL GROUP' column. I tried almost everything but I cant figure it out...

import pandas as pd
from bokeh.charts import Bar, output_file, show
from bokeh.charts.attributes import cat
chart1 = Bar(df,values='SCRAP',
             label=cat(columns='MATERIAL GROUP',sort=True),
             plot_width=1250,agg='sum')      
output_file('DMSY_November_2015.html')
show(chart1)

Upvotes: 1

Views: 2649

Answers (1)

Panagiotis Zarkadas
Panagiotis Zarkadas

Reputation: 25

Found a solution by creating a sorted DataFrame and using it to plot the barchart:

import pandas as pd
from bokeh.charts import Bar, output_file, show
from bokeh.charts.attributes import cat

#Creating the DataFrame for chart1: Filtering/Grouping/Sorting
df_chart1 = df_rolling[df_rolling.SCRAP>0]
df_chart1 = df_chart1.groupby(by='MATERIAL GROUP',as_index=False)    ['SCRAP'].sum()
df_chart1 = df_chart1.sort_values(by='SCRAP',ascending=False)

#Plotting chart1
chart1 = Bar(df_chart1,values='SCRAP',
             label=cat(columns='MATERIAL GROUP',sort=False),
             plot_width=1250)

output_file('DMSY_November_2015.html')
show(chart1)

Upvotes: 1

Related Questions