画图四件套
import pandas as pd
from pyecharts import options as opts
import pyecharts.charts as pyec
from pyecharts.faker import Faker
条形图
横向条形图
横向条形图只需要调用reversal_axis()
即可。
c = (
pyec.Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.reversal_axis()
.set_series_opts(label_opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-翻转 XY 轴"))
)
堆叠条形图
堆叠条形图只要在添加y
轴的函数add_yaxis
上添加stack
参数即可。
c = (
pyec.Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), stack="stack1")
.add_yaxis("商家B", Faker.values(), stack="stack1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆叠数据(全部)"))
)
设置条形图的间距
- category_gap参数:
x
轴每个分类的间距 - gap参数:统一分类下多根柱子间的间距
c = (
pyec.Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), category_gap="80%")
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-单系列柱间距离"))
c = (
pyec.Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), gap="0%")
.add_yaxis("商家B", Faker.values(), gap="0%")
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-不同系列柱间距离"),
)
)
带有网格的条形图
网格图,是在x
轴和y
轴上,都绘制横线,形成的网格。可以在opts.AxisOpts
中通过设置splitline_opts
实现。
x轴旋转角度
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
)
c = (
pyec.Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Scatter-显示分割线"),
xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True),axislabel_opts=opts.LabelOpts(rotate=-15)),
yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
)
)
c.render_notebook()
折线图
基本折线图
import pyecharts.options as opts
from pyecharts.charts import Line
x=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
y=[100,200,300,400,500,400,300]
line=(
Line()
.set_global_opts(
tooltip_opts=opts.TooltipOpts(is_show=False),
xaxis_opts=opts.AxisOpts(type_="category"),
yaxis_opts=opts.AxisOpts(
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
.add_xaxis(xaxis_data=x)
.add_yaxis(
series_name="基本折线图",
y_axis=y,
symbol="emptyCircle",
is_symbol_show=True,
label_opts=opts.LabelOpts(is_show=False),
)
)
line.render_notebook()
- series_name:图形名称
- y_axis:数据
- symbol:标记的图形,pyecharts提供的类型包括’circle’, ‘rect’, ‘roundRect’, ‘triangle’, ‘diamond’, ‘pin’, ‘arrow’, ‘none’,也可以通过 ‘image://url’ 设置为图片,其中 URL 为图片的链接。
- is_symbol_show:是否显示 symbol
连接空数据(折线图)
import pyecharts.options as opts
from pyecharts.charts import Line
x=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
y=[100,200,300,400,None,400,300]
line=(
Line()
.add_xaxis(xaxis_data=x)
.add_yaxis(
series_name="连接空数据(折线图)",
y_axis=y,
is_connect_nones=True
)
.set_global_opts(title_opts=opts.TitleOpts(title="Line-连接空数据"))
)
line.render_notebook()
多条折线重叠
import pyecharts.options as opts
from pyecharts.charts import Line
x=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
y1=[100,200,300,400,100,400,300]
y2=[200,300,200,100,200,300,400]
line=(
Line()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name="y1线",y_axis=y1,symbol="arrow",is_symbol_show=True)
.add_yaxis(series_name="y2线",y_axis=y2)
.set_global_opts(title_opts=opts.TitleOpts(title="Line-多折线重叠"))
)
line.render_notebook()
平滑曲线折线图
import pyecharts.options as opts
from pyecharts.charts import Line
x=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
y1=[100,200,300,400,100,400,300]
y2=[200,300,200,100,200,300,400]
line=(
Line()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name="y1线",y_axis=y1, is_smooth=True)
.add_yaxis(series_name="y2线",y_axis=y2, is_smooth=True)
.set_global_opts(title_opts=opts.TitleOpts(title="Line-多折线重叠"))
)
line.render_notebook()
- is_smooth:平滑曲线标志
阶梯图
import pyecharts.options as opts
from pyecharts.charts import Line
x=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
y1=[100,200,300,400,100,400,300]
line=(
Line()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name="y1线",y_axis=y1, is_step=True)
.set_global_opts(title_opts=opts.TitleOpts(title="Line-阶梯图"))
)
line.render_notebook()
- is_step:阶梯图参数
变换折线的样式
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.faker import Faker
x=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
y1=[100,200,300,400,100,400,300]
line = (
Line()
.add_xaxis(xaxis_data=x)
.add_yaxis(
"y1",
y1,
symbol="triangle",
symbol_size=30,
linestyle_opts=opts.LineStyleOpts(color="red", width=4, type_="dashed"),
itemstyle_opts=opts.ItemStyleOpts(
border_width=3, border_color="yellow", color="blue"
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Line-ItemStyle"))
)
line.render_notebook()
- linestyle_opts:折线样式配置,color设置颜色,width设置宽度,type设置类型,有’solid’, ‘dashed’, 'dotted’三种类型
- itemstyle_opts:图元样式配置,border_width设置描边宽度,border_color设置描边颜色,color设置纹理填充颜色
折线面积图
import pyecharts.options as opts
from pyecharts.charts import Line
x=['星期一','星期二','星期三','星期四','星期五','星期七','星期日']
y1=[100,200,300,400,100,400,300]
y2=[200,300,200,100,200,300,400]
line=(
Line()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name="y1线",y_axis=y1,areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
.add_yaxis(series_name="y2线",y_axis=y2,areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
.set_global_opts(title_opts=opts.TitleOpts(title="Line-多折线重叠"))
)
line.render_notebook()
双横坐标折线图
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.commons.utils import JsCode
js_formatter = """function (params) {
console.log(params);
return '降水量 ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');
}"""
line=(
Line()
.add_xaxis(
xaxis_data=[
"2016-1",
"2016-2",
"2016-3",
"2016-4",
"2016-5",
"2016-6",
"2016-7",
"2016-8",
"2016-9",
"2016-10",
"2016-11",
"2016-12",
]
)
.extend_axis(
xaxis_data=[
"2015-1",
"2015-2",
"2015-3",
"2015-4",
"2015-5",
"2015-6",
"2015-7",
"2015-8",
"2015-9",
"2015-10",
"2015-11",
"2015-12",
],
xaxis=opts.AxisOpts(
type_="category",
axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
axisline_opts=opts.AxisLineOpts(
is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#6e9ef1")
),
axispointer_opts=opts.AxisPointerOpts(
is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
),
),
)
.add_yaxis(
series_name="2015 降水量",
is_smooth=True,
symbol="emptyCircle",
is_symbol_show=False,
color="#d14a61",
y_axis=[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
label_opts=opts.LabelOpts(is_show=False),
linestyle_opts=opts.LineStyleOpts(width=2),
)
.add_yaxis(
series_name="2016 降水量",
is_smooth=True,
symbol="emptyCircle",
is_symbol_show=False,
color="#6e9ef1",
y_axis=[3.9, 5.9, 11.1, 18.7, 48.3, 69.2, 231.6, 46.6, 55.4, 18.4, 10.3, 0.7],
label_opts=opts.LabelOpts(is_show=False),
linestyle_opts=opts.LineStyleOpts(width=2),
)
.set_global_opts(
legend_opts=opts.LegendOpts(),
tooltip_opts=opts.TooltipOpts(trigger="none", axis_pointer_type="cross"),
xaxis_opts=opts.AxisOpts(
type_="category",
axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
axisline_opts=opts.AxisLineOpts(
is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#d14a61")
),
axispointer_opts=opts.AxisPointerOpts(
is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
),
),
yaxis_opts=opts.AxisOpts(
type_="value",
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
),
),
)
)
line.render_notebook()
用电量随时间变化
import pyecharts.options as opts
from pyecharts.charts import Line
x_data = [
"00:00",
"01:15",
"02:30",
"03:45",
"05:00",
"06:15",
"07:30",
"08:45",
"10:00",
"11:15",
"12:30",
"13:45",
"15:00",
"16:15",
"17:30",
"18:45",
"20:00",
"21:15",
"22:30",
"23:45",
]
y_data = [
300,
280,
250,
260,
270,
300,
550,
500,
400,
390,
380,
390,
400,
500,
600,
750,
800,
700,
600,
400,
]
line=(
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="用电量",
y_axis=y_data,
is_smooth=True,
label_opts=opts.LabelOpts(is_show=False),
linestyle_opts=opts.LineStyleOpts(width=2),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="一天用电量分布", subtitle="纯属虚构"),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
xaxis_opts=opts.AxisOpts(boundary_gap=False),
yaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="{value} W"),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True,
dimension=0,
pieces=[
{"lte": 6, "color": "green"},
{"gt": 6, "lte": 8, "color": "red"},
{"gt": 8, "lte": 14, "color": "yellow"},
{"gt": 14, "lte": 17, "color": "red"},
{"gt": 17, "color": "green"},
],
pos_right=0,
pos_bottom=100
),
)
.set_series_opts(
markarea_opts=opts.MarkAreaOpts(
data=[
opts.MarkAreaItem(name="早高峰", x=("07:30", "10:00")),
opts.MarkAreaItem(name="晚高峰", x=("17:30", "21:15")),
]
)
)
)
line.render_notebook()
这里给大家介绍几个关键参数:
①visualmap_opts:视觉映射配置项,可以将折线分段并设置标签(is_piecewise),将不同段设置颜色(pieces);
②markarea_opts:标记区域配置项,data参数可以设置标记区域名称和位置。
折线图不显示折线的原因
- 把x轴坐标转换为str。
箱线图
箱线图使用pyecharts.charts.Boxplot
来实现。
v1 = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980],
[960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790],
]
v2 = [
[890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870],
]
c = Boxplot()
c.add_xaxis(["商品1","商品2"])
c.add_yaxis("A",c.prepare_data(v1))
c.add_yaxis("B",c.prepare_data(v2))
c.render_notebook()
散点图
散点图用的是pyecharts.charts.Scatter
来实现的。
基本使用
c = (
pyec.Scatter()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Scatter-基本示例"))
)
c.render_notebook()
多维散点图
c = (
pyec.Scatter()
.add_xaxis(Faker.choose())
.add_yaxis(
"商家A",
[list(z) for z in zip(Faker.values(), Faker.choose())],
label_opts=pts.LabelOpts(
formatter=JsCode(
"function(params){return params.value[1] +' : '+ params.value[2];}"
)
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Scater-多维度数据"),
tooltip_opts=opts.TooltipOps(
formatter=JsCode(
"function (params) {return params.name + ' : ' + params.value[2];}"
)
),
visualmap_opts=opts.VisualMapOpts(
type_="color", max_=150, min_=20, dimension=1
),
)
crender_notebook()
地理图
中国地图
c = (
pyec.Map()
.add("商家A",[list(z) for z in zip(Faker.provinces, Faker.values())],"china")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-visualMap(连续型)"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
c.render_notebook()
中国局部地图
c = (
pyec.Map()
.add("商家A", [list(z) for z in zip(Faker.guangdong_city, Faker.values())], "广东")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-广东地图"),
visualmap_opts=opts.VisualMapOpts(),
)
)
c.reder_notebook()
- .add(“商家A”, [list(z) for z in zip(Faker.guangdong_city, Faker.values())], “广东”)
- 各省份地图可直接加中文名字显示(如:广东省)
- zip(Faker.guangdong_city, Faker.values())
- 元祖里面的值不接受numpy.int64类型,可接受int类型
- 元祖里面的值不接受numpy.int64类型,可接受int类型
世界地图
c = (
pyec.Map()
.add("商家A", [list(z) for z in zip(Faker.country, Faker.values())],"world")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-世界地图"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
c,render_notebook()