散景,条形图和折线图的组合
Bokeh, combination of bar and line chart
我正在尝试在散景内的条形图顶部绘制一条线。我试过:
p1 = figure()...
p1.renderer.append(Bar(...))
p1.renderer.append(Line(...))
show(p1)
到目前为止我运气不好。
使用 Basic Glyphs 可以在 Bokeh 中的一个图中组合两个或多个图形。
对于你的问题,我们可以使用直线和矩形。
from bokeh.plotting import figure, output_file, show
from bokeh.models.ranges import Range1d
import numpy
output_file("line_bar.html")
p = figure(plot_width=400, plot_height=400)
# add a line renderer
p.line([1, 2, 3, 4, 5], [6, 7, 6, 4, 5], line_width=2)
# setting bar values
h = numpy.array([2, 8, 5, 10, 7])
# Correcting the bottom position of the bars to be on the 0 line.
adj_h = h/2
# add bar renderer
p.rect(x=[1, 2, 3, 4, 5], y=adj_h, width=0.4, height=h, color="#CAB2D6")
# Setting the y axis range
p.y_range = Range1d(0, 12)
p.title = "Line and Bar"
show(p)
我们得到的情节:
搭载@tomaskazemekas:虽然最好避免在 Bokeh 中混合绘图和图表级别,但可以使用 add_glyph
:
修改高级图表对象
from bokeh.charts import Bar, output_file, show
from bokeh.models.ranges import Range1d
from bokeh.models import ColumnDataSource
from bokeh.models.glyphs import Line as Line_glyph
import numpy as np
# create dummy data
df = dict(
x=[1, 2, 3, 4, 5],
y=[6, 7, 6, 4, 5],
h=[2, 8, 5, 10, 7]
)
# create high-level bar chart
p = Bar(data=df, label='x', values='h', color='dodgerblue', title="Bar and Line Plot",
legend=False, plot_width=400, plot_height=400)
# create source data object from data
source = ColumnDataSource(data=df)
# create a line glyph object which references columns from source data
glyph = Line_glyph(x='x', y='y', line_color='grey', line_width=2)
# add the glyph to the chart
p.add_glyph(source, glyph)
# Setting the y axis range
p.y_range = Range1d(0, 12)
output_file("line_bar.html")
show(p)
结果类似:
Bar and Line Plot
我正在尝试在散景内的条形图顶部绘制一条线。我试过:
p1 = figure()...
p1.renderer.append(Bar(...))
p1.renderer.append(Line(...))
show(p1)
到目前为止我运气不好。
使用 Basic Glyphs 可以在 Bokeh 中的一个图中组合两个或多个图形。
对于你的问题,我们可以使用直线和矩形。
from bokeh.plotting import figure, output_file, show
from bokeh.models.ranges import Range1d
import numpy
output_file("line_bar.html")
p = figure(plot_width=400, plot_height=400)
# add a line renderer
p.line([1, 2, 3, 4, 5], [6, 7, 6, 4, 5], line_width=2)
# setting bar values
h = numpy.array([2, 8, 5, 10, 7])
# Correcting the bottom position of the bars to be on the 0 line.
adj_h = h/2
# add bar renderer
p.rect(x=[1, 2, 3, 4, 5], y=adj_h, width=0.4, height=h, color="#CAB2D6")
# Setting the y axis range
p.y_range = Range1d(0, 12)
p.title = "Line and Bar"
show(p)
我们得到的情节:
搭载@tomaskazemekas:虽然最好避免在 Bokeh 中混合绘图和图表级别,但可以使用 add_glyph
:
from bokeh.charts import Bar, output_file, show
from bokeh.models.ranges import Range1d
from bokeh.models import ColumnDataSource
from bokeh.models.glyphs import Line as Line_glyph
import numpy as np
# create dummy data
df = dict(
x=[1, 2, 3, 4, 5],
y=[6, 7, 6, 4, 5],
h=[2, 8, 5, 10, 7]
)
# create high-level bar chart
p = Bar(data=df, label='x', values='h', color='dodgerblue', title="Bar and Line Plot",
legend=False, plot_width=400, plot_height=400)
# create source data object from data
source = ColumnDataSource(data=df)
# create a line glyph object which references columns from source data
glyph = Line_glyph(x='x', y='y', line_color='grey', line_width=2)
# add the glyph to the chart
p.add_glyph(source, glyph)
# Setting the y axis range
p.y_range = Range1d(0, 12)
output_file("line_bar.html")
show(p)
结果类似: Bar and Line Plot