柱状/条形图,通过柱形的高度/条形的宽度来表现数据的大小。
Bar.add() 方法签名
add(name, x_axis, y_axis,
is_stack=False,
bar_category_gap='20%', **kwargs)
name -> str
图例名称
x_axis -> list
x 坐标轴数据
y_axis -> list
y 坐标轴数据
is_stack -> bool
数据堆叠,同个类目轴上系列配置相同的 stack 值可以堆叠放置
bar_category_gap -> int/str
类目轴的柱状距离,当设置为 0 时柱状是紧挨着(直方图类型),默认为 '20%'
is_stack
实现数据堆叠
from pyecharts import Bar
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("柱状图数据堆叠示例")
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
bar.render()
Note: 全局配置项要在最后一个 add() 上设置,否侧设置会被冲刷掉。
使用标记点和标记线
from pyecharts import Bar
bar = Bar("标记线和标记点示例")
bar.add("商家A", attr, v1, mark_point=["average"])
bar.add("商家B", attr, v2, mark_line=["min", "max"])
bar.render()
is_convert
交换 XY 轴
from pyecharts import Bar
bar = Bar("x 轴和 y 轴交换")
bar.add("商家A", attr, v1)
bar.add("商家B", attr, v2, is_convert=True)
bar.render()
dataZoom 效果,‘slider’ 类型
import random
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
bar = Bar("Bar - datazoom - slider 示例")
bar.add("", attr, v1, is_label_show=True, is_datazoom_show=True)
bar.render()
dataZoom 效果,‘inside’ 类型
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
bar = Bar("Bar - datazoom - inside 示例")
bar.add(
"",
attr,
v1,
is_datazoom_show=True,
datazoom_type="inside",
datazoom_range=[10, 25],
)
bar.render()
dataZoom 效果,‘both’ 类型
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
bar = Bar("Bar - datazoom - inside 示例")
bar.add(
"",
attr,
v1,
is_datazoom_show=True,
datazoom_type="both",
datazoom_range=[10, 25],
)
bar.render()
多 dataZoom 效果,效果同时支持 X、Y 轴
days = ["{}天".format(i) for i in range(30)]
days_v1 = [random.randint(1, 30) for _ in range(30)]
bar = Bar("Bar - datazoom - xaxis/yaxis 示例")
bar.add(
"",
days,
days_v1,
# 默认为 X 轴,横向
is_datazoom_show=True,
datazoom_type="slider",
datazoom_range=[10, 25],
# 新增额外的 dataZoom 控制条,纵向
is_datazoom_extra_show=True,
datazoom_extra_type="slider",
datazoom_extra_range=[10, 25],
is_toolbox_show=False,
)
bar.render()
Note: datazoom 适合所有平面直角坐标系图形,也就是(Line、Bar、Scatter、EffectScatter、Kline)
当 x 轴或者 y 轴的标签因为过于密集而导致全部显示出来会重叠的话,可采用使标签旋转的方法
attr = ["{}天".format(i) for i in range(20)]
v1 = [random.randint(1, 20) for _ in range(20)]
bar = Bar("坐标轴标签旋转示例")
bar.add("", attr, v1, xaxis_interval=0, xaxis_rotate=30, yaxis_rotate=30)
bar.render()
Note: 可通过设置 xaxis_min/xaxis_max/yaxis_min/yaxis_max 来调整 x 轴和 y 轴上的最大最小值。针对数值轴有效!
Note: 可以通过 label_color 来设置柱状的颜色,如 [’#eee’, ‘#000’],所有的图表类型的图例颜色都可通过 label_color 来修改。
瀑布图示例
attr = ["{}月".format(i) for i in range(1, 8)]
v1 = [0, 100, 200, 300, 400, 220, 250]
v2 = [1000, 800, 600, 500, 450, 400, 300]
bar = Bar("瀑布图示例")
# 利用第一个 add() 图例的颜色为透明,即 'rgba(0,0,0,0)',并且设置 is_stack 标志为 True
bar.add("", attr, v1, label_color=['rgba(0,0,0,0)'], is_stack=True)
bar.add("月份", attr, v2, is_label_show=True, is_stack=True, label_pos='inside')
bar.render()
直方图示例
from pyecharts import Bar
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("直方图示例")
bar.add("", attr * 2, v1 + v2, bar_category_gap=0)
bar.render()
某地的降水量和蒸发量柱状图
attr = ["{}月".format(i) for i in range(1, 13)]
v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
bar = Bar("柱状图示例")
bar.add("蒸发量", attr, v1, mark_line=["average"], mark_point=["max", "min"])
bar.add("降水量", attr, v2, mark_line=["average"], mark_point=["max", "min"])
bar.render()
额外的文本标签
from pyecharts import Bar
bar = Bar("柱状图", extra_html_text_label=["bar_extra_html_text_label", "color:red"])
bar.add("商家A", CLOTHES, clothes_v1, is_stack=True)
bar.add("商家B", CLOTHES, clothes_v2, is_stack=True)
bar.render()
控制 X/Y 轴坐标轴线颜色以及宽度
bar = Bar("柱状图")
bar.add(
"商家A",
CLOTHES,
clothes_v1,
xaxis_line_color="green",
xaxis_line_width=5,
xaxis_label_textcolor="black",
)
bar.render()
进行两次或多次 add 的时候,有一次的某项数据缺失,可用 0 填充
bar = Bar("折线图示例")
bar.add("商家A", CLOTHES, clothes_v1)
bar.add("商家B", CLOTHES, [55, 60, 16, 20, 0, 0])
bar.render()
Bar3D(3D 柱状图)
Bar3D.add() 方法签名
add(name, x_axis, y_axis, data,
grid3d_opacity=1,
grid3d_shading='color', **kwargs)
name -> str
图例名称
x_axis -> str
x 坐标轴数据。需为类目轴,也就是不能是数值。
y_axis -> str
y 坐标轴数据。需为类目轴,也就是不能是数值。
data -> [list], 包含列表的列表
数据项,数据中,每一行是一个『数据项』,每一列属于一个『维度』
grid3d_opacity -> int
3D 笛卡尔坐标系组的透明度(柱状的透明度),默认为 1,完全不透明。
grid3d_shading -> str
三维柱状图中三维图形的着色效果。
color:只显示颜色,不受光照等其它因素的影响。
lambert:通过经典的 lambert 着色表现光照带来的明暗。
realistic:真实感渲染,配合 light.ambientCubemap 和 postEffect 使用可以让展示的画面效果和质感有质的提升。ECharts GL 中使用了基于物理的渲染(PBR) 来表现真实感材质。
from pyecharts import Bar3D
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
x_axis = [
"12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a",
"12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"
]
y_axis = [
"Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday", "Sunday"
]
data = [
[0, 0, 5], [0, 1, 1], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 5, 0],
[0, 6, 0], [0, 7, 0], [0, 8, 0], [0, 9, 0], [0, 10, 0], [0, 11, 2],
[0, 12, 4], [0, 13, 1], [0, 14, 1], [0, 15, 3], [0, 16, 4], [0, 17, 6],
[0, 18, 4], [0, 19, 4], [0, 20, 3], [0, 21, 3], [0, 22, 2], [0, 23, 5],
[1, 0, 7], [1, 1, 0], [1, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0],
[1, 6, 0], [1, 7, 0], [1, 8, 0], [1, 9, 0], [1, 10, 5], [1, 11, 2],
[1, 12, 2], [1, 13, 6], [1, 14, 9], [1, 15, 11], [1, 16, 6], [1, 17, 7],
[1, 18, 8], [1, 19, 12], [1, 20, 5], [1, 21, 5], [1, 22, 7], [1, 23, 2],
[2, 0, 1], [2, 1, 1], [2, 2, 0], [2, 3, 0], [2, 4, 0], [2, 5, 0],
[2, 6, 0], [2, 7, 0], [2, 8, 0], [2, 9, 0], [2, 10, 3], [2, 11, 2],
[2, 12, 1], [2, 13, 9], [2, 14, 8], [2, 15, 10], [2, 16, 6], [2, 17, 5],
[2, 18, 5], [2, 19, 5], [2, 20, 7], [2, 21, 4], [2, 22, 2], [2, 23, 4],
[3, 0, 7], [3, 1, 3], [3, 2, 0], [3, 3, 0], [3, 4, 0], [3, 5, 0],
[3, 6, 0], [3, 7, 0], [3, 8, 1], [3, 9, 0], [3, 10, 5], [3, 11, 4],
[3, 12, 7], [3, 13, 14], [3, 14, 13], [3, 15, 12], [3, 16, 9], [3, 17, 5],
[3, 18, 5], [3, 19, 10], [3, 20, 6], [3, 21, 4], [3, 22, 4], [3, 23, 1],
[4, 0, 1], [4, 1, 3], [4, 2, 0], [4, 3, 0], [4, 4, 0], [4, 5, 1],
[4, 6, 0], [4, 7, 0], [4, 8, 0], [4, 9, 2], [4, 10, 4], [4, 11, 4],
[4, 12, 2], [4, 13, 4], [4, 14, 4], [4, 15, 14], [4, 16, 12], [4, 17, 1],
[4, 18, 8], [4, 19, 5], [4, 20, 3], [4, 21, 7], [4, 22, 3], [4, 23, 0],
[5, 0, 2], [5, 1, 1], [5, 2, 0], [5, 3, 3], [5, 4, 0], [5, 5, 0],
[5, 6, 0], [5, 7, 0], [5, 8, 2], [5, 9, 0], [5, 10, 4], [5, 11, 1],
[5, 12, 5], [5, 13, 10], [5, 14, 5], [5, 15, 7], [5, 16, 11], [5, 17, 6],
[5, 18, 0], [5, 19, 5], [5, 20, 3], [5, 21, 4], [5, 22, 2], [5, 23, 0],
[6, 0, 1], [6, 1, 0], [6, 2, 0], [6, 3, 0], [6, 4, 0], [6, 5, 0],
[6, 6, 0], [6, 7, 0], [6, 8, 0], [6, 9, 0], [6, 10, 1], [6, 11, 0],
[6, 12, 2], [6, 13, 1], [6, 14, 3], [6, 15, 4], [6, 16, 0], [6, 17, 0],
[6, 18, 0], [6, 19, 0], [6, 20, 1], [6, 21, 2], [6, 22, 2], [6, 23, 6]
]
range_color = ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
'#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
bar3d.add(
"",
x_axis,
y_axis,
[[d[1], d[0], d[2]] for d in data],
is_visualmap=True,
visual_range=[0, 20],
visual_range_color=range_color,
grid3d_width=200,
grid3d_depth=80,
)
bar3d.render()
data 中,如 [1, 2, 3] 表示 x 轴的索引为 1,即 “1a”;y 轴的索引为 2,即 “Thursday”;z 轴的数值为 3
设置 grid3d_shading
可以让柱状更真实
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
bar3d.add(
"",
x_axis,
y_axis,
[[d[1], d[0], d[2]] for d in data],
is_visualmap=True,
visual_range=[0, 20],
visual_range_color=range_color,
grid3d_width=200,
grid3d_depth=80,
grid3d_shading="lambert",
)
bar3d.render()
设置 is_grid3d_rotate
启动自动旋转功能
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
bar3d.add(
"",
x_axis,
y_axis,
[[d[1], d[0], d[2]] for d in data],
is_visualmap=True,
visual_range=[0, 20],
visual_range_color=range_color,
grid3d_width=200,
grid3d_depth=80,
is_grid3d_rotate=True,
)
bar3d.render()
设置 grid3d_rotate_speed
调节旋转速度
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
bar3d.add(
"",
x_axis,
y_axis,
[[d[1], d[0], d[2]] for d in data],
is_visualmap=True,
visual_range=[0, 20],
visual_range_color=range_color,
grid3d_width=200,
grid3d_depth=80,
is_grid3d_rotate=True,
grid3d_rotate_speed=180,
)
bar3d.render()