Python echarts實現數據可視化實例詳解

1.概述

pyecharts 是百度開源的,適用於數據可視化的工具,配置靈活,展示圖表相對美觀,順滑。

2.安裝

python3環境下的安裝:

pip3 install pyecharts

3.數據可視化代碼

3.1 柱狀圖

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
    Bar()
    .add_xaxis(Faker.choose())
    .add_yaxis("商傢A", Faker.values(), stack="stack1")
    .add_yaxis("商傢B", Faker.values(), stack="stack1")
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆疊數據(全部)"))
    .render("bar_stack0.html")
)

執行上述代碼,會在相對目錄生成mycharts.html文件,通過頁面打開。

3.2 折線圖

import pyecharts.options as opts
from pyecharts.charts import Line
"""
Gallery 使用 pyecharts 1.1.0
參考地址: https://echarts.apache.org/examples/editor.html?c=line-smooth
目前無法實現的功能:
暫無
"""
x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
y_data = [820, 932, 901, 934, 1290, 1330, 1320]
(
    Line()
    .set_global_opts(
        tooltip_opts=opts.TooltipOpts(is_show=False),
        xaxis_opts=opts.AxisOpts(type_="category"),
        yaxis_opts=opts.AxisOpts(
            type_="value",
            axistick_opts=opts.AxisTickOpts(is_show=True),
            splitline_opts=opts.SplitLineOpts(is_show=True),
        ),
    )
    .add_xaxis(xaxis_data=x_data)
    .add_yaxis(
        series_name="",
        y_axis=y_data,
        symbol="emptyCircle",
        is_symbol_show=True,
        is_smooth=True,
        label_opts=opts.LabelOpts(is_show=False),
    )
    .render("smoothed_line_chart.html")
)

3.3 餅圖

from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
c = (
    Pie()
    .add(
        "",
        [list(z) for z in zip(Faker.choose(), Faker.values())],
        radius=["40%", "75%"],
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Pie-Radius"),
        legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .render("pie_radius.html")
)
 

官網:Document​

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容! 

推薦閱讀: