【Python教程】绘制仪表盘图

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 

输出图像如下

985.jpg

以上就是【Python教程】绘制仪表盘图的内容,更多相关内容请关注PHP中文网(www.php.cn)!

登录后复制

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2280125.html

(0)
上一篇 2025年2月27日 17:48:54
下一篇 2025年2月23日 11:13:25

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 【Python教程】地理可视化之二

    basemap是matplotlib的一个子包,负责地图绘制。昨天的推送对如何绘制风向图进行了描述,本文再次利用该包简单介绍如何绘制海洋及海冰温度彩色图示,该图常见于noaa官网。具体操作如下: 导入命令 1)设置工作环境并导入程序包 %c…

    2025年2月27日
    200
  • Python正则表达式【1】

    本文来说说python的正则表达式。 废话不多说了,先开始最简单的: ‘.’:可以匹配除换行符以外的任意单个字符(就是个点)。 ‘*’可以匹配前面的子表达式零次或多次(就是个星号)。 所以上面…

    编程技术 2025年2月27日
    200
  • 高级正则表达式技术(Python版)

    正则表达式是从信息中搜索特定的模式的一把瑞士军刀。它们是一个巨大的工具库,其中的一些功能经常被忽视或未被充分利用。今天我将向你们展示一些正则表达式的高级用法。 举个例子,这是一个我们可能用来检测电话美国电话号码的正则表达式: r’^(1[-…

    编程技术 2025年2月27日
    200
  • Python全栈之路系列之字符串格式化

    This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing &#8…

    编程技术 2025年2月27日
    200
  • Python全栈之路系列之递归

    所谓递归其实就是函数本身调用函数,直到满足指定条件之后一层层退出函数, 例如 从前有座山,山里有座庙,庙里有个老和尚,正在给小和尚讲故事呢!故事是什么呢?“从前有座山,山里有座庙,庙里有个老和尚,正在给小和尚讲故事呢!故事是什么呢?‘从前有…

    编程技术 2025年2月27日
    200
  • Python全栈之路系列之Python3内置函数

    the python interpreter has a number of functions and types built into it that are always available. they are listed here…

    编程技术 2025年2月27日
    200
  • Python标准库系列之模块介绍

    python的模块其实就是封装了一个或者多个功能的代码集合,以便于重用,模块可以是一个文件也可以是一个目录,目录的形式称作包。 模块分类 内置模块 内置模块可以理解成当你安装好python环境之后,直接可以使用import导入的就是内置模块…

    编程技术 2025年2月27日
    200
  • Python黑魔法之描述符

    引言 Descriptors(描述符)是Python语言中一个深奥但很重要的一个黑魔法,它被广泛应用于Python语言的内核,熟练掌握描述符将会为Python程序员的工具箱添加一个额外的技巧。本文我将讲述描述符的定义以及一些常见的场景,并且…

    编程技术 2025年2月27日
    200
  • 用 Vim 写 Python 的最佳实践

    先来晒个图: 对于一些 Python 的小项目,使用 vim 是一个不错的选择。本文内容整理自我在知乎的回答 用 Vim 写 Python 的最佳实践是什么?,下面的内容是对知乎旧有回答的一个补充,尤其有一些主要针对 vim8. 如果想要更…

    2025年2月27日 编程技术
    200
  • Python全栈之路系列之赋值与运算符

    在继续下面的文章之前我们先来浏览一下python为我们提供的几种运算符,定义两个变量,分别是a和b,a的值是10,b的值是20。 算术运算符 运算符 描述 实例 +加,两个对象相加a+b=30-减,两个对象相减,可能会得到负数a-b=-10…

    编程技术 2025年2月27日
    200

发表回复

登录后才能评论