pyecharts——可视化分析

1.常用图表绘制

# 一次性导入所有图表
from pyecharts.charts import *
# pyecharts的配置项
from pyecharts import options as opts

基本图表的分类:

1.直角坐标系格式:柱状图、折线图等

2.地理坐标系:Map(区域地图)、Geo(点地图)、BMap(百度地图)

3.其他格式:饼图、漏斗图、日历图等

1.1直角坐标系:

数据格式:List类型、ChartItem——可针对单个数据样式的改变

# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [123, 153, 89, 107, 98, 23]

# 新建一个Line实例,命名为chart
chart = Line()

# 为chart添加x轴的数据
chart.add_xaxis(x_data)

# 为chart添加y轴的数据
chart.add_yaxis(
        ' ',
         y_data,
        # 区域颜色填充,默认使用系列颜色,透明度设置为0.5
        areastyle_opts=opts.AreaStyleOpts(opacity=0.5)   #折线图与面积图的差别
    )

# 在noteboo中夹在图表
# 如果是在Pycharm等IDE中 使用charts.render("xxx.html")
chart.render_notebook()     
折线图/面积图
# 新建一个Scatter实例,命名为chart
chart = Scatter()

# 为chart添加x轴的数据
chart.add_xaxis(x_data)
# 为chart添加y轴的数据
chart.add_yaxis('', y_data)

# 在noteboo中夹在图表
# 如果是在Pycharm等IDE中 使用charts.render("xxx.html")
chart.render_notebook()

'''大多数情况下,散点图会用来展示两个指标之间的关系
我们需要设置x轴的数据类型为value,y轴默认是value形式,不需要再单独设置'''

# 新建一个Scatter实例,命名为chart
chart = Scatter()
# 为chart添加x轴的数据
chart.add_xaxis(x_data)
# 为chart添加y轴的数据
chart.add_yaxis('', y_data)

# 设置x轴为数值类型
chart.set_global_opts(
        xaxis_opts=opts.AxisOpts(type_="value")
        )

chart.render_notebook()
散点图及变形
''' 直角坐标系中的热力图x轴y轴都必须是category类型'''
# x轴数据项
hours = ['0点', '1点', '2点', '3点', '4点', '5点', '6点', '7点', '8点', '9点', '10点', '11点', '12点',
         '13点', '14点', '15点', '16点', '17点', '18点', '19点', '20点', '21点', '22点']
# y轴数据项
weekdays = ['周一', '周二', '周三', '周四', '周五', '周六', '周天']
# 单个数据项点值
data = [[0, 0, 5], [1, 0, 1], [2, 0, '-'], [3, 0, '-'], [4, 0, '-'], [5, 0, '-'], [6, 0, '-'], [7, 0, '-'], 
        [8, 0, '-'], [9, 0, '-'], [10, 0, '-'], [11, 0, 2], [12, 0, 4], [13, 0, 1], [14, 0, 1], [15, 0, 3], 
        [16, 0, 4], [17, 0, 6], [18, 0, 4], [19, 0, 4], [20, 0, 3], [21, 0, 3], [22, 0, 2], [23, 0, 5], 
        [0, 1, 7], [1, 1, '-'], [2, 1, '-'], [3, 1, '-'], [4, 1, '-'], [5, 1, '-'], [6, 1, '-'], [7, 1, '-'], 
        [8, 1, '-'], [9, 1, '-'], [10, 1, 5], [11, 1, 2], [12, 1, 2], [13, 1, 6], [14, 1, 9], [15, 1, 11], 
        [16, 1, 6], [17, 1, 7], [18, 1, 8], [19, 1, 12], [20, 1, 5], [21, 1, 5], [22, 1, 7], [23, 1, 2], 
        [0, 2, 1], [1, 2, 1], [2, 2, '-'], [3, 2, '-'], [4, 2, '-'], [5, 2, '-'], [6, 2, '-'], [7, 2, '-'],
        [8, 2, '-'], [9, 2, '-'], [10, 2, 3], [11, 2, 2], [12, 2, 1], [13, 2, 9], [14, 2, 8], [15, 2, 10], 
        [16, 2, 6], [17, 2, 5], [18, 2, 5], [19, 2, 5], [20, 2, 7], [21, 2, 4], [22, 2, 2], [23, 2, 4], [0, 3, 7], 
        [1, 3, 3], [2, 3, '-'], [3, 3, '-'], [4, 3, '-'], [5, 3, '-'], [6, 3, '-'], [7, 3, '-'], [8, 3, 1], [9, 3, '-'], 
        [10, 3, 5], [11, 3, 4], [12, 3, 7], [13, 3, 14], [14, 3, 13], [15, 3, 12], [16, 3, 9], [17, 3, 5], [18, 3, 5], 
        [19, 3, 10], [20, 3, 6], [21, 3, 4], [22, 3, 4], [23, 3, 1], [0, 4, 1], [1, 4, 3], [2, 4, '-'], [3, 4, '-'],
        [4, 4, '-'], [5, 4, 1], [6, 4, '-'], [7, 4, '-'], [8, 4, '-'], [9, 4, 2], [10, 4, 4], [11, 4, 4], [12, 4, 2], 
        [13, 4, 4], [14, 4, 4], [15, 4, 14], [16, 4, 12], [17, 4, 1], [18, 4, 8], [19, 4, 5], [20, 4, 3], [21, 4, 7],
        [22, 4, 3], [23, 4, '-'], [0, 5, 2], [1, 5, 1], [2, 5, '-'], [3, 5, 3], [4, 5, '-'], [5, 5, '-'], [6, 5, '-'], 
        [7, 5, '-'], [8, 5, 2], [9, 5, '-'], [10, 5, 4], [11, 5, 1], [12, 5, 5], [13, 5, 10], [14, 5, 5], [15, 5, 7], 
        [16, 5, 11], [17, 5, 6], [18, 5, '-'], [19, 5, 5], [20, 5, 3], [21, 5, 4], [22, 5, 2], [23, 5, '-'], [0, 6, 1], 
        [1, 6, '-'], [2, 6, '-'], [3, 6, '-'], [4, 6, '-'], [5, 6, '-'], [6, 6, '-'], [7, 6, '-'], [8, 6, '-'], 
        [9, 6, '-'], [10, 6, 1], [11, 6, '-'], [12, 6, 2], [13, 6, 1], [14, 6, 3], [15, 6, 4], [16, 6, '-'], 
        [17, 6, '-'], [18, 6, '-'], [19, 6, '-'], [20, 6, 1], [21, 6, 2], [22, 6, 2], [23, 6, 6]]

# 新建heatmap实例
chart = HeatMap()
# 添加x轴数据
chart.add_xaxis(hours)
# 添加y轴数据项和数值
chart.add_yaxis(
    "",
    weekdays,
    data
    )

# 全局配置项
chart.set_global_opts(
    # 视觉组件是必须的,需要通过视觉组件的颜色来展示数据大小
    visualmap_opts=opts.VisualMapOpts(max_=10),
)

chart.render_notebook()
热力图

直角坐标系通用方法——都是继承自RectChart,都拥有以下方法。

from pyecharts.charts import *
from pyecharts import options as opts
# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
# 左边坐标轴的数据
y_data_1 = [123, 153, 89, 107, 98, 23]
# 右边坐标轴的数据
y_data_2 = [13, 15, 9, 17, 8, 6]

chart = Bar()
chart.add_xaxis(x_data)
# 添加应用于左边坐标轴的数据
chart.add_yaxis(
    '左侧', 
    y_data_1,
    # 指定坐标轴,默认0,可省略
    yaxis_index=0
    )

# 添加应用于右侧坐标轴的数据
chart.add_yaxis(
    '右侧', 
    y_data_2,
    # 指定坐标轴,默认0
    yaxis_index=1
    )

# 添加坐标轴
chart.extend_axis(yaxis=opts.AxisOpts())
chart.render_notebook()
添加坐标轴(左右双轴)
chart = Bar()
chart.add_xaxis(x_data)
chart.add_yaxis('', y_data)
# xy轴翻转
chart.reversal_axis()
chart.render_notebook()
XY轴翻转
# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data_1 = [123, 153, 89, 107, 98, 23]
y_data_2 = [13, 15, 9, 17, 8, 6]

chart_1 = Bar()
chart_1.add_xaxis(x_data)
chart_1.add_yaxis('', y_data_2)

chart_2 = Line()
chart_2.add_xaxis(x_data)
chart_2.add_yaxis('', y_data_1)

# overlap层叠
chart_1.overlap(chart_2)
chart_1.render_notebook()
层叠多图

1.2地理坐标系:

# 虚假示例数据
data = [('广东', 61), ('湖北', 85), ('湖南', 115), ('四川', 137), ('重庆', 119), ('黑龙江', 66), ('浙江', 117),
        ('山西', 52), ('河北', 80), ('安徽', 99), ('河南', 101), ('山东', 75), ('西藏', 53)]

chart = Map()
chart.add(
    "", 
    data, 
    # 必须的参数,指定地图类型
    maptype='china',
    # 不显示红点
    is_map_symbol_show=False,
    )
# 全局配置项
chart.set_global_opts(
    # 视觉组件是必须的,需要通过视觉组件的颜色来展示数据大小
    visualmap_opts=opts.VisualMapOpts(max_=max(data,key=lambda x:x[1])[1]),
)
chart.render_notebook()
区域地图(Map)
# 虚假示例数据
data = [('广东', 61), ('湖北', 85), ('湖南', 115), ('四川', 137), ('重庆', 119), ('黑龙江', 66), ('浙江', 117),
        ('山西', 52), ('河北', 80), ('安徽', 99), ('河南', 101), ('山东', 75), ('西藏', 53)]
 
chart = Geo()
chart.add_schema(
    maptype='china'
)
chart.add(
    "", 
    data,
    type_='scatter'
    )
 
# 全局配置项
chart.set_global_opts(
    # 视觉组件是必须的,需要通过视觉组件的图形大小来展示数据大小
    visualmap_opts=opts.VisualMapOpts(
        type_='size',
        ),
)
chart.render_notebook()

'''热力图'''
chart = Geo()
chart.add_schema(
    maptype='china'
)
chart.add(
    "", 
    data,
    type_='heatmap'
    )
 
 
# 全局配置项, HeatMap需要配合视觉组件
chart.set_global_opts(
    visualmap_opts=opts.VisualMapOpts(
        type_='color',
        max_=max(data,key=lambda x:x[1])[1]
    ),
)
chart.render_notebook()
点地图(散点、流向图、热力图等)

1.3其他图表:

data_pair = [['Apple', 123], ['Huawei', 153], ['Xiaomi', 89], ['Oppo', 107], ['Vivo', 98], ['Meizu', 23]]

chart = Pie()
chart.add(
       '', 
       data_pair
       )
       
chart.render_notebook()

''' 多饼图'''
# 虚假数据
data_pair = [['Apple', 123], ['Huawei', 153], ['Xiaomi', 89], ['Oppo', 107], ['Vivo', 98], ['Meizu', 23]]
 
chart = Pie()
chart.add(
       '', 
       data_pair,
       # radius控制半径大小
       radius=['30%', '40%'],
       # center指定显示位置
       center=['25%', '50%']
       )
 
chart.add(
       '', 
       data_pair,
       radius=['30%', '40%'],
       center=['75%', '50%']
       )
       
chart.render_notebook()
饼图/圆环图
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker
 
"""
Faker 是Pyechart中提供的示例数据的class
会根据图表类型随机生成一些样例数据
"""
 
bar = Bar(
    init_opts=opts.InitOpts(
        theme='light',
        width='1000px',
        height='600px')
        )
 
# 添加分类(x轴)的数据
bar.add_xaxis(Faker.choose())
 
# stack值一样的系列会堆叠在一起
# A系列和B系列都是stack1,会产生堆叠
# C系列为stack2 不与AB堆叠
bar.add_yaxis('A', Faker.values(), stack='stack1')
bar.add_yaxis('B', Faker.values(), stack='stack1')
bar.add_yaxis('C', Faker.values(), stack='stack2')
 
bar.render_notebook()
堆叠效果图
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = [random.randint(0, 100) for _ in range(30)]
# 注意ydata中包含多个维度的数据
y_data = [(random.randint(0, 100), random.randint(0, 100), random.randint(0, 100), random.randint(0, 100))
          for _ in range(30)]

# 新建散点图
scatter = Scatter(init_opts=opts.InitOpts(theme='light',
                                          width='1000px',
                                          height='600px'))
# 添加x轴数据
scatter.add_xaxis(x_data)
# 添加y轴数据
scatter.add_yaxis(
    '', 
    y_data
    )
# dimension指定数据维度
scatter.set_global_opts(visualmap_opts=[opts.VisualMapOpts(is_show=True, type_='size', dimension=2, pos_top='5%', range_text=['大小', '']),  # 通过图形大小映射
                                        opts.VisualMapOpts(is_show=True, type_='color', dimension=3, pos_top='35%', range_text=['颜色', '']),   # 通过颜色映射
                                        opts.VisualMapOpts(is_show=True, type_='opacity', dimension=4, range_opacity=[0.2, 1], pos_top='65%', range_text=['透明度', ''])  # 通过透明度映射
                                        ],
    xaxis_opts=opts.AxisOpts(type_="value"))
scatter.render_notebook()
多个视觉组件
from pyecharts.charts import *
from pyecharts import options as opts
import random
 
color_js = """
            new echarts.graphic.LinearGradient(
                                0,
                                1,
                                0,
                                0,
                                [{offset: 0, color: '#00008B'},
                                 {offset: 1, color: '#DA70D6'}],
                                false)
           """
x_data = ["2020/7/{}".format(i + 1) for i in range(7)]
 
# 随机生成点数据
y_data = [random.randint(10, 20) for i in range(len(x_data))]
 
bar = Bar(
    init_opts=opts.InitOpts(
        theme='light',
        width='1000px',
        height='600px')
)
bar.add_xaxis(x_data)
 
bar.add_yaxis(
    '',
    y_data,
    itemstyle_opts={
        'shadowBlur': 10,   # 光晕
        'shadowColor': 'rgba(0, 0, 0, 0.5)',  # 阴影颜色
        'shadowOffsetY': 5,  # 阴影偏移量——Y方向
        'shadowOffsetX': 5,  # 阴影偏移量——X方向
        'barBorderRadius':[60, 60, 20, 20],
        'color': JsCode(color_js)
    },
    label_opts=opts.LabelOpts(  # 标签设置
        font_size=25,   # 字体大小
        font_weight='bold',  # 加粗
        position='insideTop'  # 标签位置
        ),
)
 
bar.set_global_opts(
    yaxis_opts=opts.AxisOpts(is_show=False),   # 关闭Y轴显示
    xaxis_opts=opts.AxisOpts(
        boundary_gap=False,  # 两边不显示间隔
        axistick_opts=opts.AxisTickOpts(is_show=False),  # 刻度不显示
        splitline_opts=opts.SplitLineOpts(is_show=False),  # 分割线不显示
        axisline_opts=opts.AxisLineOpts(is_show=False),  # 轴线不显示
        axislabel_opts=opts.LabelOpts(  # 坐标轴标签配置
            font_size=20,  # 字体大小
            font_weight='bold'  # 加重
            )
    )
    )
 
bar.render_notebook()
渐变色与标签变化

2.常用配置

2.1系列配置

  • 这样每次add()的就是一个系列,在我们图表上可以通过图例展示出来。

  • 所以 我们刚刚所说的系列配置项,就是只会作用于在单一系列中,比如系列1中添加的配置就不会影响系列2

# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data_1 = [123, 153, 89, 107, 98, 43]
y_data_2 = [213, 184, 99, 127, 91, 73]
chart = Bar()
chart.add_xaxis(x_data)
chart.add_yaxis(
    '一季度', 
    y_data_1,
    # 标签配置
    label_opts=opts.LabelOpts(
        font_size=12,  # 字体大小
        color='white',  # 文字颜色
        font_style='italic',  # 字体风格
        font_weight='bolder',  # 加粗
        font_family='Courier New',  # 字体
        position='outsideTop',  # 显示位置
        formatter = '{a}\n{b}\n{c}万'  # 文本内容
        ),
    
    # 图元样式配置
    itemstyle_opts=opts.ItemStyleOpts(
        color='red',  # 颜色
        opacity=.5,  # 透明度
        border_width=5,  # 边框宽度
        border_color='gold'  # 边框颜色
    )

    
    )
Line1 = Line()
Line1.add_xaxis(x_data)
Line1.add_yaxis(
    '二季度', 
    y_data_2,
    
    # 连线样式配置
    linestyle_opts=opts.LineStyleOpts(
        color='red',  # 颜色
        opacity=.7,  # 透明度
        curve=0,  # 弯曲程度,0表示完全不弯曲,折线图中没用
        width=5,  # 线宽
        type_='dotted'  # 连线的类型
    ),  
    # 连线样式配置
    areastyle_opts=opts.AreaStyleOpts(
        color='red',  # 颜色
        opacity=.3,  # 透明度
    )
    )
chart.overlap(Line1)
chart.render_notebook()
''' 丑是丑了点  不过是这个意思'''
系列样式汇总

2.2全局配置

  • 与系列配置项,全局配置项即作用于图表全局的配置;

  • 当然也不是绝对的,比如我们进行视觉组件配置的时候,就可以通过series_index制定作用的系列;

  • 全局配置项通过chart.set_global_opts()进行配置添加;

chart = Line(
    #初始化配置
    init_opts=opts.InitOpts(
        width='1000px',  # 画布大小
        height='600px',
        theme='dark',  # 设置主题
        #bg_color='grey'  # 背景颜色
        )
)
# 为chart添加x轴的数据
chart.add_xaxis(x_data)

# 为chart添加y轴的数据
chart.add_yaxis(
    '系列1', 
    y_data_1,
    # 区域颜色填充,默认使用系列颜色,透明度设置为0.5
    areastyle_opts=opts.AreaStyleOpts(opacity=0.6),
    linestyle_opts=opts.LineStyleOpts(
        color='red',  # 颜色
        opacity=.6,  # 透明度
        curve=0,   # 弯曲程度,0表示完全不弯曲,折线图中没用
        width=1,   # 线宽
        type_='dotted'  # 连线的类型
    )
    )

chart.add_yaxis(
    '系列2', 
    y_data_2,
    # 区域颜色填充,默认使用系列颜色,透明度设置为0.5
    areastyle_opts=opts.AreaStyleOpts(opacity=0.3),
    linestyle_opts=opts.LineStyleOpts(
        color='blue',  # 颜色
        opacity=.6,  # 透明度
        curve=0,   # 弯曲程度,0表示完全不弯曲,折线图中没用
        width=1,   # 线宽
        type_='dotted'  # 连线的类型
    )
    )
chart.set_global_opts(
    #提示框配置项
    tooltip_opts=opts.TooltipOpts(
        is_show=True,  # 是否使用提示框
        trigger='axis',  # 触发类型
        trigger_on='mousemove|click',  # 触发条件,点击或者悬停均可出发
        axis_pointer_type='cross',  # 指示器类型,鼠标移动到图表区可以查看效果
        formatter = '{a}<br>{b}:{c}万'  # 文本内容
    )

     #区域缩放配置项
    datazoom_opts=opts.DataZoomOpts(
        range_start=0,  # 开始范围
        range_end=50,  # 结束范围
        #orient='vertical',  # 设置为垂直布局
        type_='slider',  # slider形式
        is_zoom_lock=True,  # 锁定区域大小
        pos_left='1%'  # 设置位置
        ),
    #标题配置项
    title_opts=opts.TitleOpts(
        title="居中标题",   # 主标题内容
        subtitle_target='self',  # 当前窗口打开
        pos_left='center',  # 距离左边界距离,center表示剧中
        pos_top='1%',  # 距离上边界距离
        ),
    #图例配置项
    legend_opts=opts.LegendOpts(
        selected_mode='multiple',
        pos_right='10%',  # 距离右边界距离
        pos_top='2%',  # 距离上边界的距离
        #orient='vertical',  # 设置诶垂直布局,默认水平布局=>horizontal
        legend_icon='circle',  # 修改图例icon
        textstyle_opts=opts.TextStyleOpts(
            color='red',  # 颜色
            font_size='12',   # 字体大小
            font_weight='bolder',   # 加粗
        )  # 文本样式配置
    ),
)
# 在noteboo中夹在图表
# 如果是在Pycharm等IDE中 使用charts.render("xxx.html")
chart.render_notebook()
全局配置
posted @ 2022-04-06 11:45  hungry_J  阅读(42)  评论(0)    收藏  举报