Python——視覺化神器pyecharts的正確開啟方式
Python——視覺化神器pyecharts的正確開啟方式
回家路上,無意中翻到了“python與演算法社群”公眾號中的一篇文章《pyecharts繪製出的4類驚豔動圖》,發現挺有意思。回家嘗試了下,效果不錯。也踩了一些坑,於是整理下,便於提升自己工作中的圖表視覺化能力(重點可用於PPT、週報、年終總結報告中)。
如果有幸能幫到你,點個贊吧,碼字不易。
1、Echarts的背景
Echarts 是一個由百度開源的資料視覺化工具,憑藉著良好的互動性與精巧的圖表設計,得到了眾多開發者的認可。 Python 是一門富有表達力的語言,很適合用於資料處理。當資料分析遇上資料視覺化時,pyecharts 就誕生了。目前,Echarts已支援:
- 圖表:30多種圖表
- 地圖:300多箇中國城市/ 200多個國家和地區
- 平臺:Pure Python / Jupyter Notebook / Web框架
下面是Echarts的幾個官網。
- pyecharts的Github:https://github.com/pyecharts/pyecharts/(包含安裝方法、簡介與部分demo)
- pyecharts的Demo:https://github.com/pyecharts/pyecharts-gallery(Python Echarts繪相簿,30+種圖表)
- ECHARTS官網:https://echarts.apache.org/examples/zh/#chart-type-scatter(視覺化JS工具)
其他關於pyecharts的部落格,可以參考:
注意:網上程式碼很多,有的程式碼跑不起是因為pyecharts版本存在差異。
2、安裝
以mac系統為例介紹,pyecharts的正確開啟姿勢。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts
安裝後,確認版本是1.8.1。:
Installing collected packages: pyecharts
Successfully installed pyecharts-1.8.1
3、demo
pyecharts支援兩種方式生成影像:生成html與直接生成影像。
(1)生成html,再用瀏覽器開啟
import datetime
import random
from pyecharts import options as opts
from pyecharts.charts import Calendar
begin = datetime.date(2017, 1, 1)
end = datetime.date(2017, 12, 31)
data = [
[str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)]
for i in range((end - begin).days + 1)
]
c = (
Calendar()
.add("", data, calendar_opts=opts.CalendarOpts(range_="2017"))
.set_global_opts(
title_opts=opts.TitleOpts(title="Calendar-2017年微信步數情況"),
visualmap_opts=opts.VisualMapOpts(
max_=20000,
min_=500,
orient="horizontal",
is_piecewise=True,
pos_top="230px",
pos_left="100px",
),
)
.render("calendar_base.html")
)
執行後,得到一個“calendar_base.html”檔案,然後用瀏覽器開啟,便可以得到:
(2)直接生成影像,示例如下:
from snapshot_selenium import snapshot as driver
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
def bar_chart() -> Bar:
c = (
Bar()
.add_xaxis(["襯衫", "毛衣", "領帶", "褲子", "風衣", "高跟鞋", "襪子"])
.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-測試渲染圖片"))
)
return c
# 需要安裝 snapshot-selenium 或者 snapshot-phantomjs
make_snapshot(driver, bar_chart().render(), "bar.png")
執行後,會得到一個"bar.png"圖片:
如果直接執行,報錯了,請耐心地往下面看。直接生成影像的demo,需要安裝snapshot-selenium 或者 snapshot-phantomjs。本文安裝的snapshot-selenium。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple snapshot_selenium
如果再執行,報了下面的錯誤,就得再辛苦一下咯。
selenium.common.exceptions.WebDriverException: Message: 'chromedriver'
解決方案簡化如下。(參考Mac安裝Chromedriver,命令列brew install chromedriver不得行,不用再去踩坑!!!)
- A. 開啟谷歌瀏覽器,在位址列輸入
chrome://version/
檢視到瀏覽器當前版本號,如我的mac電腦上谷歌瀏覽器版本是:
- B. 開啟谷歌瀏覽器驅動的下載網址:http://chromedriver.storage.googleapis.com/index.html,下載85.0.4183版本
- C. 選擇對應的系統,下載完後,解壓開啟,得到一個可執行檔案:chromedriver.exe
c1. 如果是Windows系統,把它放到下面兩個路徑下(":\Program Files (x86)\Google\Chrome\Application" 與python安裝的目錄下)
c2. 如果是Mac系統,把它放在"/usr/local/bin"下
- D. 再次點選執行,就可以正常出結果了。
相關文章
- Python log 的正確開啟方式Python
- IT部門資訊化正確開啟方式
- WikiPedia 的正確開啟方式
- 【Python視覺化】使用Pyecharts進行奧運會視覺化分析~Python視覺化Echarts
- 雲遊戲的正確開啟方式遊戲
- Java學習的正確開啟方式Java
- “布”道AI的正確開啟方式AI
- React之setState的正確開啟方式React
- 翻譯 | let 的正確開啟方式
- pyecharts做資料視覺化(二)Echarts視覺化
- Linux系統學習視窗的正確開啟方式Linux
- 中秋味的視覺化大屏 【以python pyecharts為工具】視覺化PythonEcharts
- 乾貨!這才是學習Python的正確開啟方式!Python
- Redis 分散式鎖的正確開啟方式Redis分散式
- 物聯網安全的正確開啟方式
- Python視覺化神器Yellowbrick使用Python視覺化
- [譯]響應式 Iframe - 正確的開啟方式
- 分散式鎖實現的正確開啟方式分散式
- vue3+ts開啟echarts的正確方式VueEcharts
- Android底部彈窗的正確開啟方式Android
- 以正確的方式開源 Python 專案Python
- 基於Redis分散式鎖的正確開啟方式Redis分散式
- [譯] 論 Android 中 Span 的正確開啟方式Android
- Vue_watch深度監聽的正確開啟方式Vue
- 這才是分散式事務的正確開啟方式!分散式
- TensorFlow中RNN實現的正確開啟方式RNN
- 論React js的正確開啟方式 管理系統介面模組化實踐ReactJS
- 視覺化執行Python的神器Jupyter Notebook視覺化Python
- 記錄Android Studio正確開啟方式Android
- Python資料視覺化工具pyecharts使用細則Python視覺化Echarts
- 【前端除錯】- 斷點除錯的正確開啟方式前端除錯斷點
- 【神器】視覺化建立骨架屏視覺化
- 等保2.0|這才是實施指南正確的開啟方式
- 這才是開啟風變程式設計的正確操作方式程式設計
- 【LBTC區塊鏈進階】分叉幣的正確開啟方式區塊鏈
- 【天行健】六西格瑪解決方案的正確開啟方式
- 軟體開發正確開啟方式:低程式碼+微服務微服務
- 推薦一款Python資料視覺化神器Python視覺化