柱状图、堆叠图、直方图
- 单系列柱状图
- 多系列柱状图
- 堆叠图
- 直方图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
from bokeh.io import output_notebook
output_notebook()
from bokeh.plotting import figure,show
from bokeh.models import ColumnDataSource
单系列柱状图
p = figure(plot_width=600, plot_height=400)
p.vbar(x=[1,2,3], width=0.5, bottom=0, top=[1.2, 2.5, 3.7],
line_width=1, line_alpha=0.8, line_color='black', line_dash=[8,4],
fill_color='red', fill_alpha=0.6
)
show(p)
绘制横向柱状图:hbar
df = pd.DataFrame({'value': np.random.randn(100)*10,
'color': np.random.choice(['red', 'blue', 'green'],100)})
p = figure(plot_width=600, plot_height=400)
p.hbar(y=df.index, height=0.5, left=0, right=df['value'],
color = df['color'] ,
fill_alpha=0.6
)
show(p)
单系列柱状图 - 分类设置标签
from bokeh.palettes import Spectral6
from bokeh.transform import factor_cmap
fruits=['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts=[5, 3, 4, 2, 4, 6]
source=ColumnDataSource(data=dict(fruits=fruits, counts=counts))
colors=['salmon', 'olive', 'darkred','goldenrod', 'skyblue','orange']
p=figure(x_range=fruits, y_range=(0,9), plot_height=350, title='Fruit Counts',tools='')
p.vbar(x='fruits', top='counts', source=source,
width=0.9, alpha=0.8,
color=factor_cmap('fruits', palette=Spectral6, factors=fruits),
legend="fruits"
)
p.xgrid.grid_line_color=None
p.legend.orientation='horizontal'
p.legend.location='top_center'
show(p)
多系列柱状图
from bokeh.transform import dodge
from bokeh.core.properties import value
df=pd.DataFrame({'2015': [2, 1, 4, 3, 2, 4],
'2016': [5, 3, 3, 2, 4, 6],
'2017': [3, 2, 4, 4, 5, 3]},
index = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries'])
df.head()
fruits=df.index.tolist()
years=df.columns.tolist()
data={'index': fruits}
for year in years:
data[year] = df[year].tolist()
source=ColumnDataSource(data=data)
p=figure(x_range=fruits, y_range=(0,7), plot_width=350, title="Fruit Count by Year", tools='')
p.vbar(x=dodge('index', -0.25, range=p.x_range), top='2015', width=0.2, source=source, color='#c9d9d3', legend=value('2015'))
p.vbar(x=dodge('index', 0.0, range=p.x_range), top='2016', width=0.2, source=source, color='#718dbf', legend=value('2016'))
p.vbar(x=dodge('index', 0.25, range=p.x_range), top='2017', width=0.2, source=source, color='#e84d60', legend=value('2017'))
p.xgrid.grid_line_color=None
p.legend.orientation='horizontal'
p.legend.location='top_center'
show(p)
堆叠图
from bokeh.core.properties import value
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']
colors=['red', 'blue', 'orange']
data={'fruits': fruits,
'2015': [2, 1, 4, 3, 2, 4],
'2016': [5, 3, 3, 2, 4, 6],
'2017': [3, 2, 4, 4, 5, 3]
}
source=ColumnDataSource(data=data)
p=figure(x_range=fruits, plot_height=450, title="Fruit Count by Year", tools='')
renderers=p.vbar_stack(years,
x='fruits',
source=source,
width=0.9, color=colors,
legend=[value(x) for x in years],
name=years
)
p.xgrid.grid_line_color = None
p.axis.minor_tick_line_color=None
p.outline_line_color=None
p.legend.location='top_center'
p.legend.orientation='horizontal'
show(p)
堆叠图 - 横向
from bokeh.palettes import GnBu3, OrRd3
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']
exports={'fruits': fruits,
'2015': [2,1,4,3,2,4],
'2016': [5,3,4,2,4,6],
'2017': [3,2,4,4,5,3]
}
imports={'fruits': fruits,
'2015': [-1,0,-1,-3,-2,-1],
'2016': [-2,-1,-3,-1,-2,-2],
'2017': [-1,-2,-1,0,-2,-2]
}
p=figure(y_range=fruits, plot_height=350, x_range=(-16,16), title="Fruit import/export by year")
p.hbar_stack(years, y='fruits', height=0.9, color=GnBu3, source=ColumnDataSource(exports),
legend=['%s exports' % x for x in years]
)
p.hbar_stack(years, y='fruits', height=0.9, color=GnBu3, source=ColumnDataSource(imports),
legend=['%s exports' % x for x in years]
)
p.y_range.range_padding=0.2
p.ygrid.grid_line_color=None
p.legend.location='top_left'
p.axis.minor_tick_line_color=None
p.outline_line_color=None
show(p)
直方图
df = pd.DataFrame({'value': np.random.randn(1000)*100})
df.index.name='index'
df.head()
hist, edges = np.histogram(df['value'],bins=20)
print(hist[:5])
print(edges)
p=figure(title="HIST", tools="save", background_fill_color="#E8DDCB")
p.quad(top=hist, bottom=0, left=edges[:-1],right=edges[1:],
fill_color="#036564", line_color="#033649"
)
show(p)
|