pyecharts 安裝及使用指南


一、概述

Charts是一個純Javascript的圖表庫,可以流暢的運行在PC和移動設備上,兼容當前絕大部分瀏覽器,底層依賴輕量級的Canvas類庫ZRender,提供直觀、生動、可交互、可高度個性化定制的數據可視化圖表。ECharts提供了常規的折線圖、柱狀圖、散點圖、餅圖、K線圖,用於統計的盒形圖,用於地理數據可視化的地圖、熱力圖、線圖,用於關系數據可視化的關系圖、treemap,多維數據可視化的平行坐標,還有用於BI的漏斗圖、儀表盤,並且支持圖與圖之間的混搭。

pyEcharts目前有0.5及以下版本和1.0以上版本,新版的pyecharts發生了許多變化。最為明顯的是以前調整變量的命令現在都發生了改變。width是舊版本中對圖表調整的參數,在新版本這一功能被調整到了option里面。網上大部分教程都是0.5及以下版本。

 

環境說明

python版本:3.7.9

操作系統:windows 10

開發工具:pycharm 2020

 

安裝

因此,這里直接使用最新版本來進行講解

pip3 install pyecharts

目前最新版本為1.8.1

 

自從 v0.3.2 開始,為了縮減項目本身的體積以及維持 pyecharts 項目的輕量化運行,pyecharts 將不再自帶地圖 js 文件。如用戶需要用到地圖圖表,可自行安裝對應的地圖文件包。下面介紹如何安裝。

  • 全球國家地圖: echarts-countries-pypkg (1.9MB): 世界地圖和 213 個國家,包括中國地圖
  • 中國省級地圖: echarts-china-provinces-pypkg (730KB):23 個省,5 個自治區
  • 中國市級地圖: echarts-china-cities-pypkg (3.8MB):370 個中國城市
  • 中國縣區級地圖: echarts-china-counties-pypkg (4.1MB):2882 個中國縣·區
  • 中國區域地圖: echarts-china-misc-pypkg (148KB):11 個中國區域地圖,比如華南、華北。

選擇自己需要的安裝

pip3 install echarts-countries-pypkg
pip3 install echarts-china-provinces-pypkg
pip3 install echarts-china-cities-pypkg
pip3 install echarts-china-counties-pypkg
pip3 install echarts-china-misc-pypkg
pip3 install echarts-united-kingdom-pypkg

 

 pyecharts 中文文檔:https://pyecharts.org/#/zh-cn/intro

 

二、柱狀圖

基本柱狀圖

示例代碼

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
import os

# 基礎數據
value = [40, 30, 26, 22, 15]
attr = ["貴陽市", "遵義市", "六盤水市", "安順市", "黔東南州"]

# 制作圖表
c = (
    # 定義Bar()柱狀圖
    Bar()
    # x坐標
    .add_xaxis(attr)
    # y坐標
    .add_yaxis("GDP", value)
    # 設置標題
    .set_global_opts(title_opts=opts.TitleOpts(title="貴州GDP柱狀圖", subtitle="副標題"))
    # 渲染網頁,輸出圖表的所有配置項
    .render()
)

# 打開網頁
os.system("render.html")
View Code

 

執行之后,會在當前目錄生成render.html文件,並會自動打開此網頁。

效果如下:

 

 

注意:執行代碼后,會輸出一段警告信息:

PendingDeprecationWarning: pyecharts 所有圖表類型將在 v1.9.0 版本開始強制使用 ChartItem 進行數據項配置 :)
  super().__init__(init_opts=init_opts)

這個可以忽略掉,因為目前最新版本是1.8.1,更高級的版本1.9.0還沒有出來,等新版本出來時,這個警告就會消失。

 

如果實在忍受不了這個提示,可以去到pyecharts庫下面的globals.py文件,找到_WarningControl類,將ShowWarning設置為False即可

參考鏈接:https://github.com/pyecharts/pyecharts/issues/1675

 

橫向柱狀圖

示例代碼

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
import os

# 基礎數據
city = ["貴陽市", "遵義市", "六盤水市", "安順市", "黔東南州"]
data1 = [40, 30, 26, 22, 15]
data2 = [13, 43, 32, 38, 20]

# 制作圖表
c = (
    # 定義Bar()柱狀圖
    Bar()
    # x坐標
    .add_xaxis(city)
    # y坐標
    .add_yaxis("2017年GD", data1)
    .add_yaxis("2016年GD", data2)
    # 翻轉 XY 軸
    .reversal_axis()
    .set_series_opts(label_opts=opts.LabelOpts(position="right"))
    # 設置標題
    .set_global_opts(title_opts=opts.TitleOpts(title="貴州GDP柱狀圖"))
    # 渲染網頁,輸出圖表的所有配置項
    .render()
)

# 打開網頁
os.system("render.html")
View Code

效果如下:

 

三、帶有漣漪特效動畫的散點圖

from pyecharts import options as opts
from pyecharts.charts import EffectScatter
from pyecharts.faker import Faker

c = (
    EffectScatter()
    .add_xaxis(Faker.choose())
    .add_yaxis("", Faker.values())
    .set_global_opts(title_opts=opts.TitleOpts(title="EffectScatter-基本示例"))
    .render()
)
# 打開網頁
os.system("render.html")
View Code

效果如下:

 

 

四、繪制3D圖形

3D折線圖

from pyecharts import options as opts
from pyecharts.charts import Line3D
from pyecharts.faker import Faker

data = [[1,2,3,4], [1,2,3,4], [0,4,8,16]]

c = (
    Line3D()
    .add(
        "",
        data,
        xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type_="value"),
        yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type_="value"),
        grid3d_opts=opts.Grid3DOpts(
            width=100, depth=100
        ),
    )
    .set_global_opts(
        # visualmap_opts=opts.VisualMapOpts(
        #     max_=30, min_=0, range_color=Faker.visual_color
        # ),
        title_opts=opts.TitleOpts(title="3D 折線圖示例"),
    )
    .render()
)

# 打開網頁
os.system("render.html")
View Code

效果如下:

 

3D散點圖

import pyecharts.options as opts
from pyecharts.charts 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']

c = (
    Scatter3D(
        init_opts=opts.InitOpts(width="1200px", height="600px")
    )  # bg_color="black"
    .add(
        series_name="3D 散點圖示例",
        data=data,
        xaxis3d_opts=opts.Axis3DOpts(
            # name=config_xAxis3D,
            type_="value",
            # textstyle_opts=opts.TextStyleOpts(color="#fff"),
        ),
        yaxis3d_opts=opts.Axis3DOpts(
            # name=config_yAxis3D,
            type_="value",
            # textstyle_opts=opts.TextStyleOpts(color="#fff"),
        ),
        zaxis3d_opts=opts.Axis3DOpts(
            # name=config_zAxis3D,
            type_="value",
            # textstyle_opts=opts.TextStyleOpts(color="#fff"),
        ),
        grid3d_opts=opts.Grid3DOpts(width=100, height=100, depth=100),
    )
    .set_global_opts(
        visualmap_opts=[
            opts.VisualMapOpts(
                type_="color",
                is_calculable=True,
                dimension=3,
                pos_top="10",
                max_=79 / 2,
                range_color=range_color,
            ),
            # opts.VisualMapOpts(
            #     type_="size",
            #     is_calculable=True,
            #     dimension=4,
            #     pos_bottom="10",
            #     max_=2.4 / 2,
            #     range_size=[10, 40],
            # ),
        ]
    )
    .render()
)
# # 打開網頁
os.system("render.html")
View Code

效果如下:

 

五、儀表盤

from pyecharts import options as opts
from pyecharts.charts import Gauge

c = (
    Gauge()
    .add("重大項目", [("投資占比", 66.66)])
    .set_global_opts(title_opts=opts.TitleOpts(title="儀表盤圖形"))
    .render()
)
# # 打開網頁
os.system("render.html")
View Code

效果如下:

 

六、水球圖

from pyecharts import options as opts
from pyecharts.charts import Liquid
import os

c = (
    Liquid()
    .add("Liquid", [0.8])
    .set_global_opts(title_opts=opts.TitleOpts(title="水球圖"))
    .render()
)
# # 打開網頁
os.system("render.html")
View Code

效果如下:

 

 

講到這里基本的圖形講解完畢,更多知識推薦大家結合實際應用研究

 

本文參考鏈接:

https://www.cnblogs.com/dgwblog/p/11811562.html


免責聲明!

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



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