python实训笔记(可视化分析)

数据分析(可视化分析)

  • 1、pyecharts的安装
  • 2、pyecharts开发文档
  • 3、示例
      • 1、柱状图
      • 2、折线图
      • 3、散点图
      • 4、层叠多图
      • 5、饼状图

1、pyecharts的安装

python -m pip install pyecharts

python -m pip install echarts-countries-pypkg

python -m pip install echarts-china-provinces-pypkg

python -m pip install echarts-china-cities-pypkg

python -m pip install echarts-china-counties-pypkg

2、pyecharts开发文档

https://pyecharts.org/#/zh-cn/intro

3、示例

1、柱状图

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

c = (
    Bar()
    .add_xaxis(Faker.choose())  #x轴传入的数据  list
    .add_yaxis("商家A", Faker.values()) #传入第一个y轴数据
    .add_yaxis("商家B", Faker.values()) #传入第二个y轴数据
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkLine(指定类型)")) #图表名称
    .set_series_opts(
        label_opts=opts.LabelOpts(is_show=False), #是否显示y轴对应数据
        markline_opts=opts.MarkLineOpts(
            data=[
                opts.MarkLineItem(type_="min", name="最小值"),
                opts.MarkLineItem(type_="max", name="最大值"),
                opts.MarkLineItem(type_="average", name="平均值"),
            ]
        ),
    )
    .render("柱状图.html") #图表返回的文件类型以及名称
)

2、折线图

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

c = (
    Line()
    .add_xaxis(Faker.choose())#x轴数据
    .add_yaxis("商家A", Faker.values()) #y轴数据
    .add_yaxis("商家B", Faker.values()) #y轴数据
    .set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例"))
    .render("line_base.html")
)

3、散点图

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

c = (
    Scatter()
    .add_xaxis(Faker.choose()) #x轴数据
    .add_yaxis("商家A", Faker.values()) #y轴数据
    .add_yaxis("商家B", Faker.values()) #轴数据
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Scatter-VisualMap(Size)"), #图表标题
        visualmap_opts=opts.VisualMapOpts(type_="size", max_=150, min_=20), #设置左下角比较参数表
    )
    .render("scatter_visualmap_size.html")
)

4、层叠多图

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

v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
v3 = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]


bar = (
    Bar()
    .add_xaxis(Faker.months) #x轴数据
    .add_yaxis("蒸发量", v1) #柱状图y轴数据1
    .add_yaxis("降水量", v2) #柱状图y轴数据2
    .extend_axis(
        yaxis=opts.AxisOpts(
            axislabel_opts=opts.LabelOpts(formatter="{value} °C"), interval=5
        )
    )
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) #是否显示y轴数值数据
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Overlap-bar+line"),
        yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} ml")),
    )
)

line = Line().add_xaxis(Faker.months).add_yaxis("平均温度", v3, yaxis_index=1) #折线图数据
bar.overlap(line)
bar.render("overlap_bar_line.html")

5、饼状图

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.choose() + Faker.choose(),
                Faker.values() + Faker.values() + Faker.values(),
            )
        ],
        center=["40%", "50%"],
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Pie-Legend 滚动"),
        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")
)

#[(数据说明,数据值),()]

你可能感兴趣的:(python实训笔记(可视化分析))