背景

Bokeh 初探之后,学习使用它来做个图

目标

做一个柱状图,支持多个 y 数据源,即有堆叠效果的柱状图 stacked bar

实现

单数据源 简单的柱状图

参考 Handling Categorical Data — Bokeh 1.4.0 documentation

from bokeh.io import show, output_file
from bokeh.plotting import figure output_file("bars.html") fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6] p = figure(x_range=fruits, plot_height=250, title="Fruit Counts", toolbar_location=None, tools="") p.vbar(x=fruits, top=counts, width=0.9) p.xgrid.grid_line_color = None
p.y_range.start = 0 show(p)

效果图见上述参考

增加一个 y 数据源,做堆叠效果

这样的话,需要考虑:

  • 数据源:不能是单一的列表了,得能容纳多组数据。用字典。
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"] data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
  • 颜色:区分不同的数据源

colors = ["green", "#718dbf", "#e84d60","#e84d20","#e84361"]

配色是个问题,一不小心就会很丑,后面会提到用调色板 palette

  • 画图:上面的vbar不支持堆叠
p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=data,legend_label=years)

导出为文件

Exporting Plots — Bokeh 1.4.0 documentation

  • html

output_file("file.html")

  • png

  • npm install selenium phantomjs

  • npm install -g phantomjs-prebuilt

  • pip install bokeh

然后 from bokeh.io import export_png

数据源: 从 .csv 文件读取数据

我试过两种方式,现在用的是第二种 pandas

  • numpy 的 genfromtxt

但是我遇到很多问题,包括不同的 dtype参数,names参数等,返回不同的数据类型的 array,感觉很不方便(如排序等),所以后来弃用了,当然也是因为我不太熟。

from numpy import genfromtxt
my_data = genfromtxt("data.csv", delimiter=',', dtype=None, encoding="utf8")
  • pandas

还是这个方便,读取文件 :

df = pd.read_csv("data.csv",header=0)

取前 7 行df = df.head(n=7)

取某一列df['col1']

几列求和df['col1'] + df['col2'] + df['col3']

排序df = df.sort_values(by='col1', ascending=False)

x axis 旋转

Styling Visual Attributes — Bokeh 1.4.0 documentation

比如左斜 旋转 45 度:

    p.xaxis.major_label_orientation = 360-45

调色板

前面我们用 colors = ["green", "#718dbf", "#e84d60","#e84d20","#e84361"] 人工配色,会很丑不专业,bokeh 有自带的调色板,倒是很方便,还好看。

>>> from bokeh.palettes import brewer
>>> colors = brewer["Blues"][6]
>>> colors
['#08519c', '#3182bd', '#6baed6', '#9ecae1', '#c6dbef', '#eff3ff']

具体列表参考:

分类数据处理

如果 x 数据只是数字 如[1,2,3],上面demo 中的 p.figure足以处理

但如果 x 或 y 坐标是一些分类数据如["apple","orange"] ,则需要再添加 x_range,或 y_range

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
p = figure(x_range=fruits, ... )
p.vbar(x=x, top=y, legend_label="Temp.", width=0.9)

参考 Handling Categorical Data — Bokeh 1.4.0 documentation

References

Python 绘图 - Bokeh 柱状图小试(Stacked Bar)的更多相关文章

  1. python绘图:matplotlib和pandas的应用

    在进行数据分析时,绘图是必不可少的模式探索方式.用Python进行数据分析时,matplotlib和pandas是最常用到的两个库.1.matplotlib库的应用准备工作如下:打开ipython,输 ...

  2. 分形、分形几何、数据可视化、Python绘图

    本系列采用turtle.matplotlib.numpy这三个Python工具,以分形与计算机图像处理的经典算法为实例,通过程序和图像,来帮助读者一步步掌握Python绘图和数据可视化的方法和技巧,并 ...

  3. 机器学习-Matplotlib绘图(柱状图,曲线图,点图)

    matplotlib 作为机器学习三大剑客之一   ,比热按时无比强大的 matplotlib是绘图库,所以呢我就分享一下简单的绘图方式 #柱状图 #导报 柱状图 import matplotlib. ...

  4. python绘图之seaborn 笔记

    前段时间学习了梁斌老师的数据分析(升级版)第三讲<探索性数据分析及数据可视化>,由于之前一直比较忙没有来得及总结,趁今天是周末有点闲暇时间,整理一下笔记: 什么是seaborn Seabo ...

  5. 一个交互式可视化Python库——Bokeh

    本篇为<Python数据可视化实战>第十篇文章,我们一起学习一个交互式可视化Python库--Bokeh. Bokeh基础 Bokeh是一个专门针对Web浏览器的呈现功能的交互式可视化Py ...

  6. Python绘图工具Plotly的简单使用

    1.Plotly被称为史上最好的绘图工具之一,为了更好的展示金融数据的复杂性. Plotly的官方网站为:https://plot.ly/ python量化的关键是金融数据可视化,无论是传统的K线图, ...

  7. 【python笔记】使用matplotlib,pylab进行python绘图

    一提到python绘图,matplotlib是不得不提的python最著名的绘图库,它里面包含了类似matlab的一整套绘图的API.因此,作为想要学习python绘图的童鞋们就得在自己的python ...

  8. python绘图 matplotlib教程

    mark一个很好的python绘图教程 https://liam0205.me/2014/09/11/matplotlib-tutorial-zh-cn/

  9. python绘图入门

    python绘图入门 学习了:https://zhuanlan.zhihu.com/p/34200452 API:https://matplotlib.org/api/pyplot_api.html ...

随机推荐

  1. transcription-coupled repair|Germ|HK|TS|Mutation|四类变异

    生命组学-可遗传变异 GC content:碱基: GC content不同的植物对应的gene length,可看作上图的转置: 由GC content看出来碱基变异程度,可以找到对应碱基改变,所以 ...

  2. Ubuntu navicat 连接mysql:access denied for user 'root'@'localhost'

    真是醉了,Ubuntu装了navicat后,准备在桌面建立图标不成,结果直接打开后连接mysql都不行,真坑,奈何远程连接就成,这就尬了,今天终于解决了 问题 我也百度了好几个方案,奈何解决不了,最后 ...

  3. 03 - CDH 6.3.x 安装

    CDH 6.3.x 离线安装 环境 CDH 6.3.1 CentOS 7 官方文档 修改主机名,配置host文件 # 根据个人需要修改主机名称 hostnamectl set-hostname nod ...

  4. mysql 实现类似开窗函数的功能

    mysql8 已经支持开窗函数 https://dev.mysql.com/doc/refman/8.0/en/window-functions.html ———————————————— sql s ...

  5. C# 开启线程的几种方式

    1.异步委托开启线程 public static void Main(string[] args) { Action<int,int> a=add; a.BeginInvoke(,,nul ...

  6. Trie树的插入,查前缀,查单词,删前缀和删单词。

    这个Trie原先用C++就敲得很熟了,看了蓝桥杯的视频后学会把一个功能这样封装起来,以后用的时候就很爽可以直接调用了,所以就用Java写了: public class Trie { private f ...

  7. Welcome to Fan Ouyang’s website!

    Welcome to Fan Ouyang's website! 欧阳璠,哲学博士,湖南娄底人. 目前为浙江大学教育学院课程与学习科学系教育技术专业百人计划研究员. 2013-2018年 明尼苏达大学 ...

  8. application.properties和application.yml

    1.application.properties 文件和 application.yml 文件优先级: 当同时存在时,那application.yml 只是个摆设. 2.yml文件的好处,天然的树状结 ...

  9. Ionic3学习笔记(十二)拍照上传图片以及从相册选择图片上传

    本文为原创文章,转载请标明出处 目录 安装插件 导入 app.module.ts 创建 provider 更多 效果图 1. 安装插件 终端运行: ionic cordova plugin add c ...

  10. Annotation标注

    # View more python tutorials on my Youtube and Youku channel!!! # Youtube video tutorial: https://ww ...