【轉載】python使用pyecharts製作各種圖表

原文地址:https://blog.csdn.net/qq_31967985/article/details/79975663

環境:pyecharts庫,echarts-countries-pypkg,echarts-china-provinces-pypkg,echarts-china-cities-pypkg 

數據:2018年4月16號的全國各地最高最低和天氣類型的數據——2018-4-16.json(爬蟲爬的)

代碼:天氣數據爬蟲代碼,圖表繪製代碼 代碼地址:https://github.com/goodloving/pyecharts.git(py文件)

一、公共屬性

1、標題欄的屬性:一般在實例化(初始化)類型時給與,如bar = Bar(“大標題”,“副標題”,···各種屬性···)

        title_color = “顏色”:標題顏色,可以是‘red’或者‘#0000’

        title_pos = ‘位置’:標題位置,如‘center’,‘left’···

        width = 1200:圖表的寬

        height = 800:圖表的高

        background_color = "顏色":圖表的背景色

        ·····

2、標籤欄的屬性:如bar.add(“標籤”,x,values,···屬性···)

        'mark_'類,通個'mark_'顯示,如 mark_point['max', 'min', 'average']:標出最大最小和平均值的點,

                mark_point_textcolor,mark_line_symbolsize·····

        'legend_'類,如legend_pos=‘left’:標籤的位置

         'is_'類,如is_label_show=True:顯示每個點的值,is_datazoom_show=True:實現移動控制x軸的數量

                is_convert = True:x,y軸是否調換

eg:

bar = pyecharts.Bar("全國各地最高氣溫", "2018-4-18", title_color='red', title_pos='right', width=1400, height=700,                         background_color='#404a59')
bar.add("最高氣溫", cities, highs, mark_point=['max', 'min', 'average'], is_label_show=True, is_datazoom_show=True,                         legend_pos='left')
bar.render('Bar-High.html')

3、Geo,Map無法顯示底圖

pyecharts v0.3.2以後,pyecharts 將不再自帶地圖 js 文件。如用戶需要用到地圖圖表,可自行安裝對應的地圖文件包。

地圖文件被分成了三個 Python 包,分別爲:
全球國家地圖: echarts-countries-pypkg (1.9MB)
中國省級地圖: echarts-china-provinces-pypkg (730KB)

中國市級地圖: echarts-china-cities-pypkg (3.8MB)

(1)pycharm直接在設置裏面搜索安裝這三個庫

(2)pip安裝

        pip install echarts-countries-pypkg 

      pip install echarts-china-provinces-pypkg  

      pip install echarts-china-cities-pypkg  

二、各種圖表

1.柱狀圖/條形圖——Bar

   bar = pyecharts.Bar("全國各地最高最低氣溫", "2018-4-18", title_pos='right', title_color='blue', width=1400,                     height=700,background_color='white')
    bar.add("最高氣溫", cities, highs, mark_point=['max'], legend_text_color='red', is_datazoom_show=True)
    bar.add("最低氣溫", cities, lows, mark_line=['min'], legend_text_color='blue' )

    bar.render('Bar-High-Low.html')

2、散點圖——EffectScatter

es = pyecharts.EffectScatter("最低氣溫動態散點圖", "2018-4-16", title_pos='right', title_color='blue', width=1400,                 height=700, background_color='white')
es.add("最低溫度", range(0, len(cities)), lows, legend_pos='center', legend_text_color='blue',symbol_size=10,                 effect_period=3, effect_scale=3.5, symbol='pin',is_datazoom_show=True,is_label_show=True)

es.render("EffectScatter-low.html")

3、漏斗與——Funnel

fl = pyecharts.Funnel("最高氣溫漏斗圖", "2018-40-16", title_pos='left', width=1400, height=700)
fl.add("最低氣溫", cities[:15], lows[:15], is_label_show=True, label_pos='inside', label_text_color='white')

fl.render("Funnel-low.html")

4、儀表盤——Guage

    gu = pyecharts.Gauge("儀表盤圖")
    gu.add("指標", "達標", 80)

    gu.render("Guage-eg.html")

5、地理座標圖——Geo

    geo = pyecharts.Geo("最高氣溫地理座標系圖", '2018-4-16', title_color='#fff', title_pos='center', width=1200,                 height=600, background_color='#404a95')
     geo.add("最高氣溫", cities, highs, is_visualmap=True, visual_range=[0, 40], visual_text_color='#fff', symbol_size=5,                 legend_pos='right',is_geo_effect_show=True)

     geo.render("Geo-Low.html")

6、關係圖——Graph(略)

7、折線/面積圖——Line

    line = pyecharts.Line("氣溫變化折線圖", '2018-4-16', width=1200, height=600)
    line.add("最高氣溫", cities, highs, mark_point=['average'], is_datazoom_show=True)
    line.add("最低氣溫", cities, lows, mark_line=['average'], is_smooth=True)

    line.render('Line-High-Low.html')

    line = pyecharts.Line("氣溫變化折線圖", '2018-4-16', width=1200, height=600)
   line.add("最高氣溫", cities, highs, mark_point=['average'], is_datazoom_show=True, is_fill=True, line_opacity=0.2,             area_opacity=0.4)
 line.add("最低氣溫", cities, lows, mark_line=['average'], is_smooth=True, is_fill=True, area_color="#000",             area_opacity=0.5)

    line.render('Area-High-Low.html')

8、水滴球——Liquid

    lq = pyecharts.Liquid("水滴球")
    lq.add("Liquid", [0.8, 0.5, 0.2], is_liquid_outline_show=False, is_liquid_animation=True)

    lq.render("LiQuid.html")

9、地圖——Map

    a_city = []
    for i in cities:
        a_city.append(i + '市')
    map = pyecharts.Map("湖北最低氣溫", width=1200, height=600)
 map.add("最低氣溫", a_city, lows, maptype='湖北', is_visualmap=True, visual_text_color='#000',             visual_range=  [-15, 20])

    map.render("Map-low.html")

value = [95.1, 23.2, 43.3, 66.4, 88.5]

attr= ["China", "Canada", "Brazil", "Russia", "United States"]

map = Map("世界地圖示例", width=1200, height=600)

map.add("", attr, value, maptype="world", is_visualmap=True, visual_text_color='#000')

map.render('Map-World.html')

10、平行座標圖——Parallel

    parallel = pyecharts.Parallel("高低溫度的平行座標系圖", '2018-4-16', width=1200, height=600)
    parallel.config(cities[:20])
    parallel.add("高低溫", [highs[:20], lows[:20]], is_random=True)

    parallel.render('Parallel-High-Low.html')

11、餅圖——Pie

    sun = 0
    cloud = 0
    lit_rain = 0
    mit_rain = 0
    sail = 0
    shadom = 0
    z_rain = 0
    th_rain = 0
    for i in types:
        if i == '晴':
            sun += 1
        elif i == '多雲':
            cloud += 1
        elif i == '小雨':
            lit_rain += 1
        elif i == '中雨':
            mit_rain += 1
        elif i == '陰':
            shadom += 1
        elif i == '陣雨':
            z_rain += 1
        elif i == '雷陣雨':
            th_rain += 1
        elif i == '揚沙':
            sail += 1
    pie = pyecharts.Pie("全國天氣類型比例", '2018-4-16')
    pie.add('天氣類型', weather, [mit_rain, lit_rain, sail, sun, th_rain, cloud, shadom, z_rain], is_label_show=True)

    pie.render('Pie-weather.html')

修改:pie = pyecharts.Pie("全國天氣類型比例", '2018-4-16', title_pos='center')
        pie.add('天氣類型', weather, [mit_rain, lit_rain, sail, sun, th_rain, cloud, shadom, z_rain], is_label_show=True,                 legend_pos='left', label_text_color=None, legend_orient='vertical', radius=[30, 75])

           pie.render('Pie-weather.html')

pie鑲嵌:

center -> list

餅圖的中心(圓心)座標,數組的第一項是橫座標,第二項是縱座標,默認爲 [50, 50]默認設置成百分比,設置成百分比時第一項是相對於容器寬度,第二項是相對於容器高度

rosetype -> str

是否展示成南丁格爾圖,通過半徑區分數據大小,有'radius'和'area'兩種模式。默認爲'radius'radius:扇區圓心角展現數據的百分比,半徑展現數據的大小area:所有扇區圓心角相同,僅通過半徑展現數據大小

    pie = pyecharts.Pie("全國天氣類型比例", '2018-4-16')
  pie.add('', weather, [mit_rain, lit_rain, sail, sun, th_rain, cloud, shadom, z_rain], is_label_show=True, label_text_color=None,         legend_orient='vertical', radius=[40, 50], center=[50, 50])
    pie.add('', ['中雨', '小雨', '揚沙', '晴'], [lit_rain, mit_rain, sun, sail], radius=[10, 35], center=[50, 50], rosetype='area')

    pie.render('Pie-weather.html')

至此,pyecharts的大多數圖標的繪製我們都可以實現了,更多知識可以查看下面鏈接

鏈接:pyecharts


--------------------- 
作者:goodlovingz 
來源:CSDN 
原文:https://blog.csdn.net/qq_31967985/article/details/79975663 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章