首页 > 解决方案 > 散景垂直条形图

问题描述

我目前正在研究 Bokeh 作为 Seaborn 的替代品,但我无法让垂直条形图工作。

我想生成一个垂直条形图,显示 2018 年的总和,按“气缸”

import pandas as pd
import numpy as np
df = pd.read_csv('table1.csv')

df.head(5)

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral5
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg as df
from bokeh.transform import factor_cmap

output_file("groupby.html")

df.Cylinders = df.Cylinders.astype(str)
group = df.groupby('Cylinders')

source = ColumnDataSource(group)

cyl_cmap = factor_cmap('Cylinders', palette=Spectral5, 
factors=sorted(df.Cylinders.unique()))

p = figure(plot_height=350, x_range=group, title="Sales by Cylinder",
       toolbar_location=None, tools="")

p.vbar(x='Cylinders', top='2018', 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)

https://i.stack.imgur.com/Is6zH.png

KeyError: 'Cylinders' 我无法找出导致错误的原因。

编辑 - 问题已解决

这是代码的外观:

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral5
from bokeh.plotting import figure
from bokeh.transform import factor_cmap

output_file("groupby.html")

df.origin = df.Cylinders.astype(str)
group = df.groupby('origin')

source = ColumnDataSource(group)

cyl_cmap = factor_cmap('origin', palette=Spectral5, 
factors=sorted(df.origin.unique()))

p = figure(plot_height=350, x_range=group, title="Sales by origin",
       toolbar_location=None, tools="")

p.vbar(x='origin', top='2018_max', 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)

标签: chartsbokeh

解决方案


推荐阅读