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)!

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor