Pyecharts Practice

1 快速开始

import pyecharts
# 查看pyecharts版本
print(pyecharts.__version__)

1.1 绘制图表

from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
# bar.render()用于在本地生成html文件
# bar,render_notebook()只在notebook里生成图片
bar.render_notebook()
柱状图

1.2 使用options配置项

from pyecharts.charts import Bar
from pyecharts import options as opts

bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
#bar.render()
bar.render_notebook()
柱状图

1.3 渲染图片

from pyecharts.charts import Bar
from pyecharts.render import make_snapshot

# 使用 snapshot-selenium 渲染图片
from snapshot_selenium import snapshot

bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
# 将网页保存成图片存在本地
make_snapshot(snapshot, bar.render(), "bar.png")

1.4 使用主题

from pyecharts.charts import Bar
from pyecharts import options as opts
# 内置主题类型可查看 pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType

bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.add_yaxis("商家B", [15, 6, 45, 20, 35, 66])
bar.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
#bar.render()
bar.render_notebook()
柱状图

2 图表类型

2.1 基本图表

2.1.1 漏斗图

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Funnel, Page

funnel = Funnel()
funnel.add("商品", [list(z) for z in zip(Faker.choose(), Faker.values())])
funnel.set_global_opts(title_opts=opts.TitleOpts(title="漏斗图-基本示例"))
funnel.render_notebook()
漏斗图
# 标签内置
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Funnel, Page

funnel = Funnel()
funnel.add("商品", [list(z) for z in zip(Faker.choose(), Faker.values())],label_opts=opts.LabelOpts(position="inside"))
funnel.set_global_opts(title_opts=opts.TitleOpts(title="漏斗图(标签内置)"))
funnel.render_notebook()
漏斗图
# 倒置
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Funnel, Page

funnel = Funnel()
funnel.add("商品", [list(z) for z in zip(Faker.choose(), Faker.values())],sort_="ascending",label_opts=opts.LabelOpts(position="inside"))
funnel.set_global_opts(title_opts=opts.TitleOpts(title="漏斗图(倒置)"))
funnel.render_notebook()
漏斗图

2.1.2 仪表盘

from pyecharts import options as opts
from pyecharts.charts import Gauge, Page

base_gauge = Gauge()
base_gauge.add("", [("完成率", 66.6)])
base_gauge.render_notebook()
仪表盘
from pyecharts import options as opts
from pyecharts.charts import Gauge, Page

base_gauge = Gauge()
base_gauge.add("业务指标",[("完成率", 55.5)],axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30)))
base_gauge.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-不同颜色"),legend_opts=opts.LegendOpts(is_show=False))
base_gauge.render_notebook()
仪表盘

2.1.3 水球图

from pyecharts import options as opts
from pyecharts.charts import Liquid, Page

liquid_base = Liquid()
liquid_base.add('lq',[0.6,0.7])
liquid_base.set_global_opts(title_opts=opts.TitleOpts(title="水球图-基本示例"))
liquid_base.render_notebook()
水球图

2.1.4 饼图

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Pie

base_pie = Pie()
base_pie.add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
base_pie.set_global_opts(title_opts=opts.TitleOpts(title="饼图-基本示例"))
base_pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
base_pie.render_notebook()
饼图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Pie

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

rose_pie = Pie()
rose_pie.add(
            "",
            [list(z) for z in zip(Faker.choose(), Faker.values())],
            radius=["30%", "75%"],
            center=["25%", "50%"],
            rosetype="radius",
            label_opts=opts.LabelOpts(is_show=False),
        )
rose_pie.render_notebook()
玫瑰图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Pie

rose_pie = Pie()
rose_pie.add(
            "",
            [list(z) for z in zip(Faker.choose(), Faker.values())],
            radius=["30%", "75%"],
            center=["25%", "50%"],
            rosetype="area",
        )
rose_pie.set_global_opts(title_opts=opts.TitleOpts(title="玫瑰图示例"))
rose_pie.render_notebook()
玫瑰图

2.1.5 雷达图

from pyecharts import options as opts
from pyecharts.charts import Radar

v1 = [[4300, 10000, 28000, 35000, 50000, 19000]]
v2 = [[5000, 14000, 28000, 31000, 42000, 21000]]
base_radar = Radar()
base_radar.add_schema(schema=[
                opts.RadarIndicatorItem(name="销售", max_=6500),
                opts.RadarIndicatorItem(name="管理", max_=16000),
                opts.RadarIndicatorItem(name="信息技术", max_=30000),
                opts.RadarIndicatorItem(name="客服", max_=38000),
                opts.RadarIndicatorItem(name="研发", max_=52000),
                opts.RadarIndicatorItem(name="市场", max_=25000),
                ])
base_radar.add("预算分配",v1,color="#f9713c")
base_radar.add("实际开销",v2,color="#b3e4a1")
base_radar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
base_radar.set_global_opts(title_opts=opts.TitleOpts(title="雷达图-基本示例"))
base_radar.render_notebook()
雷达图

2.1.6 主题河流图

from pyecharts import options as opts
from pyecharts.charts import Page, ThemeRiver

data = [
        ["2015/11/08", 10, "DQ"],
        ["2015/11/09", 15, "DQ"],
        ["2015/11/10", 35, "DQ"],
        ["2015/11/14", 7, "DQ"],
        ["2015/11/15", 2, "DQ"],
        ["2015/11/16", 17, "DQ"],
        ["2015/11/17", 33, "DQ"],
        ["2015/11/18", 40, "DQ"],
        ["2015/11/19", 32, "DQ"],
        ["2015/11/20", 26, "DQ"],
        ["2015/11/08", 35, "TY"],
        ["2015/11/09", 36, "TY"],
        ["2015/11/10", 37, "TY"],
        ["2015/11/11", 22, "TY"],
        ["2015/11/12", 24, "TY"],
        ["2015/11/13", 26, "TY"],
        ["2015/11/14", 34, "TY"],
        ["2015/11/15", 21, "TY"],
        ["2015/11/16", 18, "TY"],
        ["2015/11/17", 45, "TY"],
        ["2015/11/18", 32, "TY"],
        ["2015/11/19", 35, "TY"],
        ["2015/11/20", 30, "TY"],
        ["2015/11/08", 21, "SS"],
        ["2015/11/09", 25, "SS"],
        ["2015/11/10", 27, "SS"],
        ["2015/11/11", 23, "SS"],
        ["2015/11/12", 24, "SS"],
        ["2015/11/13", 21, "SS"],
        ["2015/11/14", 35, "SS"],
        ["2015/11/15", 39, "SS"],
        ["2015/11/16", 40, "SS"],
        ["2015/11/17", 36, "SS"],
        ["2015/11/18", 33, "SS"],
        ["2015/11/19", 43, "SS"],
        ["2015/11/20", 40, "SS"],
        ["2015/11/14", 7, "QG"],
        ["2015/11/15", 2, "QG"],
        ["2015/11/16", 17, "QG"],
        ["2015/11/17", 33, "QG"],
        ["2015/11/18", 40, "QG"],
        ["2015/11/19", 32, "QG"],
        ["2015/11/20", 26, "QG"],
        ["2015/11/21", 35, "QG"],
        ["2015/11/22", 40, "QG"],
        ["2015/11/23", 32, "QG"],
        ["2015/11/24", 26, "QG"],
        ["2015/11/25", 22, "QG"],
        ["2015/11/08", 10, "SY"],
        ["2015/11/09", 15, "SY"],
        ["2015/11/10", 35, "SY"],
        ["2015/11/11", 38, "SY"],
        ["2015/11/12", 22, "SY"],
        ["2015/11/13", 16, "SY"],
        ["2015/11/14", 7, "SY"],
        ["2015/11/15", 2, "SY"],
        ["2015/11/16", 17, "SY"],
        ["2015/11/17", 33, "SY"],
        ["2015/11/18", 40, "SY"],
        ["2015/11/19", 32, "SY"],
        ["2015/11/20", 26, "SY"],
        ["2015/11/21", 35, "SY"],
        ["2015/11/22", 4, "SY"],
        ["2015/11/23", 32, "SY"],
        ["2015/11/24", 26, "SY"],
        ["2015/11/25", 22, "SY"],
        ["2015/11/08", 10, "DD"],
        ["2015/11/09", 15, "DD"],
        ["2015/11/10", 35, "DD"],
        ["2015/11/11", 38, "DD"],
        ["2015/11/12", 22, "DD"],
        ["2015/11/13", 16, "DD"],
        ["2015/11/14", 7, "DD"],
        ["2015/11/15", 2, "DD"],
        ["2015/11/16", 17, "DD"],
        ["2015/11/17", 33, "DD"],
        ["2015/11/18", 4, "DD"],
        ["2015/11/19", 32, "DD"],
        ["2015/11/20", 26, "DD"],
    ]
theme_river = ThemeRiver()
theme_river.add(["DQ", "TY", "SS", "QG", "SY", "DD"],data,
            singleaxis_opts=opts.SingleAxisOpts(type_="time", pos_bottom="10%"))
theme_river.set_global_opts(title_opts=opts.TitleOpts(title="主题河流图-基本示例"))
theme_river.render_notebook()
主题河流图

2.1.7 词云图

from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType


words = [
    ("Sam S Club", 10000),
    ("Macys", 6181),
    ("Amy Schumer", 4386),
    ("Jurassic World", 4055),
    ("Charter Communications", 2467),
    ("Chick Fil A", 2244),
    ("Planet Fitness", 1868),
    ("Pitch Perfect", 1484),
    ("Express", 1112),
    ("Home", 865),
    ("Johnny Depp", 847),
    ("Lena Dunham", 582),
    ("Lewis Hamilton", 555),
    ("KXAN", 550),
    ("Mary Ellen Mark", 462),
    ("Farrah Abraham", 366),
    ("Rita Ora", 360),
    ("Serena Williams", 282),
    ("NCAA baseball tournament", 273),
    ("Point Break", 265),
]
base_wordcloud = WordCloud()
base_wordcloud.add("", words, word_size_range=[20, 100])
base_wordcloud.set_global_opts(title_opts=opts.TitleOpts(title="词云图-基本示例"))
base_wordcloud.render_notebook()
词云图
from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType


words = [
    ("Sam S Club", 10000),
    ("Macys", 6181),
    ("Amy Schumer", 4386),
    ("Jurassic World", 4055),
    ("Charter Communications", 2467),
    ("Chick Fil A", 2244),
    ("Planet Fitness", 1868),
    ("Pitch Perfect", 1484),
    ("Express", 1112),
    ("Home", 865),
    ("Johnny Depp", 847),
    ("Lena Dunham", 582),
    ("Lewis Hamilton", 555),
    ("KXAN", 550),
    ("Mary Ellen Mark", 462),
    ("Farrah Abraham", 366),
    ("Rita Ora", 360),
    ("Serena Williams", 282),
    ("NCAA baseball tournament", 273),
    ("Point Break", 265),
]
base_wordcloud = WordCloud()
base_wordcloud.add("", words, word_size_range=[20, 100],shape=SymbolType.DIAMOND)
base_wordcloud.set_global_opts(title_opts=opts.TitleOpts(title="词云图-基本示例"))
base_wordcloud.render_notebook()
钻石型词云图

2.2 直角坐标系图表

2.2.1 柱状图/条形图

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values())
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-基本示例",subtitle="此处是副标题"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values(),is_selected=False)
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-默认取消显示某Series",subtitle="此处是副标题"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values())
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-显示ToolBox",subtitle="此处是副标题"),
                   toolbox_opts=opts.ToolboxOpts(),
                   legend_opts=opts.LegendOpts(is_show=False))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values(),category_gap="40%")
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-单系列柱间距离"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values())
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-Y 轴 formatter"),
                   yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value}/月")))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values())
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-XY轴名称"),
                   xaxis_opts=opts.AxisOpts(name="此处是X轴"),
                   yaxis_opts=opts.AxisOpts(name="此处是Y轴"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values())
bar.reversal_axis()
bar.set_series_opts(label_opts=opts.LabelOpts(position="right"))
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-翻转XY轴"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values(),stack="stack1")
bar.add_yaxis("商家B",Faker.values(),stack="stack1")
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
bar.set_global_opts(title_opts=opts.TitleOpts(title="堆叠柱状图"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values(),stack="stack1")
bar.add_yaxis("商家B",Faker.values(),stack="stack1")
bar.add_yaxis("商家C",Faker.values())
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
bar.set_global_opts(title_opts=opts.TitleOpts(title="(部分系列)堆叠柱状图"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values())
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
                   markpoint_opts=opts.MarkPointOpts(
                   data=[
                       opts.MarkPointItem(type_="max",name="最大值"),
                       opts.MarkPointItem(type_="min",name="最小值")
                   ]))
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-指定类型标记点"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
x,y = Faker.choose(),Faker.values()
bar.add_xaxis(x)
bar.add_yaxis("商家A",y,markpoint_opts=opts.MarkPointOpts(
                   data=[
                       opts.MarkPointItem(name="自定义标记点",coord=[x[2],y[2]],value=y[2])
                   ]))
bar.add_yaxis("商家B",Faker.values())
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-指定类型标记点"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values())
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
                   markline_opts=opts.MarkLineOpts(
                   data=[
                       opts.MarkLineItem(type_="max",name="最大值"),
                       opts.MarkLineItem(type_="min",name="最小值")
                   ]))
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-指定类型标记线"))
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.days_attrs)
bar.add_yaxis("商家A",Faker.days_values)
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-水平数据缩放"),
                   datazoom_opts=opts.DataZoomOpts())
bar.render_notebook()
柱状图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(Faker.days_attrs)
bar.add_yaxis("商家A",Faker.days_values)
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图-水平数据缩放"),
                   datazoom_opts=opts.DataZoomOpts(orient="vertical"))
bar.render_notebook()
柱状图
from pyecharts import options as opts
from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis([
                "名字很长的X轴标签1",
                "名字很长的X轴标签2",
                "名字很长的X轴标签3",
                "名字很长的X轴标签4",
                "名字很长的X轴标签5",
                "名字很长的X轴标签6",
            ])
bar.add_yaxis("商家A", [10, 20, 30, 40, 50, 40])
bar.add_yaxis("商家B", [20, 10, 40, 30, 40, 50])
bar.set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
                   title_opts=opts.TitleOpts(title="柱状图-旋转x轴标签",subtitle="解决标签过长问题"))
bar.render_notebook()
柱状图

2.2.2 箱型图

from pyecharts import options as opts
from pyecharts.charts import Boxplot

v1 = [
        [850, 740, 900, 1070, 930, 850, 950, 980, 980, 880]
        + [1000, 980, 930, 650, 760, 810, 1000, 1000, 960, 960],
        [960, 940, 960, 940, 880, 800, 850, 880, 900]
        + [840, 830, 790, 810, 880, 880, 830, 800, 790, 760, 800],
    ]
v2 = [
        [890, 810, 810, 820, 800, 770, 760, 740, 750, 760]
        + [910, 920, 890, 860, 880, 720, 840, 850, 850, 780],
        [890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870]
        + [870, 810, 740, 810, 940, 950, 800, 810, 870],
    ]

box_plot = Boxplot()
box_plot.add_xaxis(["expr1", "expr2"])
box_plot.add_yaxis("A", box_plot.prepare_data(v1))
box_plot.add_yaxis("B", box_plot.prepare_data(v2))
box_plot.set_global_opts(title_opts=opts.TitleOpts(title="箱型图-基本示例"))
box_plot.render_notebook()
箱型图

2.2.3 涟漪特效散点图

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import EffectScatter
from pyecharts.globals import SymbolType

effect_scatter = EffectScatter()
effect_scatter.add_xaxis(Faker.choose())
effect_scatter.add_yaxis("",Faker.values())
effect_scatter.set_global_opts(title_opts=opts.TitleOpts(title="涟漪特效散点图-基本示例"))
effect_scatter.render_notebook()
散点图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import EffectScatter
from pyecharts.globals import SymbolType

effect_scatter = EffectScatter()
effect_scatter.add_xaxis(Faker.choose())
effect_scatter.add_yaxis("",Faker.values())
effect_scatter.set_global_opts(title_opts=opts.TitleOpts(title="涟漪特效散点图-显示网格线"),
                              xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
                              yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)))
effect_scatter.render_notebook()
散点图

2.2.4 折线图/面积图

import pyecharts.options as opts
from pyecharts.faker import  Faker
from pyecharts.charts import Line

line_base = Line()
line_base.add_xaxis(Faker.choose())
line_base.add_yaxis("商家A",Faker.values())
line_base.add_yaxis("商家B",Faker.values())
line_base.set_global_opts(title_opts=opts.TitleOpts(title="折线图-基本示例"))
line_base.render_notebook()
折线图
import pyecharts.options as opts
from pyecharts.faker import  Faker
from pyecharts.charts import Line

line = Line()
y =Faker.values()
y[3],y[6]=None,None
line.add_xaxis(Faker.choose())
line.add_yaxis("商家A",y,is_connect_nones=True)
line.set_global_opts(title_opts=opts.TitleOpts(title="折线图-连接空值"))
line.render_notebook()
折线图
import pyecharts.options as opts
from pyecharts.faker import  Faker
from pyecharts.charts import Line

line_base = Line()
line_base.add_xaxis(Faker.choose())
line_base.add_yaxis("商家A",Faker.values(),is_smooth=True)
line_base.add_yaxis("商家B",Faker.values(),is_smooth=True)
line_base.set_global_opts(title_opts=opts.TitleOpts(title="折线图-平滑曲线"))
line_base.render_notebook()
折线图
import pyecharts.options as opts
from pyecharts.faker import  Faker
from pyecharts.charts import Line

line_base = Line()
line_base.add_xaxis(Faker.choose())
line_base.add_yaxis("商家A",Faker.values(),areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
line_base.add_yaxis("商家B",Faker.values(),areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
line_base.set_global_opts(title_opts=opts.TitleOpts(title="折线图-m面积图"))
line_base.render_notebook()
面积图
import pyecharts.options as opts
from pyecharts.faker import  Faker
from pyecharts.charts import Line

line_base = Line()
line_base.add_xaxis(Faker.choose())
line_base.add_yaxis("商家A",Faker.values(),markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="min")]))
line_base.add_yaxis("商家B",Faker.values(),markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]))
line_base.set_global_opts(title_opts=opts.TitleOpts(title="折线图-标记点"))
line_base.render_notebook()
折线图
import pyecharts.options as opts
from pyecharts.faker import  Faker
from pyecharts.charts import Line

line_base = Line()
line_base.add_xaxis(Faker.choose())
line_base.add_yaxis("商家A",Faker.values(),markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]))
line_base.add_yaxis("商家B",Faker.values(),)
line_base.set_global_opts(title_opts=opts.TitleOpts(title="折线图-标记点"))
line_base.render_notebook()
折线图

2.2.5 散点图

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Scatter

scatter = Scatter()
scatter.add_xaxis(Faker.choose())
scatter.add_yaxis("商家A",Faker.values())
scatter.set_global_opts(title_opts=opts.TitleOpts(title="散点图-基本示例"))
scatter.render_notebook()
散点图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Scatter

scatter = Scatter()
scatter.add_xaxis(Faker.choose())
scatter.add_yaxis("商家A",Faker.values())
scatter.set_global_opts(title_opts=opts.TitleOpts(title="散点图-显示网格线"),
                       xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
                       yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)))
scatter.render_notebook()
散点图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Scatter

scatter = Scatter()
scatter.add_xaxis(Faker.choose())
scatter.add_yaxis("商家A",Faker.values())
scatter.set_global_opts(title_opts=opts.TitleOpts(title="散点图-VisualMap"),
                       visualmap_opts=opts.VisualMapOpts(max_=150))
scatter.render_notebook()
散点图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Scatter

scatter = Scatter()
scatter.add_xaxis(Faker.choose())
scatter.add_yaxis("商家A",Faker.values())
scatter.add_yaxis("商家B",Faker.values())
scatter.set_global_opts(title_opts=opts.TitleOpts(title="散点图-VisualMap"),
                       visualmap_opts=opts.VisualMapOpts(type_="size",max_=150,min_=20))
scatter.render_notebook()
散点图

2.3 地理图表

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType

geo_base = Geo()
geo_base.add_schema(maptype="china")
geo_base.add("geo",[list(z) for z in zip(Faker.provinces,Faker.values())])
geo_base.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo_base.set_global_opts(title_opts=opts.TitleOpts(title="地理图表-基本示例"),
                        visualmap_opts=opts.VisualMapOpts())
geo_base.render_notebook()
地理图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType

geo_base = Geo()
geo_base.add_schema(maptype="china")
geo_base.add("geo",[list(z) for z in zip(Faker.provinces,Faker.values())])
geo_base.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo_base.set_global_opts(title_opts=opts.TitleOpts(title="地理图表-Visual(分段型)"),
                        visualmap_opts=opts.VisualMapOpts(is_piecewise=True))
geo_base.render_notebook()
地理图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType

geo_base = Geo()
geo_base.add_schema(maptype="china")
geo_base.add("geo",[list(z) for z in zip(Faker.provinces,Faker.values())],type_=ChartType.EFFECT_SCATTER)
geo_base.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo_base.set_global_opts(title_opts=opts.TitleOpts(title="地理图表-涟漪特效图"))
geo_base.render_notebook()
地理图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType

geo_base = Geo()
geo_base.add_schema(maptype="china")
geo_base.add("geo",[list(z) for z in zip(Faker.provinces,Faker.values())],type_=ChartType.HEATMAP)
geo_base.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo_base.set_global_opts(title_opts=opts.TitleOpts(title="地理图表-基本示例"),
                        visualmap_opts=opts.VisualMapOpts())
geo_base.render_notebook()
地理图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType

geo_base = Geo()
geo_base.add_schema(maptype="广东")
geo_base.add("geo",[list(z) for z in zip(Faker.guangdong_city,Faker.values())],type_=ChartType.HEATMAP)
geo_base.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo_base.set_global_opts(title_opts=opts.TitleOpts(title="地理图表-广东地图"),
                        visualmap_opts=opts.VisualMapOpts())
geo_base.render_notebook()
地理图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType

geo_line = Geo()
geo_line.add_schema(maptype="china")
geo_line.add("",[("广州", 55), ("北京", 66), ("杭州", 77), ("重庆", 88)],
            type_=ChartType.EFFECT_SCATTER,color="green")
geo_line.add( "geo",[("广州", "上海"), ("广州", "北京"), ("广州", "杭州"), ("广州", "重庆")],
            type_=ChartType.LINES,
            effect_opts=opts.EffectOpts(symbol=SymbolType.ARROW, symbol_size=6, color="blue"),
            linestyle_opts=opts.LineStyleOpts(curve=0.2))
geo_line.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo_line.set_global_opts(title_opts=opts.TitleOpts(title="地理图表-指示线"))
geo_line.render_notebook()
地理图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType

geo_line = Geo()
geo_line.add_schema(maptype="china",itemstyle_opts=opts.ItemStyleOpts(color="#323c48", border_color="#111"))
geo_line.add("",[("广州", 55), ("北京", 66), ("杭州", 77), ("重庆", 88)],
            type_=ChartType.EFFECT_SCATTER,color="white")
geo_line.add( "geo",[("广州", "上海"), ("广州", "北京"), ("广州", "杭州"), ("广州", "重庆")],
            type_=ChartType.LINES,
            effect_opts=opts.EffectOpts(symbol=SymbolType.ARROW, symbol_size=6, color="blue"),
            linestyle_opts=opts.LineStyleOpts(curve=0.2))
geo_line.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo_line.set_global_opts(title_opts=opts.TitleOpts(title="地理图表-指示线-背景"))
geo_line.render_notebook()
地理图

2.4 组合图表

2.4.1 Grid:并行多图

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line,Scatter

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values())
bar.set_global_opts(title_opts=opts.TitleOpts(title="Grid_柱状图"))

line = Line()
line.add_xaxis(Faker.choose())
line.add_yaxis("商家A",Faker.values())
line.add_yaxis("商家B",Faker.values())
line.set_global_opts(title_opts=opts.TitleOpts(title="Grid_折线图",pos_top="48%"),
                    legend_opts=opts.LegendOpts(pos_top="48%"))

grid = Grid()
grid.add(bar,grid_opts=opts.GridOpts(pos_bottom="60%"))
grid.add(line,grid_opts=opts.GridOpts(pos_top="60%"))
grid.render_notebook()
并行多图
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line,Scatter

scatter = Scatter()
scatter.add_xaxis(Faker.choose())
scatter.add_yaxis("商家A",Faker.values())
scatter.add_yaxis("商家B",Faker.values())
scatter.set_global_opts(title_opts=opts.TitleOpts(title="Grid_散点图"),
                       legend_opts=opts.LegendOpts(pos_left="20%"))

line = Line()
line.add_xaxis(Faker.choose())
line.add_yaxis("商家A",Faker.values())
line.add_yaxis("商家B",Faker.values())
line.set_global_opts(title_opts=opts.TitleOpts(title="Grid_折线图",pos_right="5%"),
                    legend_opts=opts.LegendOpts(pos_right="20%"))

grid = Grid()
grid.add(scatter,grid_opts=opts.GridOpts(pos_left="55%"))
grid.add(line,grid_opts=opts.GridOpts(pos_right="55%"))
grid.render_notebook()
并行多图

2.4.2 Page:顺序多图

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Page

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A",Faker.values())
bar.add_yaxis("商家B",Faker.values())
bar.set_global_opts(title_opts=opts.TitleOpts(title="Page-柱状图"))

line = Line()
line.add_xaxis(Faker.choose())
line.add_yaxis("商家A",Faker.values())
line.add_yaxis("商家B",Faker.values())
line.set_global_opts(title_opts=opts.TitleOpts(title="Page-折线图"))

page = Page()
page.add(bar,line)
page.render_notebook()
顺序多图

2.4.3 Tab:选项卡多图

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar, Tab, Pie, Line
from pyecharts.components import Table

bar = Bar()
bar.add_xaxis(Faker.days_attrs)
bar.add_yaxis("商家A", Faker.days_values)
bar.set_global_opts(title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"),
            datazoom_opts=[opts.DataZoomOpts()])

line = Line()
line.add_xaxis(Faker.choose())
line.add_yaxis("商家A",Faker.values(),markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="min")]))
line.add_yaxis("商家B",Faker.values(),markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]))
line.set_global_opts(title_opts=opts.TitleOpts(title="Line-MarkPoint"))

v = Faker.choose()
pie = Pie()
pie.add("",
        [list(z) for z in zip(v, Faker.values())],
        radius=["30%", "75%"],
        center=["25%", "50%"],
        rosetype="radius",
        label_opts=opts.LabelOpts(is_show=False),
        )
pie.add("",
        [list(z) for z in zip(v, Faker.values())],
        radius=["30%", "75%"],
        center=["75%", "50%"],
        rosetype="area",
        )
pie.set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图示例"))

table = Table()
headers = ["City name", "Area", "Population", "Annual Rainfall"]
rows = [
        ["Brisbane", 5905, 1857594, 1146.4],
        ["Adelaide", 1295, 1158259, 600.5],
        ["Darwin", 112, 120900, 1714.7],
        ["Hobart", 1357, 205556, 619.5],
        ["Sydney", 2058, 4336374, 1214.8],
        ["Melbourne", 1566, 3806092, 646.9],
        ["Perth", 5386, 1554769, 869.4],
    ]
table.add(headers, rows).set_global_opts(title_opts=opts.ComponentTitleOpts(title="Table"))

tab = Tab()
tab.add(bar, "柱状图")
tab.add(line, "折线图")
tab.add(pie, "玫瑰图")
tab.add(table, "表格")
tab.render_notebook()
选项卡多图

2.4.4 Timeline:时间线轮播多图

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar, Page, Pie, Timeline

x = Faker.choose()
tl = Timeline()
for i in range(2015, 2020):
    bar = Bar()
    bar.add_xaxis(x)
    bar.add_yaxis("商家A", Faker.values())
    bar.add_yaxis("商家B", Faker.values())
    bar.set_global_opts(title_opts=opts.TitleOpts("某商店{}年营业额".format(i)))
    tl.add(bar, "{}年".format(i))
tl.render_notebook()
时间线轮播多图

2.5 HTML组件

2.5.1 表格

from pyecharts.components import Table
from pyecharts.options import ComponentTitleOpts

table = Table()
headers = ["City name", "Area", "Population", "Annual Rainfall"]
rows = [
    ["Brisbane", 5905, 1857594, 1146.4],
    ["Adelaide", 1295, 1158259, 600.5],
    ["Darwin", 112, 120900, 1714.7],
    ["Hobart", 1357, 205556, 619.5],
    ["Sydney", 2058, 4336374, 1214.8],
    ["Melbourne", 1566, 3806092, 646.9],
    ["Perth", 5386, 1554769, 869.4],
]
table.add(headers, rows)
table.set_global_opts(title_opts=ComponentTitleOpts(title="Table-我是主标题", subtitle="我是副标题支持换行哦"))
table.render()
表格

2.5.2 图像

from pyecharts.components import Image
from pyecharts.options import ComponentTitleOpts

image = Image()
img_src = "https://user-images.githubusercontent.com/19553554/39612358-499eb2ae-4f91-11e8-8f56-179c4f0bf2df.png"
image.add(src=img_src,
    style_opts={"width": "200px", "height": "200px", "style": "margin-top: 20px"},)
image.set_global_opts(
    title_opts=ComponentTitleOpts(title="Image-基本示例", subtitle="我是副标题支持换行哦"))
image.render()

主题风格

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
from pyecharts.faker import Faker

def theme_bar(theme_name):
    bar = Bar(init_opts=opts.InitOpts(theme=theme_name))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis("商家A",Faker.values())
    bar.add_yaxis("商家B",Faker.values())
    bar.add_yaxis("商家C",Faker.values())
    bar.add_yaxis("商家D",Faker.values())
    bar.set_global_opts(title_opts=opts.TitleOpts(title=theme_name))
    return bar.render_notebook()
# 默认主题WHITE
theme_bar(theme_name=ThemeType.WHITE)
white
# LIGHT
theme_bar(theme_name=ThemeType.LIGHT)
light
# DARK
theme_bar(theme_name=ThemeType.DARK)
dark
# CHALK
theme_bar(theme_name=ThemeType.CHALK)
chalk
# ESSOS
theme_bar(theme_name=ThemeType.ESSOS)
essos
# INFOGRAPHIC
theme_bar(theme_name=ThemeType.INFOGRAPHIC)
infographic
# MACARONS
theme_bar(theme_name=ThemeType.MACARONS)
macarons
# PURPLE_PASSION
theme_bar(theme_name=ThemeType.PURPLE_PASSION)
pupple_passion
# ROMA
theme_bar(theme_name=ThemeType.ROMA)
roma
# ROMANTIC
theme_bar(theme_name=ThemeType.ROMANTIC)
romantic
# SHINE
theme_bar(theme_name=ThemeType.SHINE)
shine
# VINTAGE
theme_bar(theme_name=ThemeType.VINTAGE)
vintage
# WALDEN
theme_bar(theme_name=ThemeType.WALDEN)
walden
# WESTEROS
theme_bar(theme_name=ThemeType.WESTEROS)
westeros
# WONDERLAND
theme_bar(theme_name=ThemeType.WONDERLAND)
wonderland

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343