【Python入门可视化】:12个完整数据可视化小例子,带你玩转可视化~

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

总共22个完整的pyecharts例子,包含常用的配置方法,每个小例子都包含完整代码,为避免混淆,每个例子都差不多只包含单一配置的代码。

在这里插入图片描述

1. 柱状图堆叠

不同系列的数据使用相同的stack值会堆叠在一起;

在这里插入图片描述

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


def bar_stack():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    # stack值一样的系列会堆叠在一起
    bar.add_yaxis('A', Faker.values(), stack='stack1')
    bar.add_yaxis('B', Faker.values(), stack='stack1')
    bar.add_yaxis('C', Faker.values(), stack='stack2')
    return bar



chart = bar_stack()
chart.render_notebook()
复制代码

2.关闭坐标轴显示

碰上类目标签过长的时候,可以选择关闭坐标轴,将数据&标签直接显示在图形中

在这里插入图片描述

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


def bar_with_axis_off():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('', Faker.values())
    # 碰上类目标签过长的时候,可以选择关闭坐标轴,直接显示在图形中
    bar.set_series_opts(label_opts=opts.LabelOpts(position='insideLeft', formatter='{b}:{c}'))
    bar.set_global_opts(xaxis_opts=opts.AxisOpts(is_show=False),
                        yaxis_opts=opts.AxisOpts(is_show=False))
    bar.reversal_axis()
    return bar


chart = bar_with_axis_off()
chart.render_notebook()
复制代码

3.更改坐标轴数据类型

x轴默认数据类型是使用离散型,在使用散点图的时候可调整为数值型 在这里插入图片描述

from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = [random.randint(0, 20) for _ in range(100)]
y_data = [random.randint(0, 50) for _ in range(100)]


def scatter_with_value_xaxis():
    scatter = Scatter(init_opts=opts.InitOpts(theme='light',
                                              width='1000px',
                                              height='600px'))
    scatter.add_xaxis(x_data)
    scatter.add_yaxis('', y_data)
    # X轴默认数据类型为离散数据,设置为数值型
    scatter.set_global_opts(xaxis_opts=opts.AxisOpts(type_="value"))
    return scatter

chart = scatter_with_value_xaxis()
chart.render_notebook()
复制代码

4.双Y轴【直方图&折线图】

实际是Bar和Line两个图表共用同一套坐标体系,将Bar和Line分别指向不同的Y轴 在这里插入图片描述

from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ['香蕉', '梨子', '水蜜桃', '核桃', '西瓜', '苹果']
y_data_1 = [random.randint(10, 50) for _ in range(len(x_data))]
y_data_2 = [random.randint(100, 500) for _ in range(len(x_data))]


def bar_line_combine_with_two_axis():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    # 添加一个Y轴
    bar.extend_axis(yaxis=opts.AxisOpts())
    bar.add_yaxis('左边Y轴', y_data_1, yaxis_index=0)

    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(x_data)
    # 将line数据通过yaxis_index指向后添加的Y轴
    line.add_yaxis('右边Y轴', y_data_2, yaxis_index=1)

    bar.overlap(line)
    return bar


chart = bar_line_combine_with_two_axis()
chart.render_notebook()
复制代码

5.直方图——双Y轴

在这里插入图片描述

from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ['香蕉', '梨子', '水蜜桃', '核桃', '西瓜', '苹果']
y_data_1 = [random.randint(10, 50) for _ in range(len(x_data))]
y_data_2 = [random.randint(100, 500) for _ in range(len(x_data))]


def bar_with_multiple_axis():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    # 添加一个Y轴
    bar.extend_axis(yaxis=opts.AxisOpts())
    # 分别指定使用的Y轴
    bar.add_yaxis('左边Y轴', y_data_1, yaxis_index=0)
    bar.add_yaxis('右边Y轴', y_data_2, yaxis_index=1)
    return bar


chart = bar_with_multiple_axis()
chart.render_notebook()
复制代码

6.折线图——双X轴

在这里插入图片描述

from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data_1 = ["2020/10/{}".format(i + 1) for i in range(30)]
x_data_2 = ["2019/10/{}".format(i + 1) for i in range(30)]
y_data_1 = [random.randint(10, 50) for _ in range(30)]
y_data_2 = [random.randint(20, 60) for _ in range(30)]


def line_with_two_xaxis():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(x_data_1)
    # 添加一个x轴
    line.extend_axis(xaxis_data=x_data_2, xaxis=opts.AxisOpts())
    line.add_yaxis('下面X轴', y_data_1)
    line.add_yaxis('上面X轴', y_data_2)
    return line


chart = line_with_two_xaxis()
chart.render_notebook()
复制代码

7.缩略轴——inside组件

在这里插入图片描述

from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ["2020/10/{}".format(i + 1) for i in range(30)]

# 随机生成点数据
y_data = [random.randint(10, 20) for i in range(len(x_data))]


def bar_datazoom_inside():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    bar.add_yaxis('', y_data)
    bar.set_global_opts(datazoom_opts=opts.DataZoomOpts(type_='inside',
                                                        range_start=50,   # 设置起止位置,50%-100%
                                                        range_end=100))
    return bar


chart = bar_datazoom_inside()
chart.render_notebook()
复制代码

8.缩略轴——slider组件

在这里插入图片描述

from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ["2020/10/{}".format(i + 1) for i in range(30)]

# 随机生成点数据
y_data = [random.randint(10, 20) for i in range(len(x_data))]


def bar_with_datazoom_slider():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    bar.add_yaxis('', y_data)
    bar.set_global_opts(datazoom_opts=opts.DataZoomOpts(range_start=50,   # 设置起止位置,50%-100%
                                                        range_end=100))
    return bar


chart = bar_with_datazoom_slider()
chart.render_notebook()
复制代码

9.XY轴翻转

在这里插入图片描述

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


def bar_reverse_axis():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    # x,y轴翻转
    bar.reversal_axis()
    return bar


chart = bar_reverse_axis()
chart.render_notebook()
复制代码

10.设置动画效果

在图表加载前会有一段动画效果 在这里插入图片描述

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

"""
更多动画效果可参见:https://echarts.apache.org/examples/zh/editor.html?c=line-easing
"""


def bar_with_animation():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px',
                                      animation_opts=opts.AnimationOpts(animation_delay=1000,   # 动画延时
                                                                        animation_easing='bounceIn')
                                      )
              )
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    return bar


chart = bar_with_animation()
chart.render_notebook()
复制代码

在这里插入图片描述

11.直方图带视觉组件

在这里插入图片描述

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


def bar_with_visualmap_color():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    # 设置视觉组件,默认通过颜色映射数据,数值范围为0-100,可通过min_和max_进行自定义
    bar.set_global_opts(visualmap_opts=opts.VisualMapOpts())
    return bar


chart = bar_with_visualmap_color()
chart.render_notebook()
复制代码

12.设置渐变色(线性渐变)

在这里插入图片描述

from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.commons.utils import JsCode

color_js = """
            new echarts.graphic.LinearGradient(
                                0, 
                                1, 
                                0, 
                                0,
                                [{offset: 0, color: '#008B8B'}, 
                                 {offset: 1, color: '#FF6347'}], 
                                false)
           """


def bar_with_linear_gradient_color():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('', Faker.values(),
                  # 使用JsCode执行渐变色代码
                  itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js)))

    return bar


chart = bar_with_linear_gradient_color()
chart.render_notebook()
复制代码

如果对于你有学习上的帮助一定要收藏起来,更多有关Python学习经验,工具安装包分享,可私信我,关注我,持续更新。

猜你喜欢

转载自juejin.im/post/7018838286076477447

相关文章