Python——視覺化神器pyecharts的正確開啟方式

GeekZW發表於2020-09-24

                               Python——視覺化神器pyecharts的正確開啟方式

 

        回家路上,無意中翻到了“python與演算法社群”公眾號中的一篇文章《pyecharts繪製出的4類驚豔動圖》,發現挺有意思。回家嘗試了下,效果不錯。也踩了一些坑,於是整理下,便於提升自己工作中的圖表視覺化能力(重點可用於PPT、週報、年終總結報告中)。

        如果有幸能幫到你,點個贊吧,碼字不易。

 

1、Echarts的背景

        Echarts 是一個由百度開源的資料視覺化工具,憑藉著良好的互動性與精巧的圖表設計,得到了眾多開發者的認可。 Python 是一門富有表達力的語言,很適合用於資料處理。當資料分析遇上資料視覺化時,pyecharts 就誕生了。目前,Echarts已支援:

  • 圖表:30多種圖表
  • 地圖:300多箇中國城市/ 200多個國家和地區
  • 平臺:Pure Python / Jupyter Notebook / Web框架

下面是Echarts的幾個官網。

其他關於pyecharts的部落格,可以參考:

  1. pyecharts在手,天下我有(常用圖表篇下)(低版本)
  2. Python視覺化神器——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電腦上谷歌瀏覽器版本是:

  • C. 選擇對應的系統,下載完後,解壓開啟,得到一個可執行檔案:chromedriver.exe

c1. 如果是Windows系統,把它放到下面兩個路徑下(":\Program Files (x86)\Google\Chrome\Application"python安裝的目錄下

c2. 如果是Mac系統,把它放在"/usr/local/bin"

  • D. 再次點選執行,就可以正常出結果了。

 

相關文章