pip install pandas_bokeh
pip install pandas
import pandas as pd
import pandas_bokeh
pandas_bokeh.output_notebook()
# 也可以直接
# pd.options.plotting.backend = 'plotly'
#当然,以下也是可以的
# pd.options.plotting.backend = 'Pandas_bokeh'
# pd.options.plotting.backend = 'Holoviews'
# pd.options.plotting.backend = 'Matplotlib'
# pd.options.plotting.backend = 'Hyplot'
import numpy as np
np.random.seed(55)
df = pd.DataFrame({"宁德时代": np.random.randn(100)+0.2,
"贵州茅台": np.random.randn(100)+0.17},
index=pd.date_range('1/1/2021', periods=100))
# 默认行累加
df = df.cumsum()
# 每个值加50
df = df + 50
df.plot_bokeh(kind="line")
Figure(
id = '1003', …)
above = [],
align = 'start',
aspect_ratio = None,
aspect_scale = 1,
background = None,
background_fill_alpha = 1.0,
background_fill_color = '#ffffff',
below = [DatetimeAxis(id='1014', ...)],
border_fill_alpha = 1.0,
border_fill_color = '#ffffff',
center = [Grid(id='1017', ...), Grid(id='1021', ...), Legend(id='1065', ...)],
css_classes = [],
disabled = False,
extra_x_ranges = {},
extra_x_scales = {},
extra_y_ranges = {},
extra_y_scales = {},
frame_height = None,
frame_width = None,
height = 400,
height_policy = 'auto',
hidpi = True,
inner_height = 0,
inner_width = 0,
js_event_callbacks = {},
js_property_callbacks = {},
left = [LinearAxis(id='1018', ...)],
lod_factor = 10,
lod_interval = 300,
lod_threshold = 2000,
lod_timeout = 500,
margin = (0, 0, 0, 0),
match_aspect = False,
max_height = None,
max_width = None,
min_border = 5,
min_border_bottom = None,
min_border_left = None,
min_border_right = None,
min_border_top = None,
min_height = None,
min_width = None,
name = None,
outer_height = 0,
outer_width = 0,
outline_line_alpha = 1.0,
outline_line_cap = 'butt',
outline_line_color = '#e5e5e5',
outline_line_dash = [],
outline_line_dash_offset = 0,
outline_line_join = 'bevel',
outline_line_width = 1,
output_backend = 'webgl',
renderers = [GlyphRenderer(id='1041', ...), GlyphRenderer(id='1074', ...)],
reset_policy = 'standard',
right = [],
sizing_mode = 'fixed',
subscribed_events = [],
syncable = True,
tags = [],
title = Title(id='1004', ...),
title_location = 'above',
toolbar = Toolbar(id='1029', ...),
toolbar_location = 'right',
toolbar_sticky = True,
visible = True,
width = 600,
width_policy = 'auto',
x_range = DataRange1d(id='1006', ...),
x_scale = LinearScale(id='1010', ...),
y_range = DataRange1d(id='1008', ...),
y_scale = LinearScale(id='1012', ...))
df.plot_bokeh.line(
figsize=(800, 450),
title="宁德时代 vs 贵州茅台",
xlabel="日期",
ylabel="股票价格 [$]",
yticks=[0, 100, 200, 300, 400],
ylim=(0, 200), # y轴范围最大限制
xlim=("2021-01-01", "2021-04-01"), # x轴范围最大限制
colormap=["red", "blue"],
plot_data_points=True,
plot_data_points_size=10,
marker="asterisk",
)
Figure(
id = '3444', …)
above = [],
align = 'start',
aspect_ratio = None,
aspect_scale = 1,
background = None,
background_fill_alpha = 1.0,
background_fill_color = '#ffffff',
below = [DatetimeAxis(id='3455', ...)],
border_fill_alpha = 1.0,
border_fill_color = '#ffffff',
center = [Grid(id='3458', ...), Grid(id='3462', ...), Legend(id='3506', ...)],
css_classes = [],
disabled = False,
extra_x_ranges = {},
extra_x_scales = {},
extra_y_ranges = {},
extra_y_scales = {},
frame_height = None,
frame_width = None,
height = 450,
height_policy = 'auto',
hidpi = True,
inner_height = 0,
inner_width = 0,
js_event_callbacks = {},
js_property_callbacks = {},
left = [LinearAxis(id='3459', ...)],
lod_factor = 10,
lod_interval = 300,
lod_threshold = 2000,
lod_timeout = 500,
margin = (0, 0, 0, 0),
match_aspect = False,
max_height = None,
max_width = None,
min_border = 5,
min_border_bottom = None,
min_border_left = None,
min_border_right = None,
min_border_top = None,
min_height = None,
min_width = None,
name = None,
outer_height = 0,
outer_width = 0,
outline_line_alpha = 1.0,
outline_line_cap = 'butt',
outline_line_color = '#e5e5e5',
outline_line_dash = [],
outline_line_dash_offset = 0,
outline_line_join = 'bevel',
outline_line_width = 1,
output_backend = 'webgl',
renderers = [GlyphRenderer(id='3482', ...), GlyphRenderer(id='3513', ...), GlyphRenderer(id='3546', ...), GlyphRenderer(id='3580', ...)],
reset_policy = 'standard',
right = [],
sizing_mode = 'fixed',
subscribed_events = [],
syncable = True,
tags = [],
title = Title(id='3445', ...),
title_location = 'above',
toolbar = Toolbar(id='3470', ...),
toolbar_location = 'right',
toolbar_sticky = True,
visible = True,
width = 800,
width_policy = 'auto',
x_range = Range1d(id='3447', ...),
x_scale = LinearScale(id='3451', ...),
y_range = Range1d(id='3449', ...),
y_scale = LinearScale(id='3453', ...))
ts = pd.Series(np.random.randn(
100), index=pd.date_range('1/1/2021', periods=100))
df = pd.DataFrame(np.random.randn(100, 4),
index=ts.index, columns=list('ABCD'))
df = df.cumsum()
df.plot_bokeh(rangetool=True)
Column(
id = '4175', …)
align = 'start',
aspect_ratio = None,
background = None,
children = [Figure(id='3950', ...), Figure(id='3983', ...)],
css_classes = [],
disabled = False,
height = None,
height_policy = 'auto',
js_event_callbacks = {},
js_property_callbacks = {},
margin = (0, 0, 0, 0),
max_height = None,
max_width = None,
min_height = None,
min_width = None,
name = None,
rows = 'auto',
sizing_mode = None,
spacing = 0,
subscribed_events = [],
syncable = True,
tags = [],
visible = True,
width = None,
width_policy = 'auto')
data = {
'fruits':
['苹果', '梨', '草莓', '西瓜', '葡萄', '香蕉'],
'2015': [2, 1, 4, 3, 2, 4],
'2016': [5, 3, 3, 2, 4, 6],
'2017': [3, 2, 4, 4, 5, 3]
}
df = pd.DataFrame(data).set_index("fruits")
p_bar = df.plot_bokeh.bar(
ylabel="每斤的的价格 [¥]",
title="水果每年的价格",
alpha=0.6)
我们看到上面的直方图是按照不同的年份分开来的,我们也可以堆叠起来,通过stacked这个参数来实现
df.plot_bokeh.bar(
ylabel="每斤的的价格 [¥]",
title="水果每年的价格",
stacked=True,
alpha=0.6)
Figure(
id = '5598', …)
above = [],
align = 'start',
aspect_ratio = None,
aspect_scale = 1,
background = None,
background_fill_alpha = 1.0,
background_fill_color = '#ffffff',
below = [LinearAxis(id='5609', ...)],
border_fill_alpha = 1.0,
border_fill_color = '#ffffff',
center = [Grid(id='5612', ...), Grid(id='5616', ...), Legend(id='5654', ...)],
css_classes = [],
disabled = False,
extra_x_ranges = {},
extra_x_scales = {},
extra_y_ranges = {},
extra_y_scales = {},
frame_height = None,
frame_width = None,
height = 400,
height_policy = 'auto',
hidpi = True,
inner_height = 0,
inner_width = 0,
js_event_callbacks = {},
js_property_callbacks = {},
left = [LinearAxis(id='5613', ...)],
lod_factor = 10,
lod_interval = 300,
lod_threshold = 2000,
lod_timeout = 500,
margin = (0, 0, 0, 0),
match_aspect = False,
max_height = None,
max_width = None,
min_border = 5,
min_border_bottom = None,
min_border_left = None,
min_border_right = None,
min_border_top = None,
min_height = None,
min_width = None,
name = None,
outer_height = 0,
outer_width = 0,
outline_line_alpha = 1.0,
outline_line_cap = 'butt',
outline_line_color = '#e5e5e5',
outline_line_dash = [],
outline_line_dash_offset = 0,
outline_line_join = 'bevel',
outline_line_width = 1,
output_backend = 'webgl',
renderers = [GlyphRenderer(id='5643', ...), GlyphRenderer(id='5660', ...), GlyphRenderer(id='5676', ...)],
reset_policy = 'standard',
right = [],
sizing_mode = 'fixed',
subscribed_events = [],
syncable = True,
tags = [],
title = Title(id='5599', ...),
title_location = 'above',
toolbar = Toolbar(id='5624', ...),
toolbar_location = 'right',
toolbar_sticky = True,
visible = True,
width = 600,
width_policy = 'auto',
x_range = DataRange1d(id='5601', ...),
x_scale = LinearScale(id='5605', ...),
y_range = DataRange1d(id='5603', ...),
y_scale = LinearScale(id='5607', ...))
ts = pd.Series(np.random.randn(
100), index=pd.date_range('1/1/2021', periods=100))
df = pd.DataFrame(np.random.randn(100, 4),
index=ts.index, columns=list('ABCD'))
df.plot_bokeh.area(
# x="Year", # 指定列名
stacked=True,
legend="top_left",
colormap=["yellow", "orange", "black", "grey"],
title="全球不同能源的消耗量",
ylabel="不同能源的消耗(吨)",
ylim=(0, 10))
Figure(
id = '15858', …)
above = [],
align = 'start',
aspect_ratio = None,
aspect_scale = 1,
background = None,
background_fill_alpha = 1.0,
background_fill_color = '#ffffff',
below = [DatetimeAxis(id='15869', ...)],
border_fill_alpha = 1.0,
border_fill_color = '#ffffff',
center = [Grid(id='15872', ...), Grid(id='15876', ...), Legend(id='15920', ...)],
css_classes = [],
disabled = False,
extra_x_ranges = {},
extra_x_scales = {},
extra_y_ranges = {},
extra_y_scales = {},
frame_height = None,
frame_width = None,
height = 400,
height_policy = 'auto',
hidpi = True,
inner_height = 0,
inner_width = 0,
js_event_callbacks = {},
js_property_callbacks = {},
left = [LinearAxis(id='15873', ...)],
lod_factor = 10,
lod_interval = 300,
lod_threshold = 2000,
lod_timeout = 500,
margin = (0, 0, 0, 0),
match_aspect = False,
max_height = None,
max_width = None,
min_border = 5,
min_border_bottom = None,
min_border_left = None,
min_border_right = None,
min_border_top = None,
min_height = None,
min_width = None,
name = None,
outer_height = 0,
outer_width = 0,
outline_line_alpha = 1.0,
outline_line_cap = 'butt',
outline_line_color = '#e5e5e5',
outline_line_dash = [],
outline_line_dash_offset = 0,
outline_line_join = 'bevel',
outline_line_width = 1,
output_backend = 'webgl',
renderers = [GlyphRenderer(id='15896', ...), GlyphRenderer(id='15927', ...), GlyphRenderer(id='15959', ...), GlyphRenderer(id='15993', ...), GlyphRenderer(id='16030', ...)],
reset_policy = 'standard',
right = [],
sizing_mode = 'fixed',
subscribed_events = [],
syncable = True,
tags = [],
title = Title(id='15859', ...),
title_location = 'above',
toolbar = Toolbar(id='15884', ...),
toolbar_location = 'right',
toolbar_sticky = True,
visible = True,
width = 600,
width_policy = 'auto',
x_range = DataRange1d(id='15861', ...),
x_scale = LinearScale(id='15865', ...),
y_range = Range1d(id='15863', ...),
y_scale = LinearScale(id='15867', ...))