Pyecharts,數據可視化神器。說到它就不得不提Echarts,它是由百度開源的一款使用JavaScript實現的開源可視化庫,涵蓋了各種圖表、滿足各類業務需求,而pyecharts也就是Python與Echarts結合之后的產物,封裝了Echarts各類圖表的基本操作,然后通過渲染機制,
輸出一個包含JS代碼的HTML文件。
使用Pyecharts創建圖形的基本步驟是:
1. 准備數據
2. 設計圖形的樣式、背景顏色
3. Pyecharts繪圖
4. 設計圖表的標題或者圖例等屬性
5. 導出至html
Pyecharts畫圖的基本實例:
1 from pyecharts.charts import Bar,Boxplot 2 from pyecharts.faker import Faker 3 import pyecharts.options as opts 4 from pyecharts.globals import ThemeType 5 6 # 1,堆疊柱狀圖(全部堆疊) 7 def barGrid(): 8 c = ( 9 Bar() 10 .add_xaxis(Faker.choose()) 11 .add_yaxis("商家1", Faker.values(), stack="stack1") 12 .add_yaxis("商家2", Faker.values(), stack="stack1") 13 .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) 14 .set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆疊數據(全部)")) 15 .render("bar_stack_1212.html") 16 ) 17 18 # 2,部分堆疊柱狀圖(部分堆疊) 19 def barGrid_2(): 20 """ 21 部分堆疊柱狀圖 22 """ 23 c = ( 24 Bar(init_opts=opts.InitOpts(width='1080px', height='960px', theme=ThemeType.LIGHT)) 25 .add_xaxis(Faker.choose()) 26 .add_yaxis('商家A', Faker.values(), stack='stack0', bar_max_width='100px') 27 .add_yaxis('商家B', Faker.values(), stack='stack0') 28 .add_yaxis('商家C', Faker.values()) # 不堆疊 29 .set_global_opts(title_opts=opts.TitleOpts(title='柱狀圖堆疊(部分)')) 30 .set_series_opts(label_opts=opts.LabelOpts(is_show=True)) 31 ).render('bar_stack_party.html') 32 33 # 3,柱狀圖的橫坐標傾斜角度 34 def bar_rotation(): 35 c = ( 36 Bar() 37 .add_xaxis( 38 ['名字很長的x軸標簽1', 39 '名字很長的x軸標簽2', 40 '名字很長的x軸標簽3', 41 '名字很長的x軸標簽4', 42 '名字很長的x軸標簽5', 43 '名字很長的x軸標簽6', 44 '名字很長的x軸標簽7'] 45 ) 46 .add_yaxis('商家1', Faker.values()) # 第一組y數據 47 .add_yaxis('商家2', Faker.values()) # 第二組y數據 48 .set_global_opts( 49 xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=15)), # 讓x軸傾斜45度 50 title_opts=opts.TitleOpts(title='柱狀圖x軸標簽傾斜示意圖',subtitle='x軸標簽太長了') 51 ) 52 ).render('bar_rotation_45.html') 53 54 # 4,柱狀圖水平有拖塊兒 55 def bar_auto_zoom_horizontal(): 56 c = ( 57 Bar(init_opts=opts.InitOpts(width='1080px', height='960px', theme=ThemeType.LIGHT)) 58 .add_xaxis(Faker.days_attrs) 59 .add_yaxis('實例1', Faker.days_values) 60 .set_global_opts( 61 title_opts=opts.TitleOpts(title='Bar-柱狀圖縮放(拖塊-水平)'), 62 datazoom_opts=opts.DataZoomOpts(), # 水平方向設置托塊 63 ) 64 ).render('bar_datazoom_horizontal_example.html') 65 66 # 5,柱狀圖垂直方向拖塊 67 def bar_auto_zoom_vertical(): 68 c = ( 69 Bar(init_opts=opts.InitOpts(width='1080px', height='960px', theme=ThemeType.LIGHT)) 70 .add_xaxis(Faker.days_attrs) 71 .add_yaxis('實例1', Faker.days_values) 72 .set_global_opts( 73 title_opts=opts.TitleOpts(title='Bar-柱狀圖縮放(拖塊-垂直)'), 74 datazoom_opts=opts.DataZoomOpts(orient='vertical') # 垂直方向設置拖塊 75 ) 76 ).render('bar_datazoom_vertical_example.html') 77 78 # 6,柱狀圖拖塊內置和外置 79 def bar_auto_zoom(): 80 """ 81 通過縮放拖塊來實現數據縮放 82 """ 83 c = ( 84 Bar(init_opts=opts.InitOpts(width='1080px',height='960px', theme=ThemeType.LIGHT)) 85 .add_xaxis(Faker.days_attrs) 86 .add_yaxis('實例1', Faker.days_values) 87 .set_global_opts( 88 title_opts=opts.TitleOpts(title='Bar-柱狀圖縮放(內置-外置)'), 89 datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_='inside')], # 內置拖塊 90 ) 91 ).render('bar_zoom_last.html') 92 93 # 7,柱狀圖x軸和y軸的命名 94 def bar_x_y_name(): 95 c = ( 96 Bar(init_opts=opts.InitOpts(width='1080px',height='960px', theme=ThemeType.DARK)) 97 .add_xaxis(Faker.choose()) 98 .add_yaxis('實例1', Faker.values()) 99 .add_yaxis('實例2', Faker.values()) 100 .set_global_opts( 101 title_opts=opts.TitleOpts('柱狀圖給x,y軸命名'), 102 yaxis_opts=opts.AxisOpts(name='Y軸名稱'), 103 xaxis_opts=opts.AxisOpts(name='X軸名稱'), 104 ) 105 ).render('bar_name_x_y.html') 106 107 # 8,柱狀圖不同柱子之間的距離也可以不相同 108 def bar_bar_distance(): 109 c = ( 110 Bar(init_opts=opts.InitOpts(width='1080px', height='960px', theme=ThemeType.ROMANTIC)) 111 .add_xaxis(Faker.choose()) 112 .add_yaxis('實例1', Faker.values(), gap='0%') 113 .add_yaxis('實例2', Faker.values(), gap='5%') 114 .set_global_opts(title_opts=opts.TitleOpts(title='柱狀圖間距不同')) 115 ).render('bar_bar_distance.html') 116 117 # 9,柱狀圖是水平方向的 118 def bar_horizontal_y_axis(): 119 c = ( 120 Bar(init_opts=opts.InitOpts(width='1080px', height='960px', theme=ThemeType.ROMA)) 121 .add_xaxis(Faker.choose()) 122 .add_yaxis('實例1',Faker.values()) 123 .add_yaxis('實例2', Faker.values()) 124 .set_global_opts(title_opts=opts.TitleOpts(title='柱狀圖x,y軸翻轉')) 125 .reversal_axis() # 翻轉x,y軸 126 ).render('x_y_reversal.html') 127 128 # 9,直方圖 129 def horitogram(): 130 c = ( 131 Bar(init_opts=opts.InitOpts(width='1280px', height='960px', theme=ThemeType.WONDERLAND)) 132 .add_xaxis(Faker.choose()) 133 .add_yaxis('實例1', Faker.values(), category_gap=0, color=Faker.rand_color()) 134 .set_global_opts(title_opts=opts.TitleOpts('直方圖-Bar')) 135 ).render('bar_horitogram.html') 136 137 # 10,箱型圖 138 def box_chart_pyec(): 139 v1 = [ 140 [850, 740, 950, 1090, 930, 850, 950, 980, 1000, 880, 1000, 980], 141 [980, 940, 960, 940, 900, 800, 850, 880, 950, 840, 830, 800], 142 ] 143 v2 = [ 144 [890, 820, 820, 820, 800, 770, 760, 760, 750, 760, 950, 920], 145 [900, 840, 800, 810, 760, 810, 790, 850, 820, 850, 870, 880], 146 ] 147 c = ( 148 Boxplot(init_opts=opts.InitOpts(width='1280px', height='960px', theme=ThemeType.LIGHT)) 149 .add_xaxis(["實例1", "實例2"]) 150 .add_yaxis('類目1', Boxplot.prepare_data(v1)) 151 .add_yaxis('類目2', Boxplot.prepare_data(v2)) 152 .set_global_opts(title_opts=opts.TitleOpts(title='箱型圖-基本實例')) 153 ).render('box_plot_py.html') 154 155 # 11,日歷圖 156 from pyecharts.charts import Calendar 157 def calendar_chart(): 158 """ 159 日歷圖具體指按照日歷的布局,用顏色展現每一天的數據,從而比較直觀地看到全年的數據情況, 160 例如展示超市全年的銷售額,從而看出具體某個月份或者某個星期的銷售額比較低 161 """ 162 c = ( 163 Calendar(init_opts=opts.InitOpts(width='1280px', height='960px', theme=ThemeType.HALLOWEEN)) 164 .add('', data, calendar_opts=opts.CalendarOpts(range_="2020")) 165 .set_global_opts( 166 title_opts=opts.TitleOpts(title='日歷圖-2020年銷售額'), 167 visualmap_opts=opts.VisualMapOpts( 168 max_=250000, 169 min_=10000, 170 orient='horizontal', 171 is_piecewise=True, 172 pos_left='100px', 173 pos_top='230px', 174 ), 175 ) 176 ).render('calendar_chart.html') 177 178 # 12,K線圖 179 from pyecharts.charts import Kline 180 def k_chart(): 181 k = ( 182 Kline(init_opts=opts.InitOpts(width='1280px', height='960px', theme=ThemeType.VINTAGE)) 183 .add_xaxis(["2020/7/{}".format(i + 1) for i in range(31)]) 184 .add_yaxis('kLine', data) 185 .set_global_opts( 186 yaxis_opts=opts.AxisOpts(is_scale=True), 187 xaxis_opts=opts.AxisOpts(is_scale=True), 188 title_opts=opts.TitleOpts(title='K線圖基本實例'), 189 ) 190 ).render('kline_test.html') 191 192 # 13,漏斗圖 193 from pyecharts.charts import Funnel 194 def funnel_chart(): 195 f = ( 196 Funnel(init_opts=opts.InitOpts(width='1280px', height='960px', theme=ThemeType.LIGHT)) 197 .add('類目', [list(z) for z in zip(Faker.choose(), Faker.values())]) 198 .set_global_opts(title_opts=opts.TitleOpts(title='漏斗圖基本實例')) 199 ).render('funnel_test.html') 200 201 202 # 14,折線圖 203 from pyecharts.charts import Line 204 def line_chart(): 205 l = ( 206 Line() 207 .add_xaxis(Faker.choose()) 208 .add_yaxis('商家1', Faker.values()) 209 .add_yaxis('商家2', Faker.values()) 210 .set_global_opts(title_opts=opts.TitleOpts(title='折線圖基本實例')) 211 ).render('line_test.html') 212 213 # 15,水球圖 214 from pyecharts.charts import Liquid 215 def liquid_chart(): 216 """水球圖通常用於完成指標完成程度""" 217 l = ( 218 Liquid() 219 .add('lq', [0.55, 0.75]) 220 .set_global_opts(title_opts=opts.TitleOpts(title='水球圖基本實例')) 221 ).render('liquid_test.html') 222 223 # 16,詞雲圖 224 from pyecharts.charts import WordCloud 225 def wordcloud_chart(): 226 w = ( 227 WordCloud() 228 .add(series_name='詞雲示例圖', data_pair=data, word_size_range=[5,100]) 229 .set_global_opts( 230 title_opts=opts.TitleOpts( 231 title='詞雲示例圖', 232 title_textstyle_opts=opts.TextStyleOpts(font_size=23)), 233 tooltip_opts=opts.TooltipOpts(is_show=True) 234 ) 235 ).render('basci_wordcloud.html') 236 237 # 17,餅圖 238 from pyecharts.charts import Pie 239 def pie_chart(): 240 p = ( 241 Pie() 242 .add('類目', data_pair=[list(z) for z in zip(Faker.choose(), Faker.values())]) 243 .set_global_opts(title_opts=opts.TitleOpts(title='餅圖基本實例'),) 244 .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}")) 245 ).render('pie_test.html') 246 247 # 18,儀表盤圖 248 from pyecharts.charts import Gauge 249 def gauge_chart(): 250 g = ( 251 Gauge(init_opts=opts.InitOpts()) 252 .add(" ",[('完成率', 70)]) # 元組數據類型 253 .set_global_opts(title_opts=opts.TitleOpts(title='儀表盤圖基本實例')) 254 ).render('gauge_test.html') 255 256 # 19,地圖 257 from pyecharts.charts import Map 258 def map_chart(): 259 m = ( 260 Map(init_opts=opts.InitOpts()) 261 .add('實例1', [list(z) for z in zip(Faker.provinces, Faker.values())]) 262 .set_global_opts(title_opts=opts.TitleOpts(title='地圖基本實例')) 263 ).render('map_test.html') 264 265 266 # 20,漣漪散點圖 267 from pyecharts.charts import EffectScatter 268 def effectscatter_chart(): 269 e = ( 270 EffectScatter(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE)) 271 .add_xaxis(Faker.choose()) 272 .add_yaxis('商家1', Faker.values()) 273 .set_global_opts(title_opts=opts.TitleOpts(title='漣漪散點圖基本實例')) 274 ).render('effectscatter_test.html')
Pyecharts組合圖表
將繪制出來的若干份圖表組合到一塊兒,總共有這幾種方式
- Grid: 並行/垂直放置多張圖
- Page: 順序多圖
- Tab: 多個頁面多圖
- Timeline: 時間軸循環輪播多圖
羡慕別人的可視化大屏,把所有圖表放在同一界面里,自己也來嘗試下,使用page來順序展示多張圖表,挑上面代碼的幾個函數來試試,要把代碼稍微改下,就是把render全部改成return:
1 from pyecharts.charts import Pie 2 def pie_chart(): 3 p = ( 4 Pie() 5 .add('類目', data_pair=[list(z) for z in zip(Faker.choose(), Faker.values())]) 6 .set_global_opts(title_opts=opts.TitleOpts(title='餅圖基本實例'),) 7 .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}")) 8 ) 9 return p 10 # 18,儀表盤圖 11 from pyecharts.charts import Gauge 12 def gauge_chart(): 13 g = ( 14 Gauge(init_opts=opts.InitOpts()) 15 .add(" ",[('完成率', 70)]) # 元組數據類型 16 .set_global_opts(title_opts=opts.TitleOpts(title='儀表盤圖基本實例')) 17 ) 18 return g 19 # 19,地圖 20 from pyecharts.charts import Map 21 def map_chart(): 22 m = ( 23 Map(init_opts=opts.InitOpts()) 24 .add('實例1', [list(z) for z in zip(Faker.provinces, Faker.values())]) 25 .set_global_opts(title_opts=opts.TitleOpts(title='地圖基本實例')) 26 ) 27 return m 28 29 # 20,漣漪散點圖 30 from pyecharts.charts import EffectScatter 31 def effectscatter_chart(): 32 e = ( 33 EffectScatter(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE)) 34 .add_xaxis(Faker.choose()) 35 .add_yaxis('商家1', Faker.values()) 36 .set_global_opts(title_opts=opts.TitleOpts(title='漣漪散點圖基本實例')) 37 ) 38 return e 39 40 from pyecharts.charts import Page 41 42 page = Page(layout=Page.DraggablePageLayout, page_title='基於Pyecharts的可視化大屏') # 之所以用DraggablePageLayout屬性是為了調整成我們所想要的布局,然后將我們所繪制的圖表一一添加 43 page.add( 44 pie_chart(), 45 gauge_chart(), 46 map_chart(), 47 effectscatter_chart(), 48 ).render('可視化大屏設置.html') # 運行完成后手動調整布局
對圖片布局完成之后,要記得點擊左上角的save config按鈕對布局文件進行保存。之后本地會生成一個chart_config.json文件,然后運行下面的代碼:
page.save_resize_html('可視化大屏設置.html', cfg_file='chart_config.json', dest='可視化大屏.html')
運行后就是需要的可視化大屏頁面了。效果大致如下:

