bokeh(bokeh.js)是一个可在python中提供交互式的可视化库,其支持web浏览器,并提供类似于d3.js软件一样的完美展示功能。本文简单介绍如何使用该程序库绘制仪表盘图,具体操作如下:
导入命令
1)设置工作环境
%cd "F:\Dropbox\python"
登录后复制
2)导入程序包
import matplotlib.pyplot as pltimport numpy as npfrom matplotlib.image import BboxImagefrom matplotlib._png import read_pngimport matplotlib.colorsfrom matplotlib.cbook import get_sample_datafrom collections import OrderedDictfrom math import log, sqrtimport numpy as npimport pandas as pdfrom bokeh.plotting import figure, show, output_file
登录后复制
3)读取数据
立即学习“Python免费学习笔记(深入)”;
df =pd.read_csv("stata_auto.csv" )
登录后复制
4)定义参数并绘制图像
drug_color = OrderedDict([("price", "#0d3362"),("weight", "#c64737"),("rep78", "black" ),])gram_color = {"Domestic" : "#aeaeb8","Foreign" : "#e69584",}width = 800height = 800inner_radius = 90outer_radius = 300 - 10minr = sqrt(log(.001 * 1E4))maxr = sqrt(log(1000 * 1E4))a = (outer_radius - inner_radius) / (minr - maxr)b = inner_radius - a * maxrdef rad(mic):return a * np.sqrt(np.log(mic * 1E4)) + bbig_angle = 2.0 * np.pi / (len(df) + 1)small_angle = big_angle / 7x = np.zeros(len(df))y = np.zeros(len(df))output_file("burtin.html", title="burtin.py example")p = figure(plot_width=width, plot_height=height, title="",x_axis_type=None, y_axis_type=None,x_range=[-420, 420], y_range=[-420, 420],min_border=0, outline_line_color="black",background_fill="#f0e1d2", border_fill="#f0e1d2")p.line(x+1, y+1, alpha=0)angles = np.pi/2 - big_angle/2 - df.index.to_series()*big_anglecolors = [gram_color[gram] for gram in df.foreign]p.annular_wedge(x, y, inner_radius, outer_radius, -big_angle+angles, angles, color=colors,)p.annular_wedge(x, y, inner_radius, rad(df.price),-big_angle+angles+5*small_angle, -big_angle+angles+6*small_angle,color=drug_color['price'])p.annular_wedge(x, y, inner_radius, rad(df.mpg),-big_angle+angles+3*small_angle, -big_angle+angles+4*small_angle,color=drug_color['weight'])p.annular_wedge(x, y, inner_radius, rad(df.gear_ratio),-big_angle+angles+1*small_angle, -big_angle+angles+2*small_angle,color=drug_color['rep78'])labels = np.power(10.0, np.arange(-3, 4))radii = a * np.sqrt(np.log(labels * 1E4)) + bp.circle(x, y, radius=radii, fill_color=None, line_color="white")p.text(x[:-1], radii[:-1], [str(r) for r in labels[:-1]],text_font_size="8pt", text_align="center", text_baseline="middle")p.annular_wedge(x, y, inner_radius-10, outer_radius+10,-big_angle+angles, -big_angle+angles, color="black")xr = radii[0]*np.cos(np.array(-big_angle/2 + angles))yr = radii[0]*np.sin(np.array(-big_angle/2 + angles))label_angle=np.array(-big_angle/2+angles)label_angle[label_angle输出图像如下
以上就是【Python教程】绘制仪表盘图的内容,更多相关内容请关注PHP中文网(www.php.cn)!
登录后复制
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2280125.html