Bokeh (Bokeh.js) is a visual library that can be interactive in Python, supports Web browsers, and provides the perfect presentation functionality similar to D3.js software. This article provides a brief description of how to use the library to draw a dashboard diagram with the following details:
Import command
1) Set the working environment
%CD "F:\\dropbox\\python"
2) Import Package
Import Matplotlib.pyplot as Pltimport numpy as Npfrom matplotlib.image import bboximagefrom matplotlib._png import READ_PN Gimport 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) Read data
DF =pd.read_csv ("Stata_auto.csv")
4) Defining parameters and drawing images
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 (1E4)) A = (Outer_radius-inner_radius)/(MINR-MAXR) b = inner_radius-a * maxrdef rad (MIC): Retur N 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_co Lor=none, line_color= "white") P.text (X[:-1], radii[:-1], [Str (R) to 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 sidep.text (XR, yr, Df.make, angle=label_angle,text_font_size= "9pt", t ext_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_alig N= "left", text_baseline= "Middle") p.rect ([ -40, -40, -40], [, 0, -18], width=30, Height=13,color=list (drug_ Color.values ()) P.text ([ -15, -15, -15], [, 0, -18], Text=list (Drug_color.keys ()), text_font_size= "9pt", text_align= " Left ", text_baseline=" middle ") P.xgrid.grid_line_color = Nonep.ygrid.grid_line_color = Noneshow (P)
The output image is as follows
The above is the "Python tutorial" to draw the contents of the dashboard diagram, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!