charts - 散景垂直条形图
问题描述
我目前正在研究 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)
解决方案
推荐阅读
- javascript - 我在 JavaScript 上找到“对象布局库”
- react-native - 出现错误:路由“书籍”的组件必须是 React 组件。例如:从 './MyScreen' 导入 MyScreen;
- javascript - 有没有办法可以实时跟踪网站源代码的类变化?
- c++ - 用 move 初始化 lambda 函数参数是个好主意吗?
- python - 使用 openpyxl 插入 xls 时将 CSV 格式保留在字符串中
- javascript - 每次通过我的循环时都希望 html/js 画布更新
- javascript - 侧面板滑入时修改主要内容的 CSS
- javascript - 上下文 Api 状态没有改变
- php - 点击并点击保持操作在手机/Firefox Mobile Tester 中不起作用
- tensorflow - 张量 %s 不是 DeepPavlov 库的此图问题的一个元素