Python數據可視化之Pyecharts_example

Pyecharts_example源碼
p

導入模塊

from pyecharts import options as opts
from pyecharts.charts import Bar, Pie, Geo, Map, BMap, Page, WordCloud, Grid, Line, Scatter, Kline
from pyecharts.globals import ThemeType, ChartType, SymbolType, CurrentConfig
from pyecharts.faker import Faker
from pyecharts.commons.utils import JsCode
# CurrentConfig.ONLINE_HOST = 'E:/Software/pyecharts-assets-master/assets/'
import os

os.chdir(r"E:\Python\教程\pyecharts-master\pyecharts-master\example")
os.getcwd()
# E:\Python\教程\pyecharts-master\pyecharts-master\example
'E:\\Python\\教程\\pyecharts-master\\pyecharts-master\\example'

基本圖形

Calendar:日曆圖

Calendar-2017年微信步數情況

import datetime
import random

from pyecharts.charts import Calendar

def calendar_base() -> 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",
            ),
        )
    )
    return c
calendar_base().render_notebook()
    <div id="3712fe5fa9b54dd78ae381cfe27eea2b" style="width:900px; height:500px;"></div>

Funnel:漏斗圖

Funnel-基本示例

from pyecharts.charts import Funnel, Page


def funnel_base() -> Funnel:
    c = (
        Funnel()
        .add("商品", [list(z) for z in zip(Faker.choose(), Faker.values())])
        .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-基本示例"))
    )
    return c
funnel_base().render_notebook()
    <div id="8357aea292724d74b2d1cb79fc5fa181" style="width:900px; height:500px;"></div>

Funnel-Label(inside)

def funnel_label_inside() -> Funnel:
    c = (
        Funnel()
        .add(
            "商品",
            [list(z) for z in zip(Faker.choose(), Faker.values())],
            label_opts=opts.LabelOpts(position="inside"),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-Label(inside)"))
    )
    return c
funnel_label_inside().render_notebook()
    <div id="4603b36bb46a46c7bf22f2cd31cad895" style="width:900px; height:500px;"></div>

Funnel-Sort(ascending)

def funnel_sort_ascending() -> Funnel:
    c = (
        Funnel()
        .add(
            "商品",
            [list(z) for z in zip(Faker.choose(), Faker.values())],
            sort_="ascending",
            label_opts=opts.LabelOpts(position="inside"),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-Sort(ascending)"))
    )
    return c
funnel_sort_ascending().render_notebook()
    <div id="1bc78eb05a9c44d9a634ea5b96a5f94b" style="width:900px; height:500px;"></div>

Gauge:儀表盤

Gauge-基本示例

from pyecharts.charts import Gauge, Page


def gauge_base() -> Gauge:
    c = (
        Gauge()
        .add("", [("完成率", 66.6)])
        .set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))
    )
    return c
gauge_base().render_notebook()
    <div id="2831bc58eff941dc8e6b5e773730793b" style="width:900px; height:500px;"></div>

Gauge-不同顏色

def gauge_color() -> Gauge:
    c = (
        Gauge()
        .add(
            "業務指標",
            [("完成率", 55.5)],
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(
                    color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30
                )
            ),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Gauge-不同顏色"),
            legend_opts=opts.LegendOpts(is_show=False),
        )
    )
    return c
gauge_color().render_notebook()
    <div id="a07d809b0db44dcfb575ec5dd37a8b12" style="width:900px; height:500px;"></div>

Gauge-分割段數-Label

def gauge_splitnum_label() -> Gauge:
    c = (
        Gauge()
        .add(
            "業務指標",
            [("完成率", 55.5)],
            split_number=5,
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(
                    color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30
                )
            ),
#             label_opts=opts.LabelOpts(formatter="{value}"),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Gauge-分割段數-Label"),
            legend_opts=opts.LegendOpts(is_show=False),
        )
    )
    return c
gauge_splitnum_label().render_notebook()
    <div id="a53ebb9a4077485ba3e699ed3a76ceae" style="width:900px; height:500px;"></div>

Gauge-改變輪盤內的字體

def gauge_label_title_setting() -> Gauge:
    c = (
        Gauge()
        .add(
            "",
            [("完成率", 66.6)],
            title_label_opts=opts.LabelOpts(
                font_size=40, color="blue", font_family="Microsoft YaHei"
            ),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="Gauge-改變輪盤內的字體"))
    )
    return c
gauge_label_title_setting().render_notebook()
    <div id="125298ae062447a58414cbbb669fcfad" style="width:900px; height:500px;"></div>

Graph:關係圖

Graph-基本示例

import json
import os

from pyecharts.charts import Graph, Page

def graph_base() -> Graph:
    nodes = [
        {"name": "結點1", "symbolSize": 10},
        {"name": "結點2", "symbolSize": 20},
        {"name": "結點3", "symbolSize": 30},
        {"name": "結點4", "symbolSize": 40},
        {"name": "結點5", "symbolSize": 50},
        {"name": "結點6", "symbolSize": 40},
        {"name": "結點7", "symbolSize": 30},
        {"name": "結點8", "symbolSize": 20},
    ]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get("name"), "target": j.get("name")})
    c = (
        Graph()
        .add("", nodes, links, repulsion=8000)
        .set_global_opts(title_opts=opts.TitleOpts(title="Graph-基本示例"))
    )
    return c
graph_base().render_notebook()
    <div id="d71a48116e9940c889ed0e3cb688e09f" style="width:900px; height:500px;"></div>

Graph-GraphNode-GraphLink

def graph_with_opts() -> Graph:
    nodes = [
        opts.GraphNode(name="結點1", symbol_size=10),
        opts.GraphNode(name="結點2", symbol_size=20),
        opts.GraphNode(name="結點3", symbol_size=30),
        opts.GraphNode(name="結點4", symbol_size=40),
        opts.GraphNode(name="結點5", symbol_size=50),
    ]
    links = [
        opts.GraphLink(source="結點1", target="結點2"),
        opts.GraphLink(source="結點2", target="結點3"),
        opts.GraphLink(source="結點3", target="結點4"),
        opts.GraphLink(source="結點4", target="結點5"),
        opts.GraphLink(source="結點5", target="結點1"),
    ]
    c = (
        Graph()
        .add("", nodes, links, repulsion=4000)
        .set_global_opts(title_opts=opts.TitleOpts(title="Graph-GraphNode-GraphLink"))
    )
    return c
graph_with_opts().render_notebook()
    <div id="df812eb5762f42cd97887603469cc042" style="width:900px; height:500px;"></div>

Graph-GraphNode-GraphLink-WithEdgeLabel

def graph_with_edge_opts() -> Graph:
    nodes_data = [
        opts.GraphNode(name="結點1", symbol_size=10),
        opts.GraphNode(name="結點2", symbol_size=20),
        opts.GraphNode(name="結點3", symbol_size=30),
        opts.GraphNode(name="結點4", symbol_size=40),
        opts.GraphNode(name="結點5", symbol_size=50),
        opts.GraphNode(name="結點6", symbol_size=60),
    ]
    links_data = [
        opts.GraphLink(source="結點1", target="結點2", value=2),
        opts.GraphLink(source="結點2", target="結點3", value=3),
        opts.GraphLink(source="結點3", target="結點4", value=4),
        opts.GraphLink(source="結點4", target="結點5", value=5),
        opts.GraphLink(source="結點5", target="結點6", value=6),
        opts.GraphLink(source="結點6", target="結點1", value=7),
    ]
    c = (
        Graph()
        .add(
            "",
            nodes_data,
            links_data,
            repulsion=4000,
            edge_label=opts.LabelOpts(
                is_show=True,
                position="middle",
                formatter="{b} 的數據 {c}",
            ),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Graph-GraphNode-GraphLink-WithEdgeLabel")
        )
    )
    return c
graph_with_edge_opts().render_notebook()
    <div id="67f0b0da310145519880384a07f86221" style="width:900px; height:500px;"></div>

Graph-微博轉發關係圖

def graph_weibo() -> Graph:
    with open(os.path.join("fixtures", "weibo.json"), "r", encoding="utf-8") as f:
        j = json.load(f)
        nodes, links, categories, cont, mid, userl = j
    c = (
        Graph()
        .add(
            "",
            nodes,
            links,
            categories,
            repulsion=50,
            linestyle_opts=opts.LineStyleOpts(curve=0.2),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .set_global_opts(
            legend_opts=opts.LegendOpts(is_show=False),
            title_opts=opts.TitleOpts(title="Graph-微博轉發關係圖"),
        )
    )
    return c
graph_weibo().render_notebook()
    <div id="5b2d56203c6b433a96b422e5703f7ceb" style="width:900px; height:500px;"></div>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章