阅读 13

五分钟Pyecharts快速上手使用

本文主要参考:官方文档

使用例子

强烈建议看一看例子的源码

爬取京东评价信息并可视化

安装和查看版本

安装:

换源为清华源。

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts
复制代码

查看版本:

import pyecharts
print(pyecharts.__version__)
复制代码

导包

本文使用的版本是1.7.1

最常见的问题是直接导入模块失败,如:

from pyecharts import Bar
复制代码

因为扩展模块的原因,需要这么写才能导入

from pyecharts.charts import Bar
复制代码

绘制柱形图

#创建图表并设置主题
bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
#添加x值,x是一个列表
bar.add_xaxis(name)
#添加y值,y是一个列表
bar.add_yaxis('购买人数',count)
#设置主标题、副标题
bar.set_global_opts(title_opts=opts.TitleOpts(title="Dior999色号分布", subtitle="数据来源:京东"))
#设置生成html文件
bar.render('bar.html')
复制代码

饼图文档

文档地址

绘制饼状图

from pyecharts.charts import Pie
from pandas import Series
import pandas as pd
from pyecharts import options as opts

colors = [你的数据]
ss_colors = Series(colors)

x = ss_colors.value_counts()
list1=x.values.tolist() #数量
list2=x.index.tolist()  #名称

c = (
    Pie()
    .add(
        "",
        [
            list(z)
            for z in zip(
                list2 ,
                list1 ,
            )
        ],
        center=["40%", "50%"],
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="颜色分布"),
        legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"),
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .render("pie_scroll_legend.html")
)
复制代码

富文本饼图

from pyecharts.charts import Pie
from pandas import Series
import pandas as pd
from pyecharts import options as opts

name,count =[],[]
data_jd = pd.read_csv('K:\学习\get_jd3.csv')
data = data_jd.values.tolist()
for i in range(len(data)):
    now = data[i]
    name.append(now[0])
    count.append(now[1])

c = (
    Pie()
    .add(
        "",
        [list(z) for z in zip(name, count)],
        radius=["40%", "55%"],
        label_opts=opts.LabelOpts(
            position="outside",
            formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c}  {per|{d}%}  ",
            background_color="#eee",
            border_color="#aaa",
            border_width=1,
            border_radius=4,
            rich={
                "a": {"color": "#999", "lineHeight": 22, "align": "center"},
                "abg": {
                    "backgroundColor": "#e3e3e3",
                    "width": "100%",
                    "align": "right",
                    "height": 22,
                    "borderRadius": [4, 4, 0, 0],
                },
                "hr": {
                    "borderColor": "#aaa",
                    "width": "100%",
                    "borderWidth": 0.5,
                    "height": 0,
                },
                "b": {"fontSize": 16, "lineHeight": 33},
                "per": {
                    "color": "#eee",
                    "backgroundColor": "#334455",
                    "padding": [2, 4],
                    "borderRadius": 2,
                },
            },
        ),
    )
    .set_global_opts(title_opts=opts.TitleOpts(title=" "))
    .render("pie_rich_label.html")
)
复制代码

绘制玫瑰图

from pyecharts.charts import Pie
from pandas import Series
import pandas as pd
from pyecharts import options as opts

name,count =[],[]
data_jd = pd.read_csv('K:\学习\get_jd3.csv')
data = data_jd.values.tolist()
for i in range(len(data)):
    now = data[i]
    name.append(now[0])
    count.append(now[1])

c = (
    Pie()
    .add(
        "",
        [list(z) for z in zip(name, count)],
        radius=["30%", "75%"],
        center=["25%", "50%"],
        rosetype="radius",
        label_opts=opts.LabelOpts(is_show=False),
    )
    .set_global_opts(title_opts=opts.TitleOpts(title=" "))
    .render("pie_rosetype.html")
)
复制代码

词云图文档

词云文档

钻石型词云

我没看出来像钻石

import pyecharts.options as opts
from pyecharts.charts import WordCloud
import pandas as pd
from pyecharts.globals import SymbolType

data_jd = pd.read_csv('K:\学习\get_jd3.csv')
data = data_jd.values.tolist()
c = (
    WordCloud()
    .add("", data, word_size_range=[20, 100], shape=SymbolType.DIAMOND)
    .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-shape-diamond"))
    .render("wordcloud_diamond.html")
)
复制代码