Building a simple barchart - How to sort the values similar to a papreto chart (descending)?

Hello everyone!

I am new to programming and I started using python a few weeks ago in order to do some basic data analysis.

I started using bokeh to create some boxplots which prooved to be easy with nice results and then I moved on to bar charts.

For the bar chart the data I use is a DataFrame of 594 rows and 26 columns named df_rolling

Using the columns ‘SCRAP’ and ‘MATERIAL GROUP DESC’ I want to visualize the sum of ‘SCRAP’ per ‘MATERIAL GROUP DESC’

So I do following:

chart = Bar(df_rolling,values=‘SCRAP’,label=‘MATERIAL GROUP DESC’,agg=‘sum’,width=1250)

show(chart)

which gives me almost the result that I want. The problem is that I can’t find a way to sort it from higher to lower values of ‘SCRAP’.

For example with pandas I would do the following:

roll_chart = pd.pivot_table(df_rolling,values=[‘SCRAP’], index=‘MATERIAL GROUP DESC’, aggfunc=‘sum’)

roll_chart = roll_chart.sort_values(by=‘SCRAP’,ascending=False)

chart = roll_chart.plot(kind=‘bar’)

And that would give me the desired result.

So, how can I do the same thing with bokeh?

I read all relating threads but still can’t figure it out. Please forgive my lack of experience.

Thanking you in advance for your imput,

PZ

Update:

Following this example http://bokeh.pydata.org/en/0.10.0/docs/gallery/stacked_bar_chart.html , I did the following:

*df_chart1 = pd.pivot_table(df_rolling,values=[‘SCRAP’],*index=‘MATERIAL GROUP DESC’,aggfunc=‘sum’)

*df_chart1 = df_chart1.sort_values(by=‘SCRAP’,*ascending=False)[df_chart1.SCRAP>0]

material = df_chart1.index.values.tolist()

scrap = df_chart1[‘SCRAP’].astype(float).values

chart1 = Bar(scrap,material)

show(chart1)

And I get this:

  • File “”, line 1, in *

  • runfile(‘C:/Users/pzarkadas/Documents/SHORT_TERM/mypython/DMSY_Analysis.py’, wdir=‘C:/Users/pzarkadas/Documents/SHORT_TERM/mypython’)*

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”, line 699, in runfile*

  • execfile(filename, namespace)*

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”, line 88, in execfile*

  • exec(compile(open(filename, ‘rb’).read(), filename, ‘exec’), namespace)*

  • File “C:/Users/pzarkadas/Documents/SHORT_TERM/mypython/DMSY_Analysis.py”, line 75, in *

  • chart1 = Bar(scrap,material)*

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts\builder\bar_builder.py”, line 99, in Bar*

_ return create_and_build(BarBuilder, data, **kw)_

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 59, in create_and_build*

_ builder = builder_class(*data, **builder_kws)_

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 176, in init*

  • self._setup_attrs(data, kws)*

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 207, in _setup_attrs*

  • self.attributes[attr_name].setup(data=source, columns=attr)*

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 109, in setup*

  • self.set_columns(columns)*

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 102, in set_columns*

  • self._setup_default()*

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 70, in _setup_default*

  • self.default = next(self._setup_iterable())*

StopIteration

What am I doing wrong here ?

PZ

···

On Sunday, 17 January 2016 16:17:30 UTC+2, Panagiotis Zarkadas wrote:

Hello everyone!

I am new to programming and I started using python a few weeks ago in order to do some basic data analysis.

I started using bokeh to create some boxplots which prooved to be easy with nice results and then I moved on to bar charts.

For the bar chart the data I use is a DataFrame of 594 rows and 26 columns named df_rolling

Using the columns ‘SCRAP’ and ‘MATERIAL GROUP DESC’ I want to visualize the sum of ‘SCRAP’ per ‘MATERIAL GROUP DESC’

So I do following:

chart = Bar(df_rolling,values=‘SCRAP’,label=‘MATERIAL GROUP DESC’,agg=‘sum’,width=1250)

show(chart)

which gives me almost the result that I want. The problem is that I can’t find a way to sort it from higher to lower values of ‘SCRAP’.

For example with pandas I would do the following:

roll_chart = pd.pivot_table(df_rolling,values=[‘SCRAP’], index=‘MATERIAL GROUP DESC’, aggfunc=‘sum’)

roll_chart = roll_chart.sort_values(by=‘SCRAP’,ascending=False)

chart = roll_chart.plot(kind=‘bar’)

And that would give me the desired result.

So, how can I do the same thing with bokeh?

I read all relating threads but still can’t figure it out. Please forgive my lack of experience.

Thanking you in advance for your imput,

PZ

Hi,

What version of bokeh are you using? You can check it by running :

python -c “import bokeh;bokeh.version

···

From what I see you are looking at an outdated (0.10) version and the Charts interface in new one (0.11) have been modified. You can see the new version of the gallery example you’ve linked here: http://bokeh.pydata.org/en/latest/docs/gallery/stacked_bar_chart.html

Best

Fabio

On Mon, Jan 18, 2016 at 1:19 PM, Panagiotis Zarkadas [email protected] wrote:

Update:

Following this example http://bokeh.pydata.org/en/0.10.0/docs/gallery/stacked_bar_chart.html , I did the following:

*df_chart1 = pd.pivot_table(df_rolling,values=[‘SCRAP’],*index=‘MATERIAL GROUP DESC’,aggfunc=‘sum’)

*df_chart1 = df_chart1.sort_values(by=‘SCRAP’,*ascending=False)[df_chart1.SCRAP>0]

material = df_chart1.index.values.tolist()

scrap = df_chart1[‘SCRAP’].astype(float).values

chart1 = Bar(scrap,material)

show(chart1)

And I get this:

  • File “”, line 1, in *
  • runfile(‘C:/Users/pzarkadas/Documents/SHORT_TERM/mypython/DMSY_Analysis.py’, wdir=‘C:/Users/pzarkadas/Documents/SHORT_TERM/mypython’)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”, line 699, in runfile*
  • execfile(filename, namespace)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”, line 88, in execfile*
  • exec(compile(open(filename, ‘rb’).read(), filename, ‘exec’), namespace)*
  • File “C:/Users/pzarkadas/Documents/SHORT_TERM/mypython/DMSY_Analysis.py”, line 75, in *
  • chart1 = Bar(scrap,material)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts\builder\bar_builder.py”, line 99, in Bar*

_ return create_and_build(BarBuilder, data, **kw)_

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 59, in create_and_build*

_ builder = builder_class(*data, **builder_kws)_

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 176, in init*
  • self._setup_attrs(data, kws)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 207, in _setup_attrs*
  • self.attributes[attr_name].setup(data=source, columns=attr)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 109, in setup*
  • self.set_columns(columns)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 102, in set_columns*
  • self._setup_default()*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 70, in _setup_default*
  • self.default = next(self._setup_iterable())*

StopIteration

What am I doing wrong here ?

PZ

On Sunday, 17 January 2016 16:17:30 UTC+2, Panagiotis Zarkadas wrote:

Hello everyone!

I am new to programming and I started using python a few weeks ago in order to do some basic data analysis.

I started using bokeh to create some boxplots which prooved to be easy with nice results and then I moved on to bar charts.

For the bar chart the data I use is a DataFrame of 594 rows and 26 columns named df_rolling

Using the columns ‘SCRAP’ and ‘MATERIAL GROUP DESC’ I want to visualize the sum of ‘SCRAP’ per ‘MATERIAL GROUP DESC’

So I do following:

chart = Bar(df_rolling,values=‘SCRAP’,label=‘MATERIAL GROUP DESC’,agg=‘sum’,width=1250)

show(chart)

which gives me almost the result that I want. The problem is that I can’t find a way to sort it from higher to lower values of ‘SCRAP’.

For example with pandas I would do the following:

roll_chart = pd.pivot_table(df_rolling,values=[‘SCRAP’], index=‘MATERIAL GROUP DESC’, aggfunc=‘sum’)

roll_chart = roll_chart.sort_values(by=‘SCRAP’,ascending=False)

chart = roll_chart.plot(kind=‘bar’)

And that would give me the desired result.

So, how can I do the same thing with bokeh?

I read all relating threads but still can’t figure it out. Please forgive my lack of experience.

Thanking you in advance for your imput,

PZ

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/9b570804-b813-434d-b009-a8c3a5deacf6%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Fabio Pliger

Senior Software Engineer, Bokeh

Many thanks for your interest.I realized I was using bokeh 0.10 and I updated to bokeh 0.11.
Then I 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)

···

On Thursday, 21 January 2016 19:04:14 UTC+2, Fabio Pliger wrote:

Hi,

What version of bokeh are you using? You can check it by running :

python -c “import bokeh;bokeh.version

From what I see you are looking at an outdated (0.10) version and the Charts interface in new one (0.11) have been modified. You can see the new version of the gallery example you’ve linked here: http://bokeh.pydata.org/en/latest/docs/gallery/stacked_bar_chart.html

Best

Fabio

On Mon, Jan 18, 2016 at 1:19 PM, Panagiotis Zarkadas [email protected] wrote:

Update:

Following this example http://bokeh.pydata.org/en/0.10.0/docs/gallery/stacked_bar_chart.html , I did the following:

*df_chart1 = pd.pivot_table(df_rolling,values=[‘SCRAP’],*index=‘MATERIAL GROUP DESC’,aggfunc=‘sum’)

*df_chart1 = df_chart1.sort_values(by=‘SCRAP’,*ascending=False)[df_chart1.SCRAP>0]

material = df_chart1.index.values.tolist()

scrap = df_chart1[‘SCRAP’].astype(float).values

chart1 = Bar(scrap,material)

show(chart1)

And I get this:

  • File “”, line 1, in *
  • runfile(‘C:/Users/pzarkadas/Documents/SHORT_TERM/mypython/DMSY_Analysis.py’, wdir=‘C:/Users/pzarkadas/Documents/SHORT_TERM/mypython’)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”, line 699, in runfile*
  • execfile(filename, namespace)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”, line 88, in execfile*
  • exec(compile(open(filename, ‘rb’).read(), filename, ‘exec’), namespace)*
  • File “C:/Users/pzarkadas/Documents/SHORT_TERM/mypython/DMSY_Analysis.py”, line 75, in *
  • chart1 = Bar(scrap,material)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts\builder\bar_builder.py”, line 99, in Bar*

_ return create_and_build(BarBuilder, data, **kw)_

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 59, in create_and_build*

_ builder = builder_class(*data, **builder_kws)_

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 176, in init*
  • self._setup_attrs(data, kws)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 207, in _setup_attrs*
  • self.attributes[attr_name].setup(data=source, columns=attr)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 109, in setup*
  • self.set_columns(columns)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 102, in set_columns*
  • self._setup_default()*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 70, in _setup_default*
  • self.default = next(self._setup_iterable())*

StopIteration

What am I doing wrong here ?

PZ

On Sunday, 17 January 2016 16:17:30 UTC+2, Panagiotis Zarkadas wrote:

Hello everyone!

I am new to programming and I started using python a few weeks ago in order to do some basic data analysis.

I started using bokeh to create some boxplots which prooved to be easy with nice results and then I moved on to bar charts.

For the bar chart the data I use is a DataFrame of 594 rows and 26 columns named df_rolling

Using the columns ‘SCRAP’ and ‘MATERIAL GROUP DESC’ I want to visualize the sum of ‘SCRAP’ per ‘MATERIAL GROUP DESC’

So I do following:

chart = Bar(df_rolling,values=‘SCRAP’,label=‘MATERIAL GROUP DESC’,agg=‘sum’,width=1250)

show(chart)

which gives me almost the result that I want. The problem is that I can’t find a way to sort it from higher to lower values of ‘SCRAP’.

For example with pandas I would do the following:

roll_chart = pd.pivot_table(df_rolling,values=[‘SCRAP’], index=‘MATERIAL GROUP DESC’, aggfunc=‘sum’)

roll_chart = roll_chart.sort_values(by=‘SCRAP’,ascending=False)

chart = roll_chart.plot(kind=‘bar’)

And that would give me the desired result.

So, how can I do the same thing with bokeh?

I read all relating threads but still can’t figure it out. Please forgive my lack of experience.

Thanking you in advance for your imput,

PZ

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/9b570804-b813-434d-b009-a8c3a5deacf6%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.


Fabio Pliger

Senior Software Engineer, Bokeh

Great! Glad it helped.

···

On Fri, Jan 22, 2016 at 5:56 AM, Panagiotis Zarkadas [email protected] wrote:

Many thanks for your interest.I realized I was using bokeh 0.10 and I updated to bokeh 0.11.
Then I 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)

On Thursday, 21 January 2016 19:04:14 UTC+2, Fabio Pliger wrote:

Hi,

What version of bokeh are you using? You can check it by running :

python -c “import bokeh;bokeh.version

From what I see you are looking at an outdated (0.10) version and the Charts interface in new one (0.11) have been modified. You can see the new version of the gallery example you’ve linked here: http://bokeh.pydata.org/en/latest/docs/gallery/stacked_bar_chart.html

Best

Fabio

On Mon, Jan 18, 2016 at 1:19 PM, Panagiotis Zarkadas [email protected] wrote:

Update:

Following this example http://bokeh.pydata.org/en/0.10.0/docs/gallery/stacked_bar_chart.html , I did the following:

*df_chart1 = pd.pivot_table(df_rolling,values=[‘SCRAP’],*index=‘MATERIAL GROUP DESC’,aggfunc=‘sum’)

*df_chart1 = df_chart1.sort_values(by=‘SCRAP’,*ascending=False)[df_chart1.SCRAP>0]

material = df_chart1.index.values.tolist()

scrap = df_chart1[‘SCRAP’].astype(float).values

chart1 = Bar(scrap,material)

show(chart1)

And I get this:

  • File “”, line 1, in *
  • runfile(‘C:/Users/pzarkadas/Documents/SHORT_TERM/mypython/DMSY_Analysis.py’, wdir=‘C:/Users/pzarkadas/Documents/SHORT_TERM/mypython’)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”, line 699, in runfile*
  • execfile(filename, namespace)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”, line 88, in execfile*
  • exec(compile(open(filename, ‘rb’).read(), filename, ‘exec’), namespace)*
  • File “C:/Users/pzarkadas/Documents/SHORT_TERM/mypython/DMSY_Analysis.py”, line 75, in *
  • chart1 = Bar(scrap,material)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts\builder\bar_builder.py”, line 99, in Bar*

_ return create_and_build(BarBuilder, data, **kw)_

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 59, in create_and_build*

_ builder = builder_class(*data, **builder_kws)_

  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 176, in init*
  • self._setup_attrs(data, kws)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_builder.py”, line 207, in _setup_attrs*
  • self.attributes[attr_name].setup(data=source, columns=attr)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 109, in setup*
  • self.set_columns(columns)*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 102, in set_columns*
  • self._setup_default()*
  • File “C:\Users\pzarkadas\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\charts_attributes.py”, line 70, in _setup_default*
  • self.default = next(self._setup_iterable())*

StopIteration

What am I doing wrong here ?

PZ

On Sunday, 17 January 2016 16:17:30 UTC+2, Panagiotis Zarkadas wrote:

Hello everyone!

I am new to programming and I started using python a few weeks ago in order to do some basic data analysis.

I started using bokeh to create some boxplots which prooved to be easy with nice results and then I moved on to bar charts.

For the bar chart the data I use is a DataFrame of 594 rows and 26 columns named df_rolling

Using the columns ‘SCRAP’ and ‘MATERIAL GROUP DESC’ I want to visualize the sum of ‘SCRAP’ per ‘MATERIAL GROUP DESC’

So I do following:

chart = Bar(df_rolling,values=‘SCRAP’,label=‘MATERIAL GROUP DESC’,agg=‘sum’,width=1250)

show(chart)

which gives me almost the result that I want. The problem is that I can’t find a way to sort it from higher to lower values of ‘SCRAP’.

For example with pandas I would do the following:

roll_chart = pd.pivot_table(df_rolling,values=[‘SCRAP’], index=‘MATERIAL GROUP DESC’, aggfunc=‘sum’)

roll_chart = roll_chart.sort_values(by=‘SCRAP’,ascending=False)

chart = roll_chart.plot(kind=‘bar’)

And that would give me the desired result.

So, how can I do the same thing with bokeh?

I read all relating threads but still can’t figure it out. Please forgive my lack of experience.

Thanking you in advance for your imput,

PZ

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/9b570804-b813-434d-b009-a8c3a5deacf6%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.


Fabio Pliger

Senior Software Engineer, Bokeh

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/b0ccc0d7-a111-4646-920a-32cb37ed5d43%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Fabio Pliger

Senior Software Engineer, Bokeh