[Python Tutorial] Drawing a Dashboard Chart
Bokeh(Bokeh.js)是一个可在Python中提供交互式的可视化库,其支持Web浏览器,并提供类似于D3.js软件一样的完美展示功能。本文简单介绍如何使用该程序库绘制仪表盘图,具体操作如下:
导入命令
1)设置工作环境
%cd "F:\\Dropbox\\python"
2)导入程序包
import matplotlib.pyplot as plt import numpy as np from matplotlib.image import BboxImage from matplotlib._png import read_png import matplotlib.colors from matplotlib.cbook import get_sample_data from collections import OrderedDict from math import log, sqrt import numpy as np import pandas as pd from bokeh.plotting import figure, show, output_file
3)读取数据
df =pd.read_csv("stata_auto.csv" )
4)定义参数并绘制图像
drug_color = OrderedDict([ ("price", "#0d3362"), ("weight", "#c64737"), ("rep78", "black" ), ]) gram_color = { "Domestic" : "#aeaeb8", "Foreign" : "#e69584", } width = 800 height = 800 inner_radius = 90 outer_radius = 300 - 10 minr = sqrt(log(.001 * 1E4)) maxr = sqrt(log(1000 * 1E4)) a = (outer_radius - inner_radius) / (minr - maxr) b = inner_radius - a * maxr def rad(mic): return a * np.sqrt(np.log(mic * 1E4)) + b big_angle = 2.0 * np.pi / (len(df) + 1) small_angle = big_angle / 7 x = 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_angle colors = [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)) + b p.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 < -np.pi/2] += np.pi # easier to read labels on the left side p.text(xr, yr, df.make, angle=label_angle, text_font_size="9pt", text_align="center", text_baseline="middle") p.circle([-40, -40], [-370, -390], color=list(gram_color.values()), radius=5) p.text([-30, -30], [-370, -390], text=["Gram-" + gr for gr in gram_color.keys()], text_font_size="7pt", text_align="left", text_baseline="middle") p.rect([-40, -40, -40], [18, 0, -18], width=30, height=13, color=list(drug_color.values())) p.text([-15, -15, -15], [18, 0, -18], text=list(drug_color.keys()), text_font_size="9pt", text_align="left", text_baseline="middle") p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None show(p)
输出图像如下
以上就是【Python教程】绘制仪表盘图的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
