pyecharts
的可视化大法,让人爱不释手。柱状图
是我们最为常用的可视化统计图,本篇主要介绍了pyecharts
的绘制柱状图的常用配置,主要包括以下内容:
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1) # y轴设置
.add_yaxis("学生B", y_value2) # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
)
bar.render_notebook()
在系列配置项中set_series_opts()的标签设置
label_opts=opts.LabelOpts(is_show=False),False为隐藏数字
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1) # y轴设置
.add_yaxis("学生B", y_value2) # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
bar.render_notebook()
全局配置项中,yaxis_opts=opts.AxisOpts(name)以及xaxis_opts=opts.AxisOpts(name)参数设置
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1, stack = "stack1") # y轴设置
.add_yaxis("学生B", y_value2) # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="坐标轴命名"),
yaxis_opts=opts.AxisOpts(name="课程成绩"),
xaxis_opts=opts.AxisOpts(name="课程类别"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
bar.render_notebook()
全局配置项中,xaxis_opts=opts.AxisOpts(totate)参数设置,rotate = -15,垂直x轴标签逆时针旋转15度
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["很长很长的高微","很长很长的高管","很长很长的高计","很长很长的会计","很长很长的金融","很长很长的计算机"]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1) # y轴设置
.add_yaxis("学生B", y_value2) # y轴设置
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
title_opts=opts.TitleOpts(title="旋转x轴标签", subtitle="我是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
bar.render_notebook()
系列配置项中,reversal_axis(),label_opts=opts.LabelOpts(position=“right”)
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1, stack = "stack1") # y轴设置
.add_yaxis("学生B", y_value2) # y轴设置
.reversal_axis()
.set_global_opts(title_opts=opts.TitleOpts(title="旋转坐标轴"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False,position="right"))
)
bar.render_notebook()
一、指定值的标记线
在系列配置项中set_series_opts()的markline_opts=opts.MarkLineOpts()
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1) # y轴设置
.add_yaxis("学生B", y_value2) # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="增加标记线", subtitle="我是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(y=75, name="yAxis=75")])) # 75分合格线
)
bar.render_notebook()
二、平均值、最小值、最大值的标记线
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1) # y轴设置
#.add_yaxis("学生B", y_value2) # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_="min", name="最小值"),
opts.MarkLineItem(type_="max", name="最大值"),
opts.MarkLineItem(type_="average", name="平均值")])
)
)
bar.render_notebook()
三、增加标记点
从“线型”替换成“点型”,markline_opts参数设置变为markpoint_opts
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1) # y轴设置
.add_yaxis("学生B", y_value2) # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="min", name="最小值"),
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="average", name="平均值")])
)
)
bar.render_notebook()
.add_yaxis(category_gap=“80%”)参数设置,值越大表明柱子间的间距越大,柱子宽度越小
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1, category_gap="50%") # y轴设置
.add_yaxis("学生B", y_value2, category_gap="50%") # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="柱子宽度设置", subtitle="我是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
bar.render_notebook()
.add_yaxis(gap=“0%”)参数设置,,值越小表明不同系列之间的柱间距离越小
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1, gap="0%") # y轴设置
.add_yaxis("学生B", y_value2, gap="0%") # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(y=75, name="yAxis=75")])) # 75分合格线
)
bar.render_notebook()
.add_yaxis()参数设置,itemstyle_opts=opts.ItemStyleOpts(color)
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1, itemstyle_opts=opts.ItemStyleOpts(color="gray")) # y轴设置
.add_yaxis("学生B", y_value2, itemstyle_opts=opts.ItemStyleOpts(color="black")) # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
bar.render_notebook()
.add_yaxis()参数设置,stack参数设置
from pyecharts import options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value1plus = [10, 10, 10, 10, 10, 10] #学生1三好学生每门课程加10分
y_value2 = [95, 88, 85, 96, 87, 76]
bar = (
Bar()
.add_xaxis(x_index)
.add_yaxis("学生A", y_value1, stack = "stack1") # y轴设置
.add_yaxis("加分", y_value1plus, stack = "stack1") # y轴设置
.add_yaxis("学生B", y_value2) # y轴设置
.set_global_opts(title_opts=opts.TitleOpts(title="柱状堆叠", subtitle="我是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
bar.render_notebook()
import pyecharts.options as opts
from pyecharts.charts import *
x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
classrank = [30, 25, 10, 60, 15, 5]
bar = (
Bar(init_opts=opts.InitOpts(width="800px", height="400px"))
.add_xaxis(xaxis_data=x_index)
.add_yaxis(
series_name="课程成绩",
y_axis=y_value1,
category_gap="50%",
label_opts=opts.LabelOpts(is_show=False)
)
.extend_axis( # 第二坐标轴
yaxis=opts.AxisOpts(
name="课程排名",
type_="value",
min_=0,
max_=100,
interval=20,
axislabel_opts=opts.LabelOpts(formatter="{value} %") # 设置坐标轴格式
)
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="课程成绩",
type_="value",
min_=0,
max_=100,
interval=20,
axislabel_opts=opts.LabelOpts(formatter="{value} 分"), # 设置坐标轴格式
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
)
line = (
Line()
.add_xaxis(xaxis_data=x_index)
.add_yaxis(
series_name="课程成绩",
yaxis_index=1,
y_axis=classrank,
itemstyle_opts=opts.ItemStyleOpts(color="blue"),
label_opts=opts.LabelOpts(is_show=False),
z=2 # 使折线图显示在柱状图上面
)
)
bar.overlap(line).render_notebook()
[1]https://gallery.pyecharts.org/#/Bar/README