pyecharts可視化的使用


什么是pyecharts?  

  • pyecharts 是一個用於生成 Echarts 圖表的類庫。 
  • echarts 是百度開源的一個數據可視化 JS 庫,主要用於數據可視化。pyecharts 是一個用於生成 Echarts 圖表的類庫。實際上就是 Echarts 與 Python 的對接。
  • 使用 pyecharts 可以生成獨立的網頁,也可以在 flask , Django 中集成使用。

pyecharts包含的圖表

  • Bar(柱狀圖/條形圖)
  • Bar3D(3D 柱狀圖)
  • Boxplot(箱形圖)
  • EffectScatter(帶有漣漪特效動畫的散點圖)
  • Funnel(漏斗圖)
  • Gauge(儀表盤)
  • Geo(地理坐標系)
  • Graph(關系圖)
  • HeatMap(熱力圖)
  • Kline(K線圖)
  • Line(折線/面積圖)
  • Line3D(3D 折線圖)
  • Liquid(水球圖)
  • Map(地圖)
  • Parallel(平行坐標系)
  • Pie(餅圖)
  • Polar(極坐標系)
  • Radar(雷達圖)
  • Sankey(桑基圖)
  • Scatter(散點圖)
  • Scatter3D(3D 散點圖)
  • ThemeRiver(主題河流圖)
  • WordCloud(詞雲圖)

用戶自定義

  • Grid 類:並行顯示多張圖
  • Overlap 類:結合不同類型圖表疊加畫在同張圖上
  • Page 類:同一網頁按順序展示多圖
  • Timeline 類:提供時間線輪播多張圖

pyecharts安裝

pip install pyecharts

pyecharts==0.5.10
pyecharts-javascripthon==0.0.6
pyecharts-jupyter-installer==0.0.3
pyecharts-snapshot==0.1.10
View Code

由於牆的原因,下載時會出現斷線和速度過慢的問題導致下載失敗,所以建議通過清華鏡像來進行下載::pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts

這里要專門說明一下,自從 0.3.2 開始,為了縮減項目本身的體積以及維持 pyecharts 項目的輕量化運行,pyecharts 將不再自帶地圖 js 文件。如用戶需要用到地圖圖表(Geo、Map),可自行安裝對應的地圖文件包。

  • 全球國家地圖: echarts-countries-pypkg (1.9MB): 世界地圖和 213 個國家,包括中國地圖
  • 中國省級地圖: echarts-china-provinces-pypkg (730KB):23 個省,5 個自治區
  • 中國市級地圖: echarts-china-cities-pypkg (3.8MB):370 個中國城市

Windows下通過已下的pip命令進行安裝

pip install echarts-countries-pypkg

pip install echarts-china-provinces-pypkg

pip install echarts-china-cities-pypkg
echarts-china-cities-pypkg 0.0.9

echarts-china-counties-pypkg 0.0.2

echarts-china-misc-pypkg 0.0.1

echarts-china-provinces-pypkg 0.0.3

echarts-countries-pypkg 0.1.6

echarts-united-kingdom-pypkg 0.0.1
View Code

Linux下通過以下的pip命令進行安裝

sudo pip3 install echarts-countries-pypkg 

sudo pip3 install echarts-china-provinces-pypkg

sudo pip3 install echarts-china-cities-pypkg

sudo pip3 install echarts-countries-pypkg 

sudo pip3 install echarts-china-provinces-pypkg

sudo pip3 install echarts-china-cities-pypkg

相關函數、參數說明

先介紹一些可能會用到的基本函數:

  • add()    主要方法,用於添加圖表的數據和設置各種配置項
  • show_config()    打印輸出圖表的所有配置項
  • render()    默認將會在根目錄下生成一個 render.html 的文件,支持 path 參數,設置文件保存位置,如 render(r"e:my_first_chart.html"),文件用瀏覽器打開。

默認的編碼類型為 UTF-8,在 Python3 中是沒什么問題的,Python3 對中文的支持好很多。但是在 Python2 中,編碼的處理是個很頭疼的問題,暫時沒能找到完美的解決方法,目前只能通過文本編輯器自己進行二次編碼,我用的是 Visual Studio Code,先通過 Gbk 編碼重新打開,然后再用 UTF-8 重新保存,這樣用瀏覽器打開的話就不會出現中文亂碼問題了。(這里是某位大佬遇到的問題,我直接復制過來了,供大家參考)

 

基本上所有的圖表類型都是這樣繪制的:

  1. chart_name = Type() 初始化具體類型圖表。
  2. add() 加數據及配置項。
  3. render() 生成 .html 文件。
使用之前我們要強調一點:就是python2.x和python3.x的編碼問題,在python3.x中你可以把它看做默認是unicode編碼,但在python2.x中並不是默認的,原因就在它的bytes對象定義的混亂,而pycharts是使用unicode編碼來處理字符串和文件的,所以當你使用的是python2.x時,請務必在上方插入此代碼: from future import unicode_literals。

直接將圖片保存為 png, pdf, gif 格式的文件

第一步:安裝phantomjs:

 打開系統命令行,鍵入下列代碼:

npm install -g phantomjs-prebuilt

第二步:安裝了 Nodejs 環境:

 下載鏈接:https://nodejs.org/en/download/

第三步:安裝 pyecharts-snapshot:

pip install pyecharts-snapshot

第四步:重啟計算機,render(path=“”)的輸出路徑和格式即可。

from pyecharts import Pie,configure
configure(output_image=True)
pie =Pie('餅圖',background_color = 'white',title_text_size = 25)
attr = ['惠州','東莞','廣州','深圳','佛山','江門','珠海']
v1 = [12,22,34,29,16,14,18]
pie.add('',attr,v1,is_label_show = True)
pie.render(path = 'D:\\餅圖.jpeg')

基本使用

官網文檔:https://pyecharts.org/#/zh-cn/quickstart

柱狀圖

Bar(柱狀圖/條形圖):柱狀/條形圖,通過柱形的高度/條形的寬度來表現數據的大小。

add(name, x_axis, y_axis, is_stack=False, bar_category_gap='20%', **kwargs)。

常用的參數(可以在add()中設置,實現各種需求):

  • name->str 圖例名稱 。
  • x_axis -> list:x 坐標軸數據
  • y_axis -> list:y 坐標軸數據
  • bar_category_gap -> int/str:類目軸的柱狀距離,當設置為 0 時柱狀是緊挨着(直方圖類型),默認為 '20%'
  • is stack:是否堆積
  • is convert:x,y軸是否交換
  • is_legend_show:是否顯示圖例
  • legend_orient:圖列方向,有'horizontal'(默認水平顯示),'vertical'(垂直)可選
  • legend_pos:圖例位置,有'left', 'center'(默認), 'right'可選
  • legend_text_size/color:圖例名稱字體大小/顏色
  • is_x/yaxis_show:是否顯示x/y軸
  • x/yaxis_interval:x/y軸標簽的顯示間隔
  • x/yaxis_min/max:x/y軸刻度最小值/最大值
  • x/yaxis_label_textsize/textcolor:x/y軸標簽字體大小/顏色
  • xaxis_rotate:x軸標簽旋轉角度
  • is_splitline_show:是否顯示網格線
  • is_label_show:是否顯示標簽
  • label_pos:標簽的位置,有'top'(默認), 'left', 'right', 'bottom', 'inside','outside'可選
  • label_text_color/size:標簽字體顏色/大小
  • is_random:是否隨機排列顏色列表
  • label_color:自定義標簽顏色
  • mark_point/line:標記點/線,默認有'min', 'max', 'average'可選。可自定義標記點線,具體格式如:[{'coord': [x, y], 'name': '目標標記點'}],記住格式是一個列表
  • mark_point/line_symbol:標記點/線圖形,默認為'pin'(點),有'circle'(圓形), 'rect'(正方形), 'roundRect'(圓角正方形), 'triangle'(三角形), 'diamond'(菱形), 'pin'(點), 'arrow'(箭頭)可選
  • mark_point/line_symbolsize:標記點/線圖形大小
  • mark_point/line_textcolor:標記點/線字體顏色

基本配置

'''1.在Bar()函數里添加的參數'''

# title -> str:主標題文本,支持 \n 換行,默認為 “”

# subtitle -> str:副標題文本,支持 \n 換行,默認為 “”

# width -> int:畫布寬度,默認為 800(px)

# height -> int:畫布高度,默認為 400(px)

# title_color -> str:主標題文本顏色,默認為 ‘#000’

# subtitle_color -> str:副標題文本顏色,默認為 ‘#aaa’

# background_color -> str:畫布背景顏色,默認為 ‘#fff’

# page_title -> str:指定生成的 html 文件中 <title> 標簽的值。默認為’Echarts’

# renderer -> str:指定使用渲染方式,有 ‘svg’ 和 ‘canvas’ 可選,默認為 ‘canvas’。3D 圖僅能使用 ‘canvas’(canvas和svg區別請參考:https://blog.csdn.net/helloword_chen/article/details/49788309 )


'''2.在add()函數里添加的參數'''

# is_random -> bool:是否隨機排列顏色列表,默認為 False

# label_color -> list:自定義標簽顏色。全局顏色列表,所有圖表的圖例顏色均在這里修改。如 Bar 的柱狀顏色,Line 的線條顏色等等。

# is_label_show -> bool:是否正常顯示標簽,默認不顯示。標簽即各點的數據項信息

# label_pos -> str:標簽的位置,Bar 圖默認為’top’。有’top’, ‘left’, ‘right’, ‘bottom’, ‘inside’,’outside’可選

# label_text_color -> str:標簽字體顏色,默認為 “#000”

# label_text_size -> int:標簽字體大小,默認為 12

# is_random -> bool:是否隨機排列顏色列表,默認為 False

# label_formatter -> function
def label_formatter(params):
      return params.value + ' [Good!]'
#  (params: Object|Array) => string
    '''
  參數 params 是 formatter 需要的單個數據集。格式如下:
  {
      componentType: 'series',
      
      // 系列類型
      seriesType: string,
      
      // 系列在傳入的 option.series 中的 index
      seriesIndex: number,
      
      // 系列名稱
      seriesName: string,
      
      // 數據名,類目名
      name: string,
      
      // 數據在傳入的 data 數組中的 index
      dataIndex: number,
      
      // 傳入的原始數據項
      data: Object,
      
      // 傳入的數據值
      value: number|Array,
      
      // 數據圖形的顏色
      color: string,
  }
    '''
# 總結

# 導入相關圖表包
# 進行圖表的基礎設置,創建圖表對象
# 利用add()方法進行數據輸入與圖表設置(可以使用print_echarts_options()來輸出所有可配置項)
# 利用render()方法來進行圖表保存

#柱狀圖
from pyecharts import Bar
attr =["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 =[5, 20, 36, 10, 10, 100]
v2 =[55, 60, 16, 20, 15, 80]

bar = Bar('柱形圖', '庫存量',width=1000,height=600)
bar.add('服裝', attr, v1,  is_label_show=True)
# bar.show_config()
# bar.render(path='./data/01-01柱形圖.html')

bar2 = Bar("顯示標記線和標記點")
bar2.add('商家A', attr, v1, mark_point=['avgrage'])
bar2.add('商家B', attr, v2, mark_point=['min', 'max'])
# bar2.show_config()
# bar2.render(path='./data/01-02標記點柱形圖.html')

bar3 = Bar("水平顯示")
bar3.add('商家A', attr, v1)
bar3.add('商家B', attr, v2, is_convert=True)
# bar3.show_config()
bar3.render(path='./01-03水平柱形圖.html')
bar3
from pyecharts import Bar,configure
# configure(output_image=True)
bar = Bar('各個城市的人口','虛構的',background_color = 'white',title_text_size = 25,subtitle_text_size = 15)
attr = ['惠州','東莞','廣州','深圳','佛山','江門','珠海']
v1 = [23,45,68,58,32,28,36]
v2 = [12,22,34,29,16,14,18]
bar.add('舉例數字1',attr,v1,is_label_show = True,mark_point = ['min','max'],
         mark_point_symbol = 'diamond',xaxis_rotate = 30,xaxis_label_textsize = 15,yaxis_label_textsize = 15)
bar.add('舉例數字2',attr,v2,is_label_show = True,mark_point = ['min','max'],
         mark_point_symbol = 'triangle',xaxis_rotate = 30,xaxis_label_textsize = 15,yaxis_label_textsize = 15)
# bar.render(path = 'D:\\示例1.jpeg')
bar
View Code
import random
attr = ["{}天".format(i) for i in range(20)]
v1 = [random.randint(1, 20) for _ in range(20)]
bar = Bar("坐標軸標簽旋轉示例")
bar.add("", attr, v1, xaxis_interval=0, xaxis_rotate=30, yaxis_rotate=30)

bar
View Code

折線圖

from pyecharts import Line
attr =["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 =[5, 20, 36, 10, 10, 100]
v2 =[55, 60, 16, 20, 15, 80]

# 普通折線圖
line = Line('折線圖')
line.add('商家A', attr, v1, mark_point=['max'])
line.add('商家B', attr, v2, mark_point=['min'], is_smooth=True) #is_smooth:是否平滑處理
# line.show_config()
# line.render(path='./data/01-04折線圖.html')

# 階梯折線圖
line2 = Line('階梯折線圖')
line2.add('商家A', attr, v1,  is_step=True, is_label_show=True)
# line2.show_config()
# line2.render(path='./data/01-05階梯折線圖.html')

# is_fill實現面積折線圖
line3 =Line("面積折線圖")
line3.add("商家A", attr, v1, is_fill=True, line_opacity=0.2,   area_opacity=0.4, symbol=None, mark_point=['max'])
line3.add("商家B", attr, v2, is_fill=True, area_color='#a3aed5', area_opacity=0.3, is_smooth=True) #area_opacity:填充面積的透明度;area_color:填充面積的顏色;symbol = None:去掉線上的點。
# line3.show_config()
# line3.render(path='./data/01-06面積折線圖.html')

line

柱形圖-折線圖

# 柱形圖-折線圖
from pyecharts import Bar, Line, Overlap

att = ['A', 'B', 'C', 'D', 'E', 'F']
v3 = [10, 20, 30, 40, 50, 60]
v4 = [38, 28, 58, 48, 78, 68]

bar = Bar("柱形圖-折線圖")
bar.add('bar', att, v3)
line = Line()
line.add('line', att, v4)

overlap = Overlap()
overlap.add(bar)
overlap.add(line)
# overlap.show_config()
# overlap.render(path='./data/01-066柱形圖-折線圖.html')
overlap

餅圖

from pyecharts import Pie
# 餅圖
pie = Pie('餅圖')
pie.add('芝麻餅', attr, v1, is_label_show=True,title_text_size = 25)
# pie.show_config()
# pie.render(path='./data/01-07餅圖.html')

# 設置rosetype參數實現南丁格爾圖(玫瑰圖)
pie2 = Pie("餅圖-玫瑰圖示例", title_pos='center', width=900)
pie2.add("商品A", attr, v1, center=[25, 50], is_random=True, radius=[30, 75], rosetype='radius')
pie2.add("商品B", attr, v2, center=[75, 50], is_random=True, radius=[30, 75], rosetype='area', is_legend_show=False, is_label_show=True)
'''
rosetype有'radius'和'area'兩種模式,其中:
radius:通過半徑顯示數據的大小,扇區圓心角展現數據的百分比;
area:通過半徑顯示數據的大小,各扇區圓心角相等。
另外還可以通過設置center參數調整圓心位置(center = [x,y],x為橫坐標,y為縱坐標)
'''
# pie2.show_config()
# pie2.render(path='./data/01-08玫瑰餅圖.html')

#設置radius參數實現圓環圖:radius = [x,y],x是內半徑,y是外半徑

pie3 =Pie('圓環圖',background_color = 'white',title_pos = 'center')
pie3.add('',attr,v1,is_label_show = True,radius=[30, 75],is_legend_show = False)

pie4 =Pie('圓環中的玫瑰圖',background_color = 'white')
attr = ['惠州','東莞','廣州','深圳','佛山','江門','珠海']
v1 = [12,22,34,29,16,14,18]
pie4.add( '',attr,v1,radius=[65, 75],center=[50, 50])
pie4.add('',attr,v1,radius=[0, 60],center=[50, 50],rosetype='area')

pie2

散點圖

from pyecharts import  Scatter
# 散點圖
v1 =[10, 20, 30, 40, 50, 60]
v2 =[10, 20, 30, 40, 50, 60]
scatter =Scatter("散點圖示例")
scatter.add("A", v1, v2)
scatter.add("B", v1[::-1], v2,xaxis_min = 5)
# scatter.show_config()
# scatter.render(path='./data/03-06散點圖.html')

# 散點打印Pyecharts字體 白底圖片
# scatter =Scatter("散點圖示例")
# v1, v2 = scatter.draw("./data/two.jpg")
# scatter.add("pyecharts", v1, v2, is_random=True)
# scatter.show_config()
# scatter.render(path='./data/03-06打印字體.html')
scatter
from pyecharts import Scatter

scatter = Scatter( '學霸是畫出來的')

'''
draw(self, path, color=None)
 |      將圖片上的像素點轉換為數組,如 color 為(255,255,255)時只保留非白色像素點的
 |      坐標信息返回兩個 k_lst, v_lst 兩個列表剛好作為散點圖的數據項
 |      
 |      :param path:
 |          轉換圖片的地址
 |      :param color:
 |          所要排除的顏色
 |      :return:
 |          轉換后的數組
'''
v1, v2 = scatter.draw( './pic.png')
scatter.add( '學霸印章', v1, v2, mark_size=1)

# scatter.render( './參考案例HTML/學霸是畫出來的散點圖.html')
scatter
View Code

動態散點圖

from pyecharts import EffectScatter
attr =["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 =[5, 20, 36, 10, 10, 100]
v2 =[55, 60, 16, 20, 15, 80]

# 動態散點圖
es =EffectScatter("動態散點圖")

# v1 x坐標 v2 y坐標
es.add("商家", v1, v2)
# es.show_config()
# es.render('./data/01-09散點圖.html')

# 動態散點圖各種圖形
es2 = EffectScatter("動態散點圖各種圖形")
es2.add("", [10], [10], symbol_size=20, effect_scale=3.5,  effect_period=3, symbol="pin")
es2.add("", [20], [20], symbol_size=12, effect_scale=4.5, effect_period=4,symbol="rect")
es2.add("", [30], [30], symbol_size=30, effect_scale=5.5, effect_period=5,symbol="roundRect")
es2.add("", [40], [40], symbol_size=10, effect_scale=6.5, effect_brushtype='fill',symbol="diamond")
es2.add("", [50], [50], symbol_size=16, effect_scale=5.5, effect_period=3,symbol="arrow")
es2.add("", [60], [60], symbol_size=6, effect_scale=2.5, effect_period=3,symbol="triangle")
# es.show_config()
# es.render(path = "./data/01-10動態散點圖各種圖形.html")
es2

多個餅圖

from pyecharts import Pie
pie =Pie('各類電影中"好片"所占的比例', "數據來着豆瓣", title_pos='center')
pie.add("", ["劇情", ""], [25, 75], center=[10, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, )
pie.add("", ["奇幻", ""], [24, 76], center=[30, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None,legend_pos='left' )
pie.add("", ["愛情", ""], [14, 86], center=[50, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["驚悚", ""], [11, 89], center=[70, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["冒險", ""], [27, 73], center=[90, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["動作", ""], [15, 85], center=[10, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["喜劇", ""], [54, 46], center=[30, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["科幻", ""], [26, 74], center=[50, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["懸疑", ""], [25, 75], center=[70, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["犯罪", ""], [28, 72], center=[90, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, is_legend_show=True,legend_top="center" )
# pie.show_config()
# pie.render(path='./data/01-多個餅圖.html')
pie

多標記柱形圖

from pyecharts import Bar
attr =["{}月".format(i) for i in range(1, 13)]
v1 =[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 =[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
bar =Bar("柱狀圖示例")
bar.add("蒸發量", attr, v1, mark_line=["average"], mark_point=["max", "min"])
bar.add("降水量", attr, v2, mark_line=["average"], mark_point=["max", "min"])
# bar.show_config()
# bar.render(path='./data/02-多標記柱形圖.html')
bar

儀表盤

add(name, attr, value, scale_range=None, angle_range=None, **kwargs)

  • name -> str:圖例名稱
  • attr -> list:屬性名稱
  • value -> list:屬性所對應的值
  • scale_range -> list:儀表盤數據范圍。默認為 [0, 100]
  • angle_range -> list:儀表盤角度范圍。默認為 [225, -45]
from pyecharts import Gauge
# 儀表盤
gauge = Gauge("儀表盤")
gauge.add('業務指標', '完成率', 96.66)
# gauge.show_config()
# gauge.render(path="./data/02-02儀表盤.html")
gauge
from pyecharts import Gauge

gauge = Gauge( '今日加班概率')

gauge.add( '', "可能性", 99.99, angle_range=[ 225, -45],scale_range=[ 0, 100], is_legend_show= False)

# gauge.render( './參考案例HTML/今日加班概率儀表盤.html')

gauge
View Code

漏斗圖

from pyecharts import Funnel
# 漏斗圖
funnel = Funnel('漏斗圖')
funnel.add('商品',attr,v1, is_label_show=True, label_pos='inside', label_text_color="#fff",legend_pos='left',legend_top="bottom")
# funnel.show_config()
# funnel.render(path="./data/02-01漏斗圖.html")
funnel

關系圖

from pyecharts import Funnel, Gauge, Graph
# 關系圖
nodes =[{"name": "結點1", "symbolSize": 10}, {"name": "結點2", "symbolSize": 20}, {"name": "結點3", "symbolSize": 30}, {"name": "結點4", "symbolSize": 40}, {"name": "結點5", "symbolSize": 50}, {"name": "結點6", "symbolSize": 40}, {"name": "結點7", "symbolSize": 30}, {"name": "結點8", "symbolSize": 20}]
links =[]
for i in nodes:
    for j in nodes:
        links.append({"source": i.get('name'), "target": j.get('name')})

# print(links)
# print(nodes)
graph =Graph("關系圖-環形布局示例")
graph.add("", nodes, links, is_label_show=True, repulsion=8000,     layout='circular', label_text_color=None)
# graph.show_config()
# graph.render(path="./data/02-03關系圖.html")
graph

水球圖

from pyecharts import Liquid
liquid =Liquid("水球圖")
liquid.add("Liquid", [0.6])
# liquid.show_config()
# liquid.render(path='./data/03-01水球.html')

# 圓形水球
liquid2 =Liquid("水球圖示例")
liquid2.add("Liquid", [0.6, 0.5, 0.4, 0.3], is_liquid_outline_show=False)
# liquid2.show_config()
# liquid2.render(path='./data/03-02圓形水球.html')

# 菱形水球
liquid3 =Liquid("水球圖示例")
liquid3.add("Liquid", [0.6, 0.5, 0.4, 0.3], is_liquid_animation=False, shape='diamond')
# liquid3.show_config()
# liquid3.render(path='./data/03-03菱形水球.html')
liquid3

極坐標

from pyecharts import Polar
# 極坐標
radius =['周一', '周二', '周三', '周四', '周五', '周六', '周日']
polar =Polar("極坐標系-堆疊柱狀圖示例", width=1200, height=600)
polar.add("A", [1, 2, 3, 4, 3, 5, 1], radius_data=radius, type='barRadius', is_stack=True)
polar.add("B", [2, 4, 6, 1, 2, 3, 1], radius_data=radius, type='barRadius', is_stack=True)
polar.add("C", [1, 2, 3, 4, 1, 2, 5], radius_data=radius, type='barRadius', is_stack=True)
# polar.show_config()
# polar.render(path='./data/03-04極坐標.html')
polar

雷達圖

from pyecharts import Radar
# 雷達圖
schema =[ ("銷售", 6500), ("管理", 16000), ("信息技術", 30000), ("客服", 38000), ("研發", 52000), ("市場", 25000)]
v1 =[[4300, 10000, 28000, 35000, 50000, 19000]]
v2 =[[5000, 14000, 28000, 31000, 42000, 21000]]
radar =Radar()
radar.config(schema)
radar.add("預算分配", v1, is_splitline=True, is_axisline_show=True)
radar.add("實際開銷", v2, label_color=["#4e79a7"], is_area_show=False)
# radar.show_config()
# radar.render(path='./data/03-05雷達圖.html')
# 雷達圖-Rader
from pyecharts import Radar
radar = Radar("雷達圖", "一年的降水量與蒸發量")

# //由於雷達圖傳入的數據得為多維數據,所以這里需要做一下處理
radar_data1 = [[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]]
radar_data2 = [[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]]

# //設置column的最大值,為了雷達圖更為直觀,這里的月份最大值設置有所不同
schema = [ ("Jan", 5), ("Feb",10), ("Mar", 10), ("Apr", 50), ("May", 50), ("Jun", 200), ("Jul", 200), ("Aug", 200), ("Sep", 50),
          ("Oct", 50), ("Nov", 10), ("Dec", 5)]

# //傳入坐標
radar.config(schema)
radar.add("降水量",radar_data1)

# //一般默認為同一種顏色,這里為了便於區分,需要設置item的顏色
radar.add("蒸發量",radar_data2,item_color="#1C86EE")
radar
View Code

詞雲圖

from pyecharts import WordCloud

name =['Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World', 'Charter Communications', 'Chick Fil A', 'Planet Fitness', 'Pitch Perfect', 'Express', 'Home', 'Johnny Depp', 'Lena Dunham', 'Lewis Hamilton', 'KXAN', 'Mary Ellen Mark', 'Farrah Abraham', 'Rita Ora', 'Serena Williams', 'NCAA baseball tournament', 'Point Break']
value=[10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112, 965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265]
wordcloud =WordCloud(width=1000, height=620)
wordcloud.add("", name, value, word_size_range=[20, 100])
# wordcloud.show_config()
# wordcloud.render(path='./data/05-01權重詞雲.html')


wordcloud2 =WordCloud(width=1000, height=620)
wordcloud2.add("", name, value, word_size_range=[30, 100], shape='diamond')
# wordcloud2.show_config()
# wordcloud2.render(path='./data/05-02變形詞雲.html')

# # 中文顯示
# from  pyecharts import WordCloud, ImageColorGenerator
# from PIL import Image
# import matplotlib.pyplot as plt
# import numpy as np
# words = ['好看', '不錯', '人性', '可以', '值得', '真的', '一部', '感覺', '喜歡', '一般', '演技', '還是',
#    '劇情', '一出', '有點', '出好', '好戲', '不是', '沒有', '非常', '哈哈', '喜劇', '就是', '一個',
#    '現實', '什么', '支持', '還行', '但是', '很多', '覺得', '搞笑', '值得一看', '故事', '看好',
#    '這部', '哈哈哈', '失望', '最后', '導演', '自己', '演員', '看完', '社會', '特別', '看到', '不好',
#    '比較', '表達', '那么', '作品', '個人', '東西', '思考', '這個', '第一', '不過', '情節',
#    '哈哈哈哈', '意思', '一直', '推薦', '一般般', '時候', '開始', '般般', '片子', '知道', '處女',
#    '期待', '很棒', '影院', '深度', '反應', '無聊', '可能', '一些', '精彩', '愛情', '這么', '希望',
#    '一點', '不知', '有些', '還好', '恐怖', '看着', '沒看', '還有', '觀看', '后面', '真實', '因為',
#    '如果', '出來', '部分', '確實', '我們', '意義', '深刻']

# new_worlds = " ".join(words)
# # 參照圖片
# coloring = np.array(Image.open("./pic.png"))

# # simkai.ttf 必填項 識別中文的字體,例:simkai.ttf,
# my_wordcloud = WordCloud(background_color="white", max_words=800,
#                      mask=coloring, max_font_size=120, random_state=30, scale=2,font_path="./data/simkai.ttf").generate(new_worlds)

# image_colors = WordCloud.ImageColorGenerator(coloring)
# plt.imshow(my_wordcloud.recolor(color_func=image_colors))
# plt.imshow(my_wordcloud)
# plt.axis("off")
# plt.show()

# # 保存圖片
# my_wordcloud.to_file('./data/05-03signature.png')
#詞雲圖
# 參數          含義                                                                                                                     類型


# name          圖列名稱                                                                                                                str

# attr          word數據,詞雲圖中顯示的詞語                                                                                            list

# valua         詞語權重,相當於詞頻                                                                                                    list

# shape         生成的詞雲圖輪廓, 有'circle', 'cardioid', 'diamond', 'triangle-forward', 'triangle', 'pentagon', 'star'可選             list


# word_gap      單詞間隔,默認為20                                                                                                      int


# word_size_range 單詞字體大小范圍,默認為[12,60]                                                                                        list


# rotate_step     旋轉單詞角度, 默認為45


from pyecharts import WordCloud
name =['Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World', 'Charter Communications', 'Chick Fil A', 'Planet Fitness', 'Pitch Perfect', 'Express', 'Home', 'Johnny Depp', 'Lena Dunham', 'Lewis Hamilton', 'KXAN', 'Mary Ellen Mark', 'Farrah Abraham', 'Rita Ora', 'Serena Williams', 'NCAA baseball tournament', 'Point Break']
value =[10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112, 965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265]
wordcloud =WordCloud(width=1000, height=620)
wordcloud.add("", name, value, word_size_range=[20, 100])
# wordcloud.show_config()
print(help(wordcloud))
View Code

k線圖

add(name, x_axis, y_axis, **kwargs)

name -> str:圖例名稱

x_axis -> list:x 坐標軸數據

y_axis -> [list]:包含列表的列表 y 坐標軸數據。

數據中,每一行是一個『數據項』,每一列屬於一個『維度』。 數據項具體為 [open, close, lowest, highest] (即:[開盤值, 收盤值, 最低值, 最高值])

from pyecharts import Kline
 
v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38],
      [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8],
      [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82],
      [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38],
      [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73],
      [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03],
      [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07],
      [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82],
      [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78],
      [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63],
      [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65],
      [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14],
      [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02],
      [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96],
      [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33],
      [2255.77, 2270.28, 2253.31, 2276.22]]
kline = Kline("K 線圖示例")
kline.add("日K", ["2018/9/{}".format(i + 1) for i in range(30)], v1)
kline

3D柱形圖

add(name, x_axis, y_axis, data, grid3d_opacity=1, grid3d_shading='color', **kwargs)

  • name -> str:圖例名稱。
  • x_axis -> str:x 坐標軸數據。需為類目軸,也就是不能是數值。
  • y_axis -> str:y 坐標軸數據。需為類目軸,也就是不能是數值。
  • data -> [list],:包含列表的列表 數據項,數據中,每一行是一個『數據項』,每一列屬於一個『維度。
  • grid3d_opacity -> int:3D 笛卡爾坐標系組的透明度(柱狀的透明度),默認為 1,完全不透明。
  • grid3d_shading -> str:三維柱狀圖中三維圖形的着色效果。
  • color:只顯示顏色,不受光照等其它因素的影響。
  • lambert:通過經典的 lambert 着色表現光照帶來的明暗。
  • realistic:真實感渲染,配合 light.ambientCubemap 和 postEffect 使用可以讓展示的畫面效果和質感有質的提升。ECharts GL 中使用了基於物理的渲染(PBR) 來表現真實感材質。
from pyecharts import Bar3D

bar3d = Bar3D("3D 柱狀圖示例", width=1000, height=600)
x_axis = [
    "12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a",
    "12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"
    ]
y_axis = [
    "Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday", "Sunday"
    ]
data = [
    [0, 0, 5], [0, 1, 1], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 5, 0],
    [0, 6, 0], [0, 7, 0], [0, 8, 0], [0, 9, 0], [0, 10, 0], [0, 11, 2],
    [0, 12, 4], [0, 13, 1], [0, 14, 1], [0, 15, 3], [0, 16, 4], [0, 17, 6],
    [0, 18, 4], [0, 19, 4], [0, 20, 3], [0, 21, 3], [0, 22, 2], [0, 23, 5],
    [1, 0, 7], [1, 1, 0], [1, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0],
    [1, 6, 0], [1, 7, 0], [1, 8, 0], [1, 9, 0], [1, 10, 5], [1, 11, 2],
    [1, 12, 2], [1, 13, 6], [1, 14, 9], [1, 15, 11], [1, 16, 6], [1, 17, 7],
    [1, 18, 8], [1, 19, 12], [1, 20, 5], [1, 21, 5], [1, 22, 7], [1, 23, 2],
    [2, 0, 1], [2, 1, 1], [2, 2, 0], [2, 3, 0], [2, 4, 0], [2, 5, 0],
    [2, 6, 0], [2, 7, 0], [2, 8, 0], [2, 9, 0], [2, 10, 3], [2, 11, 2],
    [2, 12, 1], [2, 13, 9], [2, 14, 8], [2, 15, 10], [2, 16, 6], [2, 17, 5],
    [2, 18, 5], [2, 19, 5], [2, 20, 7], [2, 21, 4], [2, 22, 2], [2, 23, 4],
    [3, 0, 7], [3, 1, 3], [3, 2, 0], [3, 3, 0], [3, 4, 0], [3, 5, 0],
    [3, 6, 0], [3, 7, 0], [3, 8, 1], [3, 9, 0], [3, 10, 5], [3, 11, 4],
    [3, 12, 7], [3, 13, 14], [3, 14, 13], [3, 15, 12], [3, 16, 9], [3, 17, 5],
    [3, 18, 5], [3, 19, 10], [3, 20, 6], [3, 21, 4], [3, 22, 4], [3, 23, 1],
    [4, 0, 1], [4, 1, 3], [4, 2, 0], [4, 3, 0], [4, 4, 0], [4, 5, 1],
    [4, 6, 0], [4, 7, 0], [4, 8, 0], [4, 9, 2], [4, 10, 4], [4, 11, 4],
    [4, 12, 2], [4, 13, 4], [4, 14, 4], [4, 15, 14], [4, 16, 12], [4, 17, 1],
    [4, 18, 8], [4, 19, 5], [4, 20, 3], [4, 21, 7], [4, 22, 3], [4, 23, 0],
    [5, 0, 2], [5, 1, 1], [5, 2, 0], [5, 3, 3], [5, 4, 0], [5, 5, 0],
    [5, 6, 0], [5, 7, 0], [5, 8, 2], [5, 9, 0], [5, 10, 4], [5, 11, 1],
    [5, 12, 5], [5, 13, 10], [5, 14, 5], [5, 15, 7], [5, 16, 11], [5, 17, 6],
    [5, 18, 0], [5, 19, 5], [5, 20, 3], [5, 21, 4], [5, 22, 2], [5, 23, 0],
    [6, 0, 1], [6, 1, 0], [6, 2, 0], [6, 3, 0], [6, 4, 0], [6, 5, 0],
    [6, 6, 0], [6, 7, 0], [6, 8, 0], [6, 9, 0], [6, 10, 1], [6, 11, 0],
    [6, 12, 2], [6, 13, 1], [6, 14, 3], [6, 15, 4], [6, 16, 0], [6, 17, 0],
    [6, 18, 0], [6, 19, 0], [6, 20, 1], [6, 21, 2], [6, 22, 2], [6, 23, 6]
    ]
range_color = ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
               '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
bar3d.add(
    "",
    x_axis,
    y_axis,
    [[d[1], d[0], d[2]] for d in data],
    is_visualmap=True,
    visual_range=[0, 20],
    visual_range_color=range_color,
    grid3d_width=200,
    grid3d_depth=80,
     is_grid3d_rotate=True,
    grid3d_rotate_speed=25
)
bar3d

熱力圖

from pyecharts import HeatMap

import random

x_axis = ["12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a",
          "12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"]
y_aixs = ["Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday", "Sunday"]
data = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)]
heatmap = HeatMap()
heatmap.add("熱力圖直角坐標系", x_axis, y_aixs, data, is_visualmap=True,
            visual_text_color="#000", visual_orient='horizontal')

heatmap

3D折線圖

from pyecharts import Line3D

import math
_data = []
for t in range(0, 25000):
    _t = t / 1000
    x = (1 + 0.25 * math.cos(75 * _t)) * math.cos(_t)
    y = (1 + 0.25 * math.cos(75 * _t)) * math.sin(_t)
    z = _t + 2.0 * math.sin(75 * _t)
    _data.append([x, y, z])
range_color = ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
               '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
line3d = Line3D("3D 折線圖示例", width=1200, height=600)
line3d.add("", _data, is_visualmap=True, visual_range_color=range_color, visual_range=[0, 30],
           is_grid3d_rotate=True, grid3d_rotate_speed=25)

line3d

平行坐標系-默認指示器

from pyecharts import Parallel

schema = ["data", "AQI", "PM2.5", "PM10", "CO", "NO2"]
data = [
        [1, 91, 45, 125, 0.82, 34],
        [2, 65, 27, 78, 0.86, 45,],
        [3, 83, 60, 84, 1.09, 73],
        [4, 109, 81, 121, 1.28, 68],
        [5, 106, 77, 114, 1.07, 55],
        [6, 109, 81, 121, 1.28, 68],
        [7, 106, 77, 114, 1.07, 55],
        [8, 89, 65, 78, 0.86, 51, 26],
        [9, 53, 33, 47, 0.64, 50, 17],
        [10, 80, 55, 80, 1.01, 75, 24],
        [11, 117, 81, 124, 1.03, 45]
]
parallel = Parallel("平行坐標系-默認指示器")
parallel.config(schema) 
parallel.add("parallel", data, is_random=True)

parallel

3D散點圖

from pyecharts import Scatter3D

import random
data = [[random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)] for _ in range(80)]
range_color = ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
               '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
scatter3D = Scatter3D("3D 散點圖示例", width=1200, height=600)
scatter3D.add("", data, is_visualmap=True, visual_range_color=range_color,is_grid3d_rotate=True, grid3d_rotate_speed=25)

scatter3D

多個圖

from pyecharts import Bar, Line, Scatter, EffectScatter, Grid  

attr = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("柱狀圖示例", height=720, width=1200, title_pos="65%")
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True, legend_pos="80%")
line = Line("折線圖示例")
attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
line.add("最高氣溫", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"])
line.add("最低氣溫", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"],
         mark_line=["average"], legend_pos="20%")
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
scatter = Scatter("散點圖示例", title_top="50%", title_pos="65%")
scatter.add("scatter", v1, v2, legend_top="50%", legend_pos="80%")
es = EffectScatter("動態散點圖示例", title_top="50%")
es.add("es", [11, 11, 15, 13, 12, 13, 10], [1, -2, 2, 5, 3, 2, 0], effect_scale=6,
        legend_top="50%", legend_pos="20%")

grid = Grid()
grid.add(bar, grid_bottom="60%", grid_left="60%")
grid.add(line, grid_bottom="60%", grid_right="60%")
grid.add(scatter, grid_top="60%", grid_left="60%")
grid.add(es, grid_top="60%", grid_right="60%")

grid

Timeline時間軸

from pyecharts import Bar, Timeline

from random import randint

attr = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
bar_1 = Bar("2012 年銷量", "數據純屬虛構")
bar_1.add("春季", attr, [randint(10, 100) for _ in range(6)])
bar_1.add("夏季", attr, [randint(10, 100) for _ in range(6)])
bar_1.add("秋季", attr, [randint(10, 100) for _ in range(6)])
bar_1.add("冬季", attr, [randint(10, 100) for _ in range(6)])

bar_2 = Bar("2013 年銷量", "數據純屬虛構")
bar_2.add("春季", attr, [randint(10, 100) for _ in range(6)])
bar_2.add("夏季", attr, [randint(10, 100) for _ in range(6)])
bar_2.add("秋季", attr, [randint(10, 100) for _ in range(6)])
bar_2.add("冬季", attr, [randint(10, 100) for _ in range(6)])

bar_3 = Bar("2014 年銷量", "數據純屬虛構")
bar_3.add("春季", attr, [randint(10, 100) for _ in range(6)])
bar_3.add("夏季", attr, [randint(10, 100) for _ in range(6)])
bar_3.add("秋季", attr, [randint(10, 100) for _ in range(6)])
bar_3.add("冬季", attr, [randint(10, 100) for _ in range(6)])

bar_4 = Bar("2015 年銷量", "數據純屬虛構")
bar_4.add("春季", attr, [randint(10, 100) for _ in range(6)])
bar_4.add("夏季", attr, [randint(10, 100) for _ in range(6)])
bar_4.add("秋季", attr, [randint(10, 100) for _ in range(6)])
bar_4.add("冬季", attr, [randint(10, 100) for _ in range(6)])

bar_5 = Bar("2016 年銷量", "數據純屬虛構")
bar_5.add("春季", attr, [randint(10, 100) for _ in range(6)])
bar_5.add("夏季", attr, [randint(10, 100) for _ in range(6)])
bar_5.add("秋季", attr, [randint(10, 100) for _ in range(6)])
bar_5.add("冬季", attr, [randint(10, 100) for _ in range(6)], is_legend_show=True)

timeline = Timeline(is_auto_play=True, timeline_bottom=0,timeline_play_interval=500)
timeline.add(bar_1, '2012 年')
timeline.add(bar_2, '2013 年')
timeline.add(bar_3, '2014 年')
timeline.add(bar_4, '2015 年')
timeline.add(bar_5, '2016 年')
# timeline

 箱體圖

# 箱體圖-Boxplot
# //導入箱型圖Boxplot
from pyecharts import Boxplot
boxplot = Boxplot("箱形圖", "一年的降水量與蒸發量")
data1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
data2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
x_axis = ['降水量','蒸發量']
y_axis = [data1,data2]

# //prepare_data方法可以將數據轉為嵌套的 [min, Q1, median (or Q2), Q3, max]
yaxis = boxplot.prepare_data(y_axis)
boxplot.add("天氣統計", x_axis, y_axis)
# boxplot.render()
boxplot

地圖

Geo(地理坐標系)

  • add(name, attr, value, type="scatter", maptype='china', coordinate_region='中國', symbol_size=12, border_color="#111", geo_normal_color="#323c48", geo_emphasis_color="#2a333d", geo_cities_coords=None, is_roam=True, **kwargs)
  • name -> str 圖例名稱
  • attr -> list 屬性名稱,(其實就是地點名)
  • value -> list 屬性所對應的值
  • type -> str 圖例類型,有'scatter', 'effectScatter', 'heatmap'可選。默認為 'scatter'
  • maptype -> str 地圖類型。 從 v0.3.2+ 起,地圖已經變為擴展包,支持全國省份,全國城市,全國區縣,全球國家等地圖,具體請參考 地圖自定義篇
  • coordinate_region -> str 城市坐標所屬國家。從 v0.5.7 引入,針對國際城市的地理位置的查找。默認為 中國。具體的國家/地區映射表參照 countries_regions_db.json。更多地理坐標信息可以參考 數據集篇
  • symbol_size -> int 標記圖形大小。散點的大小。默認為 12
  • border_color -> str 地圖邊界顏色。默認為 '#111'
  • geo_normal_color -> str 正常狀態下地圖區域的顏色。默認為 '#323c48'
  • geo_emphasis_color -> str 高亮狀態下地圖區域的顏色。默認為 '#2a333d'
  • geo_cities_coords -> dict 用戶自定義地區經緯度,類似如 {'阿城': [126.58, 45.32],} 這樣的字典。 i
  • s_roam -> bool 是否開啟鼠標縮放和平移漫游。默認為 True 如果只想要開啟縮放或者平移,可以設置成'scale'或者'move'。設置成 True 為都開啟
  • visual_range:可視化的數值范圍
  • visual_text_color:標簽顏色
  • is_piecewise :顏色是否分段顯示(False為漸變,True為分段)
  • is_visualmap:是否映射(數量與顏色深淺是否掛鈎)
  • visual_split_number :可視化數值分組

Map(地圖) 地圖主要用於地理區域數據的可視化

  • add(name, attr, value, maptype='china', is_roam=True, is_map_symbol_show=True, **kwargs)
  • name -> str 圖例名稱
  • attr -> list 屬性名稱
  • value -> list 屬性所對應的值
  • maptype -> str 地圖類型。 從 v0.3.2+ 起,地圖已經變為擴展包,支持全國省份,全國城市,全國區縣,全球國家等地圖,具體請參考 地圖自定義篇
  • is_roam -> bool/str 是否開啟鼠標縮放和平移漫游。默認為 True 如果只想要開啟縮放或者平移,可以設置成'scale'或者'move'。設置成 True 為都開啟
  • is_map_symbol_show -> bool 是否顯示地圖標記紅點,默認為 True
  • name_map -> dict 用自定義的地圖名稱. 有些地圖提供行政區號,name_map 可以幫助把它們轉換成用戶滿意的地名。比如英國選區地圖,倫敦選區的行政區號是 E14000639 ,把它轉換成可讀地名就需要這么一個字典

重要問題說明

這里也要先說明一點因為echarts里面有的地點可能沒有,一旦添加了沒有的在這里面,將會生成一個空的圖。有三個解決辦法:

  1. 最簡單的把不存在刪掉;
  2. 到百度地圖api里把找不到的地方的經緯度加進原始的包里(這個辦法大家可以參看這位大佬的博客https://blog.csdn.net/summerstones/article/details/78078789);
  3. 如果你的數據在地圖中並沒有坐標城市,可以通過 geo_cities_coords 自己進行添加,GeoLine圖同樣如此
#地理坐標圖
from pyecharts import Map, Geo
# data =[("海門", 9), ("鄂爾多斯", 12), ("招遠", 12), ("舟山", 12), ("齊齊哈爾", 14), ("鹽城", 15)]
# geo =Geo("全國主要城市空氣質量", "data from pm2.5", title_color="#fff", title_pos="center", width=1000, height=600, background_color='#404a59')
# attr, value =geo.cast(data)
# geo.add("", attr, value, type="effectScatter", is_random=True, effect_scale=5)#type的可選參數:effectScatter,heatmap
# # geo.show_config()


# 世界地圖數據
value = [95.1, 23.2, 43.3, 66.4, 88.5]
attr= ["China", "Canada", "Brazil", "Russia", "United States"]

# 省和直轄市
province_distribution = {'河南': 45.23, '北京': 37.56, '河北': 21, '遼寧': 12, '江西': 6, '上海': 20, '安徽': 10, '江蘇': 16, '湖南': 9, '浙江': 13, '海南': 2, '廣東': 22, '湖北': 8, '黑龍江': 11, '澳門': 1, '陝西': 11, '四川': 7, '內蒙古': 3, '重慶': 3, '雲南': 6, '貴州': 2, '吉林': 3, '山西': 12, '山東': 11, '福建': 4, '青海': 1, '舵主科技,質量保證': 1, '天津': 1, '其他': 1}
provice=list(province_distribution.keys())
values=list(province_distribution.values())

# 城市 -- 指定省的城市 xx市
city = ['南寧市', '玉林市', '梧州市', '河池市', '欽州市']
values2 = [15, 65, 7, 8, 5]

# 區縣 -- 具體城市內的區縣  xx縣
quxian = ['陸川縣', '博白縣', '玉州區', '北流市', '容縣']
values3 = [80, 5, 7, 8, 2]
map0 = Map("世界地圖示例", width=1200, height=600)
map0.add("世界地圖", attr, value, maptype="world",  is_visualmap=True, visual_text_color='#000')
# map0.render(path="./04-00世界地圖.png")

#中國地圖
map = Map("中國地圖",'中國地圖', width=1200, height=600)
map.add("", provice, values, visual_range=[0, 50],  maptype='china', is_visualmap=True,
    visual_text_color='#000')
# map.show_config()
# map.render(path="./data/04-01中國地圖.html")

#省級地圖
map2 = Map("廣西各地的靚仔產量圖",'單位:%', width=1000, height=600)
map2.add('', city, values2, visual_range=[1, 10], maptype='廣西', is_visualmap=True, visual_text_color='#000')
# map2.show_config()
# map2.render(path="./data/04-02河南地圖.html")

# # 玉林地圖 數據為商丘市下的區縣
map3 = Map("玉林地圖",'', width=1200, height=600)
map3.add("", quxian, values3, visual_range=[1, 10], maptype='玉林', is_visualmap=True,
    visual_text_color='#000')
# map3.render(path="./data/04-03商丘地圖.html")
map0
#加載pyecharts
from pyecharts import Geo,Style
import pandas as pd 
 
#導入excel表舉例
df=pd.read_excel('流動人口.xlsx')     
df.head()
 
#導入自定義的地點經緯度
geo_cities_coords={df.iloc[i]['地點']:[df.iloc[i]['經度'],df.iloc[i]['緯度']]
                    for i in range(len(df))}   
attr=list(df['地點'])
value=list(df['流動人口']/100000)
style = Style(title_color= "#fff",title_pos = "center",
width = 1200,height = 600,background_color = "#404a59")
 
#可視化
geo = Geo('東莞各個CGI總用戶數分布',**style.init_style)
geo.add("",attr,value,visual_range=[0,100],symbol_size= 5,
        visual_text_color= "#fff",is_piecewise = True,
        is_visualmap= True,maptype = '東莞', visual_split_number= 10,
        geo_cities_coords=geo_cities_coords)
 
geo.render( '東莞各個CGI總用戶數分布.html')
View Code
style = Style(title_color= "#fff",title_pos = "center",
width = 1200,height = 600,background_color = "#404a59")
geo = Geo('東莞各個CGI總用戶數分布',**style.init_style)
geo.add("",attr,value,visual_range=[0,100],
        visual_text_color= "#fff",type = 'heatmap',
        is_visualmap= True,maptype = '東莞',
        geo_cities_coords=geo_cities_coords)
 
geo.render( '東莞各個CGI總用戶數分布.html')
當增加一個參數type = 'heatmap',就會得到熱力圖:
from pyecharts import Map, GeoLines
data =[["廣州", "北京"],["鄂爾多斯", '南寧'],["招遠",'南寧'],["南寧","北京"],["舟山", '南寧'],["齊齊哈爾", '北京'], ["鹽城", '北京']]
geo =GeoLines("全國主要城市空氣質量", "data from pm2.5", title_color="#000", title_pos="center", width=1000, height=600)
# attr, value =geo.cast(data)
geo.add("",data,geo_effect_color='#6f363f',geo_effect_symbol='circle',geo_normal_color='#fff', is_geo_effect_show=True)#type的可選參數:effectScatter,heatmap
geo

Surface3D-曲面波圖

from pyecharts import Surface3D

import random
data = [[random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)] for _ in range(80)]
range_color = ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
               '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
surface3D = Surface3D("3D 散點圖示例", width=1200, height=600)
surface3D.add("", data, is_visualmap=True, visual_range_color=range_color,is_grid3d_rotate=True, grid3d_rotate_speed=25)

surface3D

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM