Getting bar plot from dataframe

Question:

I have a dataframe df like below:
enter image description here

Now I like the bar plot of the first column i.e. year against the columns A, B, C, D.

from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.palettes import Spectral6
from bokeh.models import ColumnDataSource
from bokeh.transform import factor_cmap
output_file("bar_charts.html")

There would be three different bar plots, considering each row (Year vs numerical columns).

What parameters to put in these two lines of code so that i can accomplish it:

p = figure(x_range=??, height=350, title="Count")    
p.vbar(x=?, top=?, width=0.9)
show(p)
Asked By: Tranquil Oshan

||

Answers:

This is nearly a 1 by 1 copy of the bar_doged example of the bokeh gallery.

import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show, output_notebook
from bokeh.transform import dodge
output_notebook()

df = pd.DataFrame({
    'A':[12,5,8],
    'B':[18,12,8],
    'C':[6,18,12],
    'D':[4,5,2],
    'Year':['2000', '2005', '2010']
})

source = ColumnDataSource(df)

p = figure(x_range=df['Year'], title="Bar plot",height=350)

p.vbar(x=dodge('Year', -0.3, range=p.x_range), top='A', source=source,
       width=0.19, color="#c9d9d3", legend_label="A")
p.vbar(x=dodge('Year',  -0.1,  range=p.x_range), top='B', source=source,
       width=0.19, color="#718dbf", legend_label="B")
p.vbar(x=dodge('Year',  0.1, range=p.x_range), top='C', source=source,
       width=0.19, color="#e84d60", legend_label="C")
p.vbar(x=dodge('Year',  0.3, range=p.x_range), top='D', source=source,
       width=0.19, color="#555555", legend_label="D")

p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.orientation = "horizontal"

show(p)

simple bar plot with bokeh

Answered By: mosc9575
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.