当前位置:Gxlcms > Python > 【Python教程】绘制仪表盘图

【Python教程】绘制仪表盘图

时间:2021-07-01 10:21:17 帮助过:58人阅读

Bokeh(Bokeh.js)是一个可在Python中提供交互式的可视化库,其支持Web浏览器,并提供类似于D3.js软件一样的完美展示功能。本文简单介绍如何使用该程序库绘制仪表盘图,具体操作如下:

导入命令

1)设置工作环境

  1. %cd "F:\\Dropbox\\python"

2)导入程序包

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from matplotlib.image import BboxImage
  4. from matplotlib._png import read_png
  5. import matplotlib.colors
  6. from matplotlib.cbook import get_sample_data
  7. from collections import OrderedDict
  8. from math import log, sqrt
  9. import numpy as np
  10. import pandas as pd
  11. from bokeh.plotting import figure, show, output_file

3)读取数据

  1. df =pd.read_csv("stata_auto.csv" )

4)定义参数并绘制图像

  1. drug_color = OrderedDict([
  2. ("price", "#0d3362"),
  3. ("weight", "#c64737"),
  4. ("rep78", "black" ),
  5. ])
  6. gram_color = {
  7. "Domestic" : "#aeaeb8",
  8. "Foreign" : "#e69584",
  9. }
  10. width = 800
  11. height = 800
  12. inner_radius = 90
  13. outer_radius = 300 - 10
  14. minr = sqrt(log(.001 * 1E4))
  15. maxr = sqrt(log(1000 * 1E4))
  16. a = (outer_radius - inner_radius) / (minr - maxr)
  17. b = inner_radius - a * maxr
  18. def rad(mic):
  19. return a * np.sqrt(np.log(mic * 1E4)) + b
  20. big_angle = 2.0 * np.pi / (len(df) + 1)
  21. small_angle = big_angle / 7
  22. x = np.zeros(len(df))
  23. y = np.zeros(len(df))
  24. output_file("burtin.html", title="burtin.py example")
  25. p = figure(plot_width=width, plot_height=height, title="",
  26. x_axis_type=None, y_axis_type=None,
  27. x_range=[-420, 420], y_range=[-420, 420],
  28. min_border=0, outline_line_color="black",
  29. background_fill="#f0e1d2", border_fill="#f0e1d2")
  30. p.line(x+1, y+1, alpha=0)
  31. angles = np.pi/2 - big_angle/2 - df.index.to_series()*big_angle
  32. colors = [gram_color[gram] for gram in df.foreign]
  33. p.annular_wedge(
  34. x, y, inner_radius, outer_radius, -big_angle+angles, angles, color=colors,
  35. )
  36. p.annular_wedge(x, y, inner_radius, rad(df.price),
  37. -big_angle+angles+5*small_angle, -big_angle+angles+6*small_angle,
  38. color=drug_color['price'])
  39. p.annular_wedge(x, y, inner_radius, rad(df.mpg),
  40. -big_angle+angles+3*small_angle, -big_angle+angles+4*small_angle,
  41. color=drug_color['weight'])
  42. p.annular_wedge(x, y, inner_radius, rad(df.gear_ratio),
  43. -big_angle+angles+1*small_angle, -big_angle+angles+2*small_angle,
  44. color=drug_color['rep78'])
  45. labels = np.power(10.0, np.arange(-3, 4))
  46. radii = a * np.sqrt(np.log(labels * 1E4)) + b
  47. p.circle(x, y, radius=radii, fill_color=None, line_color="white")
  48. p.text(x[:-1], radii[:-1], [str(r) for r in labels[:-1]],
  49. text_font_size="8pt", text_align="center", text_baseline="middle")
  50. p.annular_wedge(x, y, inner_radius-10, outer_radius+10,
  51. -big_angle+angles, -big_angle+angles, color="black")
  52. xr = radii[0]*np.cos(np.array(-big_angle/2 + angles))
  53. yr = radii[0]*np.sin(np.array(-big_angle/2 + angles))
  54. label_angle=np.array(-big_angle/2+angles)
  55. label_angle[label_angle < -np.pi/2] += np.pi # easier to read labels on the left side
  56. p.text(xr, yr, df.make, angle=label_angle,
  57. text_font_size="9pt", text_align="center", text_baseline="middle")
  58. p.circle([-40, -40], [-370, -390], color=list(gram_color.values()), radius=5)
  59. p.text([-30, -30], [-370, -390], text=["Gram-" + gr for gr in gram_color.keys()],
  60. text_font_size="7pt", text_align="left", text_baseline="middle")
  61. p.rect([-40, -40, -40], [18, 0, -18], width=30, height=13,
  62. color=list(drug_color.values()))
  63. p.text([-15, -15, -15], [18, 0, -18], text=list(drug_color.keys()),
  64. text_font_size="9pt", text_align="left", text_baseline="middle")
  65. p.xgrid.grid_line_color = None
  66. p.ygrid.grid_line_color = None
  67. show(p)


输出图像如下

985.jpg



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

人气教程排行