10

I would like to plot a whole pandas DataFrame with Bokeh. I.e., I am looking for a Bokeh equivalent of the third line:

import pandas as pd
income_df = pd.read_csv("income_2013_dollars.csv", sep='\t', thousands=',')
income_df.plot(x="year")

Is there currently a way to do that, or do I have to pass each y-value separately?

Anarcho-Chossid
  • 2,210
  • 4
  • 27
  • 44
  • Which part are you stuck at? You've not stated what type of plot you want, also it's easy to get the values either as an array or list, `df['y_col'].values` this may or may not be necessary, otherwise `df['y_col'].values.to_list()` will you give you a list – EdChum Feb 22 '15 at 15:46

2 Answers2

9

Note from Bokeh project maintainers: This answer refers to an obsolete and deprecated API was long since removed from Bokeh. For information about creating bar charts with modern and fully supported Bokeh APIs, see other Questions/Answers.


You may find the charts examples useful:

https://github.com/bokeh/bokeh/tree/master/examples/charts

If you wanted a bar chart it would be:

from bokeh.charts import Bar
Bar(income_df, notebook=True).show()  # assuming the index is corretly set on your df

You may want a Line or TimeSeries which work similarly - just checkout the examples for more details and more configuration - like adding titles, labels etc.

Note that you can use other output methods - notebook, file, or server. See the documentation here: http://docs.bokeh.org/en/latest/docs/user_guide/charts.html#generic-arguments

Update: (sorry for the confusion on how to display the output). An alternative way of specifying the display type of the chart is to use the methods output_notebook(), output_file("file.html"), output_server() and then use the show method. For example

from bokeh.charts import Bar
from bokeh.plotting import output_notebook, show
output_notebook()
bar = Bar(income_df)
show(bar)

However, you cannot do the following

from bokeh.charts import Bar
from bokeh.plotting import output_notebook
output_notebook()
Bar(income_df).show()  # WILL GIVE YOU AN ERROR

The two show methods are different.

bigreddot
  • 33,642
  • 5
  • 69
  • 122
birdsarah
  • 1,165
  • 8
  • 20
  • 1
    Sarah reply is very accurate and provide useful details already. Worth mentioning that the explicit support for output_notebook(), output_file(), output_server() have been added in > 0.8 releases (with many other improvements) with the intention to reduce the differences between Charts interface and other Bokeh lower level APIs. – Fabio Pliger Feb 24 '15 at 20:56
  • The link is outdated. – Soerendip May 18 '18 at 19:58
  • 1
    Bokeh charts have been deprecated, and there is no documentation on the package. I think [Bokeh plotting](https://bokeh.pydata.org/en/latest/docs/reference/plotting.html#bokeh-plotting) should be used now. – 9769953 Nov 06 '18 at 09:55
1

See this User's Guide Section for modern information on creating Bar charts with Pandas:

https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#pandas

For example:

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.sampledata.autompg import autompg as df
from bokeh.transform import factor_cmap
        
df.cyl = df.cyl.astype(str)
group = df.groupby('cyl')
    
source = ColumnDataSource(group)
    
cyl_cmap = factor_cmap('cyl', palette="Spectral5", factors=sorted(df.cyl.unique()))
    
p = figure(x_range=group, title="MPG by # Cylinders",
           toolbar_location=None, tools="")
    
p.vbar(x='cyl', top='mpg_mean', width=1, source=source,
       line_color=cyl_cmap, fill_color=cyl_cmap)
    
p.y_range.start = 0
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "some stuff"
p.xaxis.major_label_orientation = 1.2
p.outline_line_color = None
    
show(p)

enter image description here

bigreddot
  • 33,642
  • 5
  • 69
  • 122