Python使用bokeh制作条形图分类对比
报错:
AttributeError: unexpected attribute 'legend_field' to VBar, possible attributes are bottom, fill_alpha...
bokeh在低版本中1.34中使用legend和高版本是不一样的,在2.4.X中可以用legend_field='label'
,但是在低版本中需要使用legend='label'
源代码:
import csv
from bokeh.palettes import d3
from bokeh.layouts import column
from bokeh.models import ColumnDataSource,FactorRange
from bokeh.plotting import figure, output_file, show
from bokeh.transform import factor_cmapdef plot_recall_precison(csv_path,html_path):# 读取csv至字典csvFile = open(csv_path, "r")reader = csv.reader(csvFile)# 建立空列表result = {}recall_title='Recall Rate:'precision_title='Precision Rate:'#csv文件的行数lines = len(open(csv_path).readlines())for item in reader:# 忽略第一行,reader对象其实就是由CSV文档的多行数据构成的,每行数据会有一个属性:line_num表示行if reader.line_num == 1:# print(item[len(item)-1])recall_title =recall_title+str(round(float(item[len(item)-1])*100,4))+'%'continue#忽略最后统计数量的两行if reader.line_num < lines-1:result[item[0]]=item[1:len(item)-1]#倒数第二行是召回率if reader.line_num == lines-1:precision_title = precision_title+str(round(float(item[0])*100,4))+'%'csvFile.close()#把字典的key转换为列表keys = list(result.keys())#复制列表的函数[1,2,3]->[1,1,2,2,3,3]def double_list(list1):l2 =[]for i in list1:l2.append(i)l2.append(i)return l2#类别名称categorys = ['total','recall']categorys_pre = ['total','precision']#codes的数量和召回数量total_count = []recall_count = []#codes名称codes = []#legend图例的分组labels = []#转换字符为数字类型, '0123': ['54.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '1.0']# 变为'0123': [54.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]# 建立精准率的空列表precision_totalcount = [0]*len(result)i = 0for item in result.items():result[item[0]] = [float(x) for x in result[item[0]]]total_count.append(sum(result[item[0]]))codes.append(item[0])labels.append(item[0])labels.append(item[0])precision_totalcount = list(map(lambda a,b:a+b,precision_totalcount,result[item[0]]))recall_count.append(float(item[1][i]))# print(item[0],item[1],item[1][i])i = i +1print('*'*10)# print(result)print(result.keys())# print('total_count',total_count)# print('codes',codes*2)# print('recall_count',recall_count)# print('precision_totalcount',precision_totalcount)# print('labels',labels)hover_totalcount = double_list(total_count)hover_recallcount = double_list(recall_count)hover_recallrate = list(map(lambda a,b : round(a/b,4) if b !=0 else 0,hover_recallcount,hover_totalcount))hover_precision_tatalcount = double_list(precision_totalcount)hover_precision_rate = list(map(lambda a,b : round(a/b,4) if b !=0 else 0,hover_recallcount,hover_precision_tatalcount))print(hover_recallrate)print(hover_precision_rate)data = {'codes':codes,'total':total_count,'recall':recall_count,'pre_total':precision_totalcount}# create [('0123', 'total'), ('0123', 'recall'), ('1101', 'total'), ('1101', 'recall')x = [ (code, category) for code in codes for category in categorys ]x_pre = [ (code, category) for code in codes for category in categorys_pre]counts = sum(zip(data['total'], data['recall']), ()) # like an hstackcounts_pre = sum(zip(data['pre_total'], data['recall']), ()) # like an hstacksource = ColumnDataSource(data=dict(x=x, counts=counts,label=labels,hover_recallcount=hover_recallcount,hover_totalcount=hover_totalcount,hover_recallrate=hover_recallrate))source_pre = ColumnDataSource(data=dict(x_pre=x_pre, counts_pre=counts_pre,label=labels,hover_recallcount=hover_recallcount,hover_precision_tatalcount=hover_precision_tatalcount,hover_precision_rate=hover_precision_rate))TOOLTIPS = [("Total", "@hover_totalcount"),("Recall", "@hover_recallcount"),('Recall Rate',"@hover_recallrate")]TOOLTIPS_PRE = [("Total", "@hover_precision_tatalcount"),("Recall", "@hover_recallcount"),('Precision Rate', "@hover_precision_rate")]output_file(html_path)color_palette = d3['Category20'][20]+d3['Category20c'][20]#toolbar_location=None为不显示自带的那些功能,tools=""表示柱状图是不可以拖动改变的p = figure(x_range = FactorRange(*x),height=300,width=1000,tooltips=TOOLTIPS,title=recall_title,toolbar_location=None,tools="")#使用color映射条形颜色,其中调色板用d3 https://docs.bokeh.org/en/latest/docs/reference/palettes.html?highlight=viridisp.vbar(x='x', top='counts', width=0.9, bottom=0, color= factor_cmap('x', palette=color_palette[0:len(counts)],factors=x),legend='label',source=source)#toolbar_location=None为不显示自带的那些功能,tools=""表示柱状图是不可以拖动改变的p_pre= figure(x_range = FactorRange(*x_pre),height=300,width=1000,tooltips=TOOLTIPS_PRE,title=precision_title,toolbar_location=None,tools="")p_pre.vbar(x='x_pre', top='counts_pre', width=0.9, bottom=0, color= factor_cmap('x_pre', palette=color_palette[0:len(counts_pre)],factors=x_pre),legend='label',source=source_pre)#y的起始坐标是从0开始p.y_range.start = 0p.y_range.end = max(total_count)+100#x的起始柱状图距离原点的距离p.x_range.range_padding = 0.1p.xaxis.major_label_orientation = 0.5#网格线设置为无p.xgrid.grid_line_color = None#legend图例水平且居中p.legend.orientation = "horizontal"p.legend.location = "top_center"# 去除网格线p.grid.visible = False##精准率的#y的起始坐标是从0开始p_pre.y_range.start = 0p_pre.y_range.end = max(total_count)+100#x的起始柱状图距离原点的距离p_pre.x_range.range_padding = 0.1p_pre.xaxis.major_label_orientation = 0.5#网格线设置为无p_pre.xgrid.grid_line_color = None#legend图例水平且居中p_pre.legend.orientation = "horizontal"p_pre.legend.location = "top_center"# 去除网格线p_pre.grid.visible = Falseshow(column(p,p_pre))if __name__ == '__main__':csv_path = r"H:\B1project\Fastercnn_result\confusion_matrix调阈值\test.csv"html_path = r'H:\recall_pre.html'plot_recall_precison(csv_path,html_path)
Python使用bokeh制作条形图分类对比相关推荐
- python bokeh_提升视觉效果:使用Python和Bokeh制作交互式地图
python bokeh Let's face it, fellow data scientists: our clients LOVE dashboards. Why wouldn't they? ...
- python使用matplotlib制作条形图添加数据标签
在使用matplotlib展示数据的时候,当我们用到条形图的时候南面会遇到一些问题: 水平条形和竖直的条形问题: 解决方法就是更改下函数名,plt.bar()是默认竖直,而plt.barh()是默认水 ...
- python使用matplotlib可视化条形图、使用barh函数可视化条形图(使用barh函数可视化多分类的并行条形图、side by side)
python使用matplotlib可视化条形图.使用barh函数可视化条形图(使用barh函数可视化多分类的并行条形图.side by side) 目录
- [Python从零到壹] 十四.机器学习之分类算法五万字总结全网首发(决策树、KNN、SVM、分类对比实验)
欢迎大家来到"Python从零到壹",在这里我将分享约200篇Python系列文章,带大家一起去学习和玩耍,看看Python这个有趣的世界.所有文章都将结合案例.代码和作者的经验讲 ...
- 用Python优雅地制作动态条形图
公众号"算法美食屋"后台回复关键字:动态图,可添加作者微信获取完整代码和人口数据集. 先上图片: 再上视频: 最后上代码: import numpy as np import pa ...
- 用python制作条形图时出现“posx and posy should be finite values”问题的解决方法
问题1:如下图所示: 在制作条形图时,总是会报这样的错误,意思应该是x,y坐标应该是有限的值,个人在网上也没有收到有关该问题的解决方法. 想了好长时间,终于找到了问题的源头, 如下图所示,我的数据集d ...
- d3 制作条形图_停止制作常见的坏条形图的5个简单技巧
d3 制作条形图 Bar charts were probably the first type of chart you were ever introduced to in first grade ...
- python词云图制作壮观天体照_超简单:快速制作一款高逼格词云图
词云图,也叫文字云,是对文本中出现频率较高的"关键词"予以视觉化的展现,词云图过滤掉大量的低频低质的文本信息,使得浏览者只要一眼扫过文本就可领略文本的主旨. 一.先看看几个词云图 ...
- python交互式绘图库_一个交互式可视化Python库——Bokeh
本篇为<Python数据可视化实战>第十篇文章,我们一起学习一个交互式可视化Python库--Bokeh. Bokeh基础 Bokeh是一个专门针对Web浏览器的呈现功能的交互式可视化Py ...
最新文章
- 全球及中国制糖行业销售规模与运营态势研究报告2022版
- phpAmin如何导入导出大数据文件?
- ABaseApdater
- LeetCode 849. 到最近的人的最大距离
- windows7-SQLyog 安装图解
- Mac安装MySQL详细教程
- SVN之版本管理系统安装及svnadmin编码问题-yellowcong
- 【10.24】一个只属于程序员的节日
- 【杀毒】-记一次挖矿病毒sysdrr杀毒
- html五子棋游戏制作原理,原生JS+Canvas实现五子棋游戏
- nextcloud——搭建自己的云盘
- 关于经济寒冬找工作为什么这么难?
- 申请MallBook分账需要准备哪些材料呢?
- series去重_python去重函数是什么
- Loadrunner C/S关联函数(LSP)AND(LSSS)使用-案例
- MSC.SIMXPERT.V2016全集成多学科仿真解决方案
- PHP调用OpenOffice实现word转PDF
- comsol三维多孔结构 泡沫材料 孔隙介质模型
- 一个数组有n个整数,使其前面各数顺序向后移m个位置, 最后m个数变成最前面的m个数
- 动态数码管原理解释及多种写法,消影