python可視化pyecharts

python可視化pyecharts

 

簡單介紹

pyecharts 是一個用於生成 Echarts 圖表的類庫。Echarts 是百度開源的一個數據可視化 JS 庫。用 Echarts 生成的圖可視化效果很是棒,爲了與 Python 進行對接,方便在 Python 中直接使用數據生成圖。
echartsjs首頁:https://www.echartsjs.com/index.html
pyecharts首頁:http://pyecharts.herokuapp.com/
pyecharts 開發文檔:http://pyecharts.org/#/

 

 

渲染圖表

 

安裝 pyecharts

pip 安裝html

$ pip install pyecharts

源碼安裝node

$ git clone https://github.com/pyecharts/pyecharts.git
$ cd pyecharts
$ pip install -r requirements.txt
$ python setup.py install

兼容性注意python

pyecharts 支持 Python2.7+ 和 Ptyhon3.5+。若是你使用的是 Python2.7,請在代碼頂部聲明字符編碼,不然會出現中文亂碼問題。

#coding=utf-8
from __future__ import unicode_literals
注意

 

快速開始

首先開始來繪製你的第一個圖表git

from pyecharts import Bar

bar = Bar("個人第一個圖表", "這裏是副標題")
bar.add("服裝", ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"], [5, 20, 36, 10, 75, 90])
# bar.print_echarts_options() # 該行只爲了打印配置項,方便調試時使用
bar.render()    # 生成本地 HTML 文件

 

 運行程序報錯github

報錯信息
ERROR:lml.utils:failed to import pyecharts_snapshot
Traceback (most recent call last):
  File "D:\Envs\mytest\lib\site-packages\lml\utils.py", line 43, in do_import
    plugin_module = __import__(plugin_module_name)
ModuleNotFoundError: No module named 'pyecharts_snapshot'


緣由:缺乏這個依賴包
解決辦法
pip install pyecharts-snapshot

 

再次運行程序,程序執行成功會在同級目錄下生成一個html文件npm

add()
主要方法,用於添加圖表的數據和設置各類配置項
print_echarts_options()
打印輸出圖表的全部配置項
render()
默認將會在根目錄下生成一個 render.html 的文件,支持 path 參數,設置文件保存位置,如 render(r"e:\my_first_chart.html"),文件用瀏覽器打開。
Note: 能夠按右邊的下載按鈕將圖片下載到本地,若是想要提供更多實用工具按鈕,請在 add() 中設置 is_more_utils 爲 True

from pyecharts import Bar

bar = Bar("個人第一個圖表", "這裏是副標題")
bar.add("服裝", 
        ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"], [5, 20, 36, 10, 75, 90],
        is_more_utils=True)
bar.render()
詳解

 

使用主題

自 0.5.2+ 起,pyecharts 支持更換主體色系。下面是跟換爲 'dark' 的例子:json

import random

from pyecharts import Bar


X_AXIS = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
bar = Bar("個人第一個圖表", "這裏是副標題")
bar.use_theme("dark")
bar.add("商家A", X_AXIS, [random.randint(10, 100) for _ in range(6)])
bar.add("商家B", X_AXIS, [random.randint(10, 100) for _ in range(6)])
bar.add("商家C", X_AXIS, [random.randint(10, 100) for _ in range(6)])
bar.add("商家D", X_AXIS, [random.randint(10, 100) for _ in range(6)])
bar.render()

 

默認主題的效果,就是不設置主題的時候瀏覽器

 

 若是咱們要使用更多的主題,就須要安裝echarts-themes-pypkg庫,由於echarts 自帶 dark 主題,pyecharts 也就自帶了 darkapp

echarts-themes-pypkgecharts

vintage
macarons
infographic
shine
roma
westeros
wonderland
chalk
halloween
essos
walden
purple-passion
romantic
更多主題

 

安裝主題插件

pip install echarts-themes-pypkg

使用主題

更換單個圖形主題

bar.use_theme("vintage")
更換運行環境內全部圖表主題

from pyecharts import configure

# 將這行代碼置於首部
configure(global_theme='dark')

bar = Bar()
# 其餘代碼

使用本身構建的主題

Echarts 提供了主題構建工具,你能夠從中構建喜歡的主題,如 myTheme.js。而後 hack echarts-themes-pypkg 包。具體操做以下

1.cd 到你 Python 安裝環境下的 Lib/site-packages/echarts_themes_pypkg/resources 目錄下,具體路徑因操做系統而異
2.將 myTheme.js 放入到 resources/echarts-themes-js 文件夾下
3.改動 resources/registry.json 文件
 "PINYIN_MAP": {
        "shine": "shine",
        ...
        "myTheme": "myTheme"    # 這行
    },
    "FILE_MAP": {
        "shine": "shine",
        ...
        "myTheme": "myTheme"    # 還有這行
    }
1.cd 到 notebook 安裝環境下的 jupyter/nbextensions/echarts-themes-js 目錄下,具體路徑因操做系統而異
2.將 myTheme.js 放入到 echarts-themes-js 文件夾下
3.使用 chart.use_theme("myTheme")
四、5 爲可選項,若是不使用 notebook 的話能夠忽略該步驟。
使用本身構建的主題

 

 

使用 pyecharts-snapshot 插件

若是想直接將圖片保存爲 png, pdf, gif 格式的文件,可使用 pyecharts-snapshot。使用該插件請確保你的系統上已經安裝了 Nodejs 環境。

  1. 安裝 phantomjs $ npm install -g phantomjs-prebuilt
  2. 安裝 pyecharts-snapshot $ pip install pyecharts-snapshot
  3. 調用 render 方法 bar.render(path='snapshot.png') 文件結尾能夠爲 svg/jpeg/png/pdf/gif。請注意,svg 文件須要你在初始化 bar 的時候設置 renderer='svg'。

更多內容請移步至 pyecharts-snapshot

 

圖形繪製過程

圖表類提供了若干了構建和渲染的方法,在使用的過程當中,建議按照如下的順序分別調用:

步驟 描述 代碼示例 備註
1 實例一個具體類型圖表的對象 chart = FooChart()  
2 爲圖表添加通用的配置,如主題 chart.use_theme()  
3 爲圖表添加特定的配置 geo.add_coordinate()  
4 添加數據及配置項 chart.add() 參考 數據解析與導入篇
5 生成本地文件(html/svg/jpeg/png/pdf/gif) chart.render()

 

從 v0.5.9 開始,以上涉及的方法均支持鏈式調用。例如:

from pyecharts import Bar

CLOTHES = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
clothes_v1 = [5, 20, 36, 10, 75, 90]
clothes_v2 = [10, 25, 8, 60, 20, 80]

(Bar("柱狀圖數據堆疊示例")
    .add("商家A", CLOTHES, clothes_v1, is_stack=True)
    .add("商家B", CLOTHES, clothes_v2, is_stack=True)
    .render())

 

 

屢次顯示圖表

 從 v0.4.0+ 開始,pyecharts 重構了渲染的內部邏輯,改善效率。推薦使用如下方式顯示多個圖表。

 

from pyecharts import Bar, Line
from pyecharts.engine import create_default_environment

bar = Bar("個人第一個圖表", "這裏是副標題")
bar.add("服裝", ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"], [5, 20, 36, 10, 75, 90])

line = Line("個人第一個圖表", "這裏是副標題")
line.add("服裝", ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"], [5, 20, 36, 10, 75, 90])

env = create_default_environment("html")
# 爲渲染建立一個默認配置環境
# create_default_environment(filet_ype)
# file_type: 'html', 'svg', 'png', 'jpeg', 'gif' or 'pdf'

env.render_chart_to_file(bar, path='bar.html')
env.render_chart_to_file(line, path='line.html')

相比第一個例子,該代碼只是使用同一個引擎對象,減小了部分重複操做,速度有所提升。

 

 

Pandas&Numpy 簡單示例

 若是使用的是 Numpy 或者 Pandas,能夠參考這個示例

 

Note: 使用 Pandas&Numpy 時,整數類型請確保爲 int,而不是 numpy.int32

固然你也能夠採用更加酷炫的方式,使用 Jupyter Notebook 來展現圖表,matplotlib 有的,pyecharts 也會有的

Note: 從 v0.1.9.2 版本開始,廢棄 render_notebook() 方法,現已採用更加 pythonic 的作法。直接調用自己實例就能夠了。

 好比這樣

還有這樣

 

若是使用的是自定義類,直接調用自定義類示例便可

更多 Jupyter notebook 的例子請參考 notebook-use-cases。可下載後運行看看。

如需使用 Jupyter Notebook 來展現圖表,只須要調用自身實例便可,同時兼容 Python2 和 Python3 的 Jupyter Notebook 環境。全部圖表都可正常顯示,與瀏覽器一致的交互體驗,這下展現報告連 PPT 都省了!!

相關文章
相關標籤/搜索