Python入门可视化:22个完整数据可视化小例子,带你玩转可视化~
Posted yunyun云芸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python入门可视化:22个完整数据可视化小例子,带你玩转可视化~相关的知识,希望对你有一定的参考价值。
总共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()
13.设置分割线
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker
def bar_with_custom_splitline():
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())
# 设置分割线
bar.set_global_opts(yaxis_opts=opts.AxisOpts(
splitline_opts=opts.SplitLineOpts(is_show=True,
linestyle_opts=opts.LineStyleOpts(type_='dotted'))))
return bar
chart = bar_with_custom_splitline()
chart.render_notebook()
14.通过字典配置
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker
def bar_with_dict_config():
bar = Bar(init_opts=opts.InitOpts(theme='light',
width='1000px',
height='600px'))
bar.add_xaxis(Faker.choose())
bar.add_yaxis('', Faker.values())
# 通过字典配置itemstyle
bar.set_series_opts(itemstyle_opts=dict(color='#008B8B', opacity=0.8))
return bar
chart = bar_with_dict_config()
chart.render_notebook()
15.GEO 带涟漪效果散点图
from pyecharts.charts import *
from pyecharts import options as opts
def geo_effect_scatter():
geo = Geo(init_opts=opts.InitOpts(theme='light',
width='1000px',
height='600px'))
geo.add_schema(maptype="china")
geo.add("",
[以上是关于Python入门可视化:22个完整数据可视化小例子,带你玩转可视化~的主要内容,如果未能解决你的问题,请参考以下文章
50个数据可视化最有价值的图表(附完整Python代码,建议收藏)