文章目录
- 50.Bar:柱状图/条形图
- 50.1 Bar - Stack_bar_percent
- 50.2 Bar - Bar_rotate_xaxis_label
- 50.3 Bar - Bar_stack0
- ?50.4 Bar - Finance_indices_2002(略)
- 50.5 Bar - Bar_base_dict_config
- 50.6 Bar - Bar_with_brush
- 50.7 Bar - Bar_datazoom_slider
- 50.8 Bar - Bar_toolbox
- 50.9 Bar - Bar_waterfall_plot
- 50.10 Bar - Mixed_bar_and_line
- 50.11 Bar - Bar_stack1
- 50.12 Bar - Bar_xyaxis_name
- 50.13 Bar - Bar_base_with_custom_background_image
- 50.14 Bar - Bar_chart_display_delay(动画延迟效果暂时没有加入到代码中)
- 50.15 Bar - Bar_datazoom_slider_vertical
- 50.16 Bar - Bar_histogram_color
- 50.17 Bar - Bar_yaxis_formatter
- 50.18 Bar - Bar_markpoint_type
- 50.19 Bar - Multiple_y_axes
- 50.20 Bar - Bar_custom_bar_color
- 50.21 Bar - Bar_different_series_gap
- 50.22 Bar - Bar_markline_type
- ?50.23 Bar - Bar_border_radius
- 50.24 Bar - Bar_same_series_gap
- 50.25 Bar - Bar_datazoom_inside
- 50.26 Bar - Bar_is_selected
- 50.27 Bar - Bar_reversal_axis
- 50.28 Bar - Bar_markpoint_custom
- 50.29 Bar - Bar_base_with_animation
- 50.30 Bar - Bar_histogram
- 50.31 Bar - Bar_markline_custom
- 50.32 Bar - Bar_base
- 50.33 Bar - Bar_datazoom_both
- 51.Bar实践项目
- 51.1 Bar - Bar_markpoint_custom:add_yaxis()的markpoint_opts=opts.MarkPointOpts()会让set_series_opts()的markpoint_opts=opts.MarkPointOpts()失效
- 51.2 Bar综合例子1
- 51.3 Bar综合例子2
- 51.4 Bar综合例子3
- 51.5 Bar综合例子4
- 51.6 Bar综合例子5
- 51.7 Bar综合例子6:看多y轴的要点
- 51.8 Bar综合例子7:append()来添加opts.BarItem()以区别各条的颜色
50.Bar:柱状图/条形图
https://pyecharts.org/#/zh-cn/rectangular_charts
- class pyecharts.charts.Bar(RectChart)
class Bar(
# 初始化配置项,参考 `global_options.InitOpts`
init_opts: opts.InitOpts = opts.InitOpts()
)
- func pyecharts.charts.Bar.add_yaxis
def add_yaxis(
# 系列名称,用于 tooltip 的显示,legend 的图例筛选。
series_name: str,
# 系列数据
y_axis: Sequence[Numeric, opts.BarItem, dict],
# 是否选中图例
is_selected: bool = True,
# 使用的 x 轴的 index,在单个图表实例中存在多个 x 轴的时候有用。
xaxis_index: Optional[Numeric] = None,
# 使用的 y 轴的 index,在单个图表实例中存在多个 y 轴的时候有用。
yaxis_index: Optional[Numeric] = None,
# 是否启用图例 hover 时的联动高亮
is_legend_hover_link: bool = True,
# 系列 label 颜色
color: Optional[str] = None,
# 是否显示柱条的背景色。通过 backgroundStyle 配置背景样式。
is_show_background: bool = False,
# 每一个柱条的背景样式。需要将 showBackground 设置为 true 时才有效。
background_style: types.Union[types.BarBackground, dict, None] = None,
# 数据堆叠,同个类目轴上系列配置相同的 stack 值可以堆叠放置。
stack: Optional[str] = None,
# 柱条的宽度,不设时自适应。
# 可以是绝对值例如 40 或者百分数例如 '60%'。百分数基于自动计算出的每一类目的宽度。
# 在同一坐标系上,此属性会被多个 'bar' 系列共享。此属性应设置于此坐标系中最后一个 'bar' 系列上才会生效,并且是对此坐标系中所有 'bar' 系列生效。
bar_width: types.Union[types.Numeric, str] = None,
# 柱条的最大宽度。比 barWidth 优先级高。
bar_max_width: types.Union[types.Numeric, str] = None,
# 柱条的最小宽度。在直角坐标系中,默认值是 1。否则默认值是 null。比 barWidth 优先级高。
bar_min_width: types.Union[types.Numeric, str] = None,
# 柱条最小高度,可用于防止某数据项的值过小而影响交互。
bar_min_height: types.Numeric = 0,
# 同一系列的柱间距离,默认为类目间距的 20%,可设固定值
category_gap: Union[Numeric, str] = "20%",
# 不同系列的柱间距离,为百分比(如 '30%',表示柱子宽度的 30%)。
# 如果想要两个系列的柱子重叠,可以设置 gap 为 '-100%'。这在用柱子做背景的时候有用。
gap: Optional[str] = "30%",
# 是否开启大数据量优化,在数据图形特别多而出现卡顿时候可以开启。
# 开启后配合 largeThreshold 在数据量大于指定阈值的时候对绘制进行优化。
# 缺点:优化后不能自定义设置单个数据项的样式。
is_large: bool = False,
# 开启绘制优化的阈值。
large_threshold: types.Numeric = 400,
# 使用 dimensions 定义 series.data 或者 dataset.source 的每个维度的信息。
# 注意:如果使用了 dataset,那么可以在 dataset.source 的第一行/列中给出 dimension 名称。
# 于是就不用在这里指定 dimension。
# 但是,如果在这里指定了 dimensions,那么 ECharts 不再会自动从 dataset.source 的第一行/列中获取维度信息。
dimensions: types.Union[types.Sequence, None] = None,
# 当使用 dataset 时,seriesLayoutBy 指定了 dataset 中用行还是列对应到系列上,也就是说,系列“排布”到 dataset 的行还是列上。可取值:
# 'column':默认,dataset 的列对应于系列,从而 dataset 中每一列是一个维度(dimension)。
# 'row':dataset 的行对应于系列,从而 dataset 中每一行是一个维度(dimension)。
series_layout_by: str = "column",
# 如果 series.data 没有指定,并且 dataset 存在,那么就会使用 dataset。
# datasetIndex 指定本系列使用那个 dataset。
dataset_index: types.Numeric = 0,
# 是否裁剪超出坐标系部分的图形。柱状图:裁掉所有超出坐标系的部分,但是依然保留柱子的宽度
is_clip: bool = True,
# 柱状图所有图形的 zlevel 值。
z_level: types.Numeric = 0,
# 柱状图组件的所有图形的z值。控制图形的前后顺序。
# z值小的图形会被z值大的图形覆盖。
# z相比zlevel优先级更低,而且不会创建新的 Canvas。
z: types.Numeric = 2,
# 标签配置项,参考 `series_options.LabelOpts`
label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),
# 标记点配置项,参考 `series_options.MarkPointOpts`
markpoint_opts: Union[opts.MarkPointOpts, dict, None] = None,
# 标记线配置项,参考 `series_options.MarkLineOpts`
markline_opts: Union[opts.MarkLineOpts, dict, None] = None,
# 提示框组件配置项,参考 `series_options.TooltipOpts`
tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,
# 图元样式配置项,参考 `series_options.ItemStyleOpts`
itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] = None,
# 可以定义 data 的哪个维度被编码成什么。
encode: types.Union[types.JSFunc, dict, None] = None,
)
- BarItem:柱状图数据项
class BarItem(
# 数据项名称。
name: Optional[str] = None,
# 单个数据项的数值。
value: Optional[Numeric] = None,
# 单个柱条文本的样式设置,参考 `series_options.LabelOpts`。
label_opts: Union[LabelOpts, dict, None] = None,
# 图元样式配置项,参考 `series_options.ItemStyleOpts`
itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,
# 提示框组件配置项,参考 `series_options.TooltipOpts`
tooltip_opts: Union[TooltipOpts, dict, None] = None,
)
- BarBackgroundStyleOpts:柱状图背景样式配置
class BarBackgroundStyleOpts(
# 柱条的颜色。
color: str = "rgba(180, 180, 180, 0.2)",
# 柱条的描边颜色。
border_color: str = "#000",
# 柱条的描边宽度,默认不描边。
border_width: Numeric = 0,
# 柱条的描边类型,默认为实线,支持 'dashed', 'dotted'。
border_type: str = "solid",
# 圆角半径,单位px,支持传入数组分别指定 4 个圆角半径。 如:
# barBorderRadius: 5, // 统一设置四个角的圆角大小
# barBorderRadius: [5, 5, 0, 0] //(顺时针左上,右上,右下,左下)
bar_border_radius: Union[Numeric, Sequence] = 0,
# 图形阴影的模糊大小。
# 该属性配合 shadowColor,shadowOffsetX, shadowOffsetY 一起设置图形的阴影效果。
shadow_blur: Optional[Numeric] = None,
# 阴影颜色。支持的格式同color。
shadow_color: Optional[str] = None,
# 阴影水平方向上的偏移距离。
shadow_offset_x: Numeric = 0,
# 阴影垂直方向上的偏移距离。
shadow_offset_y: Numeric = 0,
# 图形透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。
opacity: Optional[Numeric] = None,
)
50.1 Bar - Stack_bar_percent
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType
list2 = [
{"value": 12, "percent": 12 / (12 + 3)},
{"value": 23, "percent": 23 / (23 + 21)},
{"value": 33, "percent": 33 / (33 + 5)},
{"value": 3, "percent": 3 / (3 + 52)},
{"value": 33, "percent": 33 / (33 + 43)},
]
list3 = [
{"value": 3, "percent": 3 / (12 + 3)},
{"value": 21, "percent": 21 / (23 + 21)},
{"value": 5, "percent": 5 / (33 + 5)},
{"value": 52, "percent": 52 / (3 + 52)},
{"value": 43, "percent": 43 / (33 + 43)},
]
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis([1, 2, 3, 4, 5])
.add_yaxis("product1", list2, stack="stack1", category_gap="50%")
.add_yaxis("product2", list3, stack="stack1", category_gap="50%")
.set_series_opts(
label_opts=opts.LabelOpts(
position="right",
formatter=JsCode(
"function(x){return Number(x.data.percent * 100).toFixed() + '%';}"
),
)
)
.render("stack_bar_percent.html")
)
50.2 Bar - Bar_rotate_xaxis_label
from pyecharts import options as opts
from pyecharts.charts import Bar
c = (
Bar()
.add_xaxis(
[
"名字很长的X轴标签1",
"名字很长的X轴标签2",
"名字很长的X轴标签3",
"名字很长的X轴标签4",
"名字很长的X轴标签5",
"名字很长的X轴标签6",
]
)
.add_yaxis("商家A", [10, 20, 30, 40, 50, 40])
.add_yaxis("商家B", [20, 10, 40, 30, 40, 50])
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
title_opts=opts.TitleOpts(title="Bar-旋转X轴标签", subtitle="解决标签名字过长的问题"),
)
.render("bar_rotate_xaxis_label.html")
)
50.3 Bar - Bar_stack0
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
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-堆叠数据(全部)"))
.render("bar_stack0.html")
)
?50.4 Bar - Finance_indices_2002(略)
50.5 Bar - Bar_base_dict_config
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
c = (
Bar({"theme": ThemeType.MACARONS})
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts={"text": "Bar-通过 dict 进行配置", "subtext": "我也是通过 dict 进行配置的"}
)
.render("bar_base_dict_config.html")
)
50.6 Bar - Bar_with_brush
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-Brush示例", subtitle="我是副标题"),
brush_opts=opts.BrushOpts(),
)
.render("bar_with_brush.html")
)
50.7 Bar - Bar_datazoom_slider
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家A", Faker.days_values)
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"),
datazoom_opts=opts.DataZoomOpts(),
)
.render("bar_datazoom_slider.html")
)
50.8 Bar - Bar_toolbox
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-显示 ToolBox"),
toolbox_opts=opts.ToolboxOpts(),
legend_opts=opts.LegendOpts(is_show=False),
)
.render("bar_toolbox.html")
)
50.9 Bar - Bar_waterfall_plot
from pyecharts.charts import Bar
from pyecharts import options as opts
x_data = [f"11月{str(i)}日" for i in range(1, 12)]
y_total = [0, 900, 1245, 1530, 1376, 1376, 1511, 1689, 1856, 1495, 1292]
y_in = [900, 345, 393, "-", "-", 135, 178, 286, "-", "-", "-"]
y_out = ["-", "-", "-", 108, 154, "-", "-", "-", 119, 361, 203]
bar = (
Bar()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="",
yaxis_data=y_total,
stack="总量",
itemstyle_opts=opts.ItemStyleOpts(color="rgba(0,0,0,0)"),
)
.add_yaxis(series_name="收入", yaxis_data=y_in, stack="总量")
.add_yaxis(series_name="支出", yaxis_data=y_out, stack="总量")
.set_global_opts(yaxis_opts=opts.AxisOpts(type_="value"))
.render("bar_waterfall_plot.html")
)
50.10 Bar - Mixed_bar_and_line
import pyecharts.options as opts
from pyecharts.charts import Bar, Line
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://www.echartsjs.com/examples/editor.html?c=mix-line-bar
目前无法实现的功能:
1、暂无
"""
x_data = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
bar = (
Bar(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="蒸发量",
yaxis_data=[
2.0,
4.9,
7.0,
23.2,
25.6,
76.7,
135.6,
162.2,
32.6,
20.0,
6.4,
3.3,
],
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
series_name="降水量",
yaxis_data=[
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),
)
.extend_axis(
yaxis=opts.AxisOpts(
name="温度",
type_="value",
min_=0,
max_=25,
interval=5,
axislabel_opts=opts.LabelOpts(formatter="{value} °C"),
)
)
.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_=250,
interval=50,
axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
)
line = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="平均温度",
yaxis_index=1,
y_axis=[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
label_opts=opts.LabelOpts(is_show=False),
)
)
bar.overlap(line).render("mixed_bar_and_line.html")
50.11 Bar - Bar_stack1
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), stack="stack1")
.add_yaxis("商家B", Faker.values(), stack="stack1")
.add_yaxis("商家C", Faker.values())
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆叠数据(部分)"))
.render("bar_stack1.html")
)
50.12 Bar - Bar_xyaxis_name
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-XY 轴名称"),
yaxis_opts=opts.AxisOpts(name="我是 Y 轴"),
xaxis_opts=opts.AxisOpts(name="我是 X 轴"),
)
.render("bar_xyaxis_name.html")
)
50.13 Bar - Bar_base_with_custom_background_image
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
c = (
Bar(
init_opts=opts.InitOpts(
bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat"}
)
)
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(
title="Bar-背景图基本示例",
subtitle="我是副标题",
title_textstyle_opts=opts.TextStyleOpts(color="white"),
)
)
)
c.add_js_funcs(
"""
var img = new Image(); img.src = 'https://s2.ax1x.com/2019/07/08/ZsS0fK.jpg';
"""
)
c.render("bar_base_with_custom_background_image.html")
50.14 Bar - Bar_chart_display_delay(动画延迟效果暂时没有加入到代码中)
import pyecharts.options as opts
from pyecharts.charts import Bar
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://www.echartsjs.com/examples/editor.html?c=bar-animation-delay
目前无法实现的功能:
1、动画延迟效果暂时没有加入到代码中
"""
category = ["类目{}".format(i) for i in range(0, 100)]
red_bar = [
0,
-8.901463875624668,
-17.025413764148556,
-24.038196249566663,
-29.66504684804471,
-33.699527649688676,
-36.00971978255796,
-36.541005056170455,
-35.31542466107655,
-32.427752866005996,
-28.038563739693934,
-22.364693082297347,
-15.667600860943732,
-8.240217424060843,
-0.3929067389459173,
7.560799717904647,
15.318054209871054,
22.599523033552096,
29.16065418543528,
34.800927952557615,
39.37074152590451,
42.77569739999406,
44.97819140223978,
45.99632376477021,
45.900279829731865,
44.806440199911805,
42.86957840395034,
40.2735832137877,
37.22119936652441,
33.92331243435557,
30.588309963978517,
27.412031986865767,
24.56878097935778,
22.203796820272576,
20.427519715115604,
19.311867685884827,
18.888649906111855,
19.150128087782186,
20.051630602288828,
21.516023200879346,
23.439750867099516,
25.700091656548704,
28.163208735293757,
30.692553648214542,
33.1571635093161,
35.439407573791215,
37.44177367693234,
39.09234039030659,
40.34865356244595,
41.19981246258526,
41.66666666666667,
41.80012531240646,
41.67768039516203,
41.39834040182826,
41.07625507973403,
40.833382300579814,
40.79160029175877,
41.06470032034727,
41.75070457358366,
42.924940903672564,
44.63427081999565,
46.89281122872821,
49.679416561286956,
52.93709961387478,
56.574470884754874,
60.46917221906629,
64.47317623531558,
68.41972346252496,
72.1315793340836,
75.43021771943799,
78.14548044723074,
80.12522637371026,
81.24447108408411,
81.41353029256493,
80.58471628367427,
78.75719600392792,
75.97969924353211,
72.35086229880064,
68.01710226438443,
63.16803467673056,
58.029567166714706,
52.854918421647554,
47.91391949819902,
43.48104807503482,
39.82272085822884,
37.18442111754884,
35.778264289169215,
35.77160292258658,
37.27724241244461,
40.345781666728996,
44.96051012913295,
51.035187614675685,
58.41491053964701,
66.8801325453253,
76.15376513468516,
85.91114110149952,
95.79248672571518,
105.41742429574506,
114.40092042993717,
122.37001313784816,
]
blue_bar = [
-50,
-47.18992898088751,
-42.54426104547181,
-36.290773900754886,
-28.71517529663627,
-20.146937097399626,
-10.94374119697364,
-1.4752538113770308,
7.893046603320797,
16.81528588241657,
24.979206795219028,
32.11821023962515,
38.02096119056733,
42.53821720798438,
45.58667093073836,
47.14973738101559,
47.275355710354944,
46.07100702178889,
43.6962693226927,
40.35333240268025,
36.275975292575026,
31.71756381888028,
26.938653692729076,
22.194784893913152,
17.725026430574392,
13.741778696752679,
10.422266555457615,
7.902063853319403,
6.270884006107842,
5.570756810898967,
5.796594266992678,
6.899033489892203,
8.7893381290192,
11.346045936704996,
14.42297348773613,
17.858132851517098,
21.483081596548438,
25.132218074866262,
28.651548555679597,
31.906490373810854,
34.788333671419466,
37.21906041552118,
39.154309232933485,
40.58437366457342,
41.5332247510366,
42.05565130942339,
42.23270781895,
42.165745792772285,
41.969375711588256,
41.76375960543808,
41.66666666666667,
41.7857343479728,
42.21136481847887,
43.01065209435119,
44.22268037417866,
45.855461823273586,
47.88469584957917,
50.25443606443524,
52.879650371477126,
55.650558977584225,
58.43853958732492,
61.10330341815434,
63.500974294013034,
65.49264961151306,
66.95298925309743,
67.77836838841961,
67.89414332224722,
67.26061575374229,
65.87733853082335,
63.785482681031894,
61.068077697490004,
57.84804048526095,
54.284018163297375,
50.564180830851214,
46.89820707575337,
43.50780217852947,
40.616171775045245,
38.4369379107128,
37.16302649485318,
36.95607267600796,
37.93688225696513,
40.17745279877072,
43.694998595987045,
48.44834150353593,
54.33692802801367,
61.20261650152743,
68.83425165632042,
76.97491319735354,
85.33159602026458,
93.58695857541488,
101.4126683297632,
108.48378461530217,
114.49355390682695,
119.16795429637915,
122.27931702317058,
123.65837448506679,
123.20413594805603,
120.89107255501017,
116.7731992576505,
110.98476877890735,
]
(
Bar(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add_xaxis(xaxis_data=category)
.add_yaxis(
series_name="bar", yaxis_data=red_bar, label_opts=opts.LabelOpts(is_show=False)
)
.add_yaxis(
series_name="bar2",
yaxis_data=blue_bar,
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="柱状图动画延迟"),
xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=False)),
yaxis_opts=opts.AxisOpts(
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
.render("bar_chart_display_delay.html")
)
50.15 Bar - Bar_datazoom_slider_vertical
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家A", Faker.days_values, color=Faker.rand_color())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-垂直)"),
datazoom_opts=opts.DataZoomOpts(orient="vertical"),
)
.render("bar_datazoom_slider_vertical.html")
)
50.16 Bar - Bar_histogram_color
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
x = Faker.dogs + Faker.animal
xlen = len(x)
y = []
for idx, item in enumerate(x):
if idx <= xlen / 2:
y.append(
opts.BarItem(
name=item,
value=(idx + 1) * 10,
itemstyle_opts=opts.ItemStyleOpts(color="#749f83"),
)
)
else:
y.append(
opts.BarItem(
name=item,
value=(xlen + 1 - idx) * 10,
itemstyle_opts=opts.ItemStyleOpts(color="#d48265"),
)
)
c = (
Bar()
.add_xaxis(x)
.add_yaxis("series0", y, category_gap=0, color=Faker.rand_color())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-直方图(颜色区分)"))
.render("bar_histogram_color.html")
)
50.17 Bar - Bar_yaxis_formatter
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-Y 轴 formatter"),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} /月")),
)
.render("bar_yaxis_formatter.html")
)
50.18 Bar - Bar_markpoint_type
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkPoint(指定类型)"))
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
opts.MarkPointItem(type_="average", name="平均值"),
]
),
)
.render("bar_markpoint_type.html")
)
50.19 Bar - Multiple_y_axes
import pyecharts.options as opts
from pyecharts.charts import Bar, Line
"""
Gallery 使用 pyecharts 1.0.0
参考地址: https://www.echartsjs.com/examples/editor.html?c=multiple-y-axis
目前无法实现的功能:
1、暂无
"""
colors = ["#5793f3", "#d14a61", "#675bba"]
x_data = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
legend_list = ["蒸发量", "降水量", "平均温度"]
evaporation_capacity = [
2.0,
4.9,
7.0,
23.2,
25.6,
76.7,
135.6,
162.2,
32.6,
20.0,
6.4,
3.3,
]
rainfall_capacity = [
2.6,
5.9,
9.0,
26.4,
28.7,
70.7,
175.6,
182.2,
48.7,
18.8,
6.0,
2.3,
]
average_temperature = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
bar = (
Bar(init_opts=opts.InitOpts(width="1680px", height="800px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="蒸发量",
yaxis_data=evaporation_capacity,
yaxis_index=0,
color=colors[1],
)
.add_yaxis(
series_name="降水量", yaxis_data=rainfall_capacity, yaxis_index=1, color=colors[0]
)
.extend_axis(
yaxis=opts.AxisOpts(
name="蒸发量",
type_="value",
min_=0,
max_=250,
position="right",
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color=colors[1])
),
axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
)
)
.extend_axis(
yaxis=opts.AxisOpts(
type_="value",
name="温度",
min_=0,
max_=25,
position="left",
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color=colors[2])
),
axislabel_opts=opts.LabelOpts(formatter="{value} °C"),
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
),
)
)
.set_global_opts(
yaxis_opts=opts.AxisOpts(
type_="value",
name="降水量",
min_=0,
max_=250,
position="right",
offset=80,
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color=colors[0])
),
axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
)
)
line = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="平均温度", y_axis=average_temperature, yaxis_index=2, color=colors[2]
)
)
bar.overlap(line).render("multiple_y_axes.html")
50.20 Bar - Bar_custom_bar_color
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
color_function = """
function (params) {
if (params.value > 0 && params.value < 50) {
return 'red';
} else if (params.value > 50 && params.value < 100) {
return 'blue';
}
return 'green';
}
"""
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis(
"商家A",
Faker.values(),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function)),
)
.add_yaxis(
"商家B",
Faker.values(),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function)),
)
.add_yaxis(
"商家C",
Faker.values(),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function)),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-自定义柱状颜色"))
.render("bar_custom_bar_color.html")
)
50.21 Bar - Bar_different_series_gap
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
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-不同系列柱间距离"))
.render("bar_different_series_gap.html")
)
50.22 Bar - Bar_markline_type
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkLine(指定类型)"))
.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="平均值"),
]
),
)
)
?50.23 Bar - Bar_border_radius
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), category_gap="60%")
.set_series_opts(
itemstyle_opts={
"normal": {
"color": JsCode(
"""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(0, 244, 255, 1)'
}, {
offset: 1,
color: 'rgba(0, 77, 167, 1)'
}], false)"""
),
"barBorderRadius": [30, 30, 30, 30],
"shadowColor": "rgb(0, 160, 221)",
}
}
)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-渐变圆柱"))
.render("bar_border_radius.html")
)
50.24 Bar - Bar_same_series_gap
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), category_gap="80%")
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-单系列柱间距离"))
.render("bar_same_series_gap.html")
)
50.25 Bar - Bar_datazoom_inside
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家A", Faker.days_values, color=Faker.rand_color())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-DataZoom(inside)"),
datazoom_opts=opts.DataZoomOpts(type_="inside"),
)
.render("bar_datazoom_inside.html")
)
50.26 Bar - Bar_is_selected
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values(), is_selected=False)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-默认取消显示某 Series"))
.render("bar_is_selected.html")
)
50.27 Bar - Bar_reversal_axis
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-翻转 XY 轴"))
.render("bar_reversal_axis.html")
)
50.28 Bar - Bar_markpoint_custom
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
x, y = Faker.choose(), Faker.values()
c = (
Bar()
.add_xaxis(x)
.add_yaxis(
"商家A",
y,
markpoint_opts=opts.MarkPointOpts(
data=[opts.MarkPointItem(name="自定义标记点", coord=[x[2], y[2]], value=y[2])]
),
)
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkPoint(自定义)"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.render("bar_markpoint_custom.html")
)
50.29 Bar - Bar_base_with_animation
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar(
init_opts=opts.InitOpts(
animation_opts=opts.AnimationOpts(
animation_delay=1000, animation_easing="elasticOut"
)
)
)
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-动画配置基本示例", subtitle="我是副标题"))
.render("bar_base_with_animation.html")
)
50.30 Bar - Bar_histogram
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), category_gap=0, color=Faker.rand_color())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-直方图"))
.render("bar_histogram.html")
)
50.31 Bar - Bar_markline_custom
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkLine(自定义)"))
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(y=50, name="yAxis=50")]
),
)
.render("bar_markline_custom.html")
)
50.32 Bar - Bar_base
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
.render("bar_base.html")
)
50.33 Bar - Bar_datazoom_both
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家A", Faker.days_values, color=Faker.rand_color())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-DataZoom(slider+inside)"),
datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")],
)
.render("bar_datazoom_both.html")
)
51.Bar实践项目
51.1 Bar - Bar_markpoint_custom:add_yaxis()的markpoint_opts=opts.MarkPointOpts()会让set_series_opts()的markpoint_opts=opts.MarkPointOpts()失效
def Bar_markpoint_type2():
x = ['河马', '蟒蛇', '老虎', '大象', '兔子', '熊猫', '狮子']
y1 = [66, 39, 35, 85, 107, 126, 105]
y2 = [89, 86, 116, 51, 137, 76, 146]
c = (
Bar()
.add_xaxis(x)
.add_yaxis("商家A", y1, gap="0%",category_gap="80%")
.add_yaxis(
"商家B",
y2,
gap="0%",
category_gap="80%",
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(name="自定义标记点1", coord=[x[2], y2[2]], value=y2[2])]),)
#.reversal_axis()
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar_markpoint_type2"),
datazoom_opts=opts.DataZoomOpts(),
)
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
##### add_yaxis()的markpoint_opts=opts.MarkPointOpts()会让set_series_opts()的markpoint_opts=opts.MarkPointOpts()失效#####
#markpoint_opts=opts.MarkPointOpts(
# data=[
# opts.MarkPointItem(type_="max", name="最大值"),
# opts.MarkPointItem(type_="min", name="最小值"),
# opts.MarkPointItem(type_="average", name="平均值"),
# ]
#),
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_="min", name="最小值"),
opts.MarkLineItem(type_="max", name="最大值"),
opts.MarkLineItem(type_="average", name="平均值"),
]
),
)
)
return c
51.2 Bar综合例子1
def Stack_bar_percent():
list2 = [
{"value": 50, "percent": 50 / (50 + 0)},
{"value": 34, "percent": 34 / (34 + 37)},
]
list3 = [
{"value": 0, "percent": 0 / (50 + 0)},
{"value": 37, "percent": 37 / (34 + 37)},
]
list4 = [
{"value": 50, "percent": 50 / (50 + 0)},
{"value": 34, "percent": 34 / (34 + 37)},
]
list5 = [
{"value": 0, "percent": 0 / (50 + 0)},
{"value": 37, "percent": 37 / (34 + 37)},
]
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat"}))
.add_xaxis(['2020','2021'])
.add_yaxis("火炬职院1-stack", list2, stack="stack1", category_gap="50%")
.add_yaxis("电子科大1", list3, stack="stack1", category_gap="50%",is_selected=False)
.add_yaxis("火炬职院2", list4, stack="stack0", category_gap="50%")
.add_yaxis("电子科大2", list5, stack="stack0", category_gap="50%")
.set_series_opts(
label_opts=opts.LabelOpts(
position="right",
formatter=JsCode(
"function(x){return Number(x.data.percent * 100).toFixed() + '%';}"
),
)
)
.set_global_opts(
title_opts=TitleOpts(title='报名人数对比'),
xaxis_opts=opts.AxisOpts(name='我是X轴',axislabel_opts=opts.LabelOpts(rotate=-15)),
yaxis_opts=opts.AxisOpts(name='我是Y轴',axislabel_opts=opts.LabelOpts(formatter="{value} /人")),
brush_opts=opts.BrushOpts(),
datazoom_opts=opts.DataZoomOpts(orient="vertical"),
)
)
c.add_js_funcs(
"""
var img = new Image(); img.src = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgdown.baidu.com%2Fimg%2F0%2F200_200%2F1c7d0637ca01803040e087fb44e47654.png&refer=http%3A%2F%2Fgdown.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1632878374&t=508cb059ea0f1889a532d817c67bc76b';
"""
)
return c
51.3 Bar综合例子2
def Bar_markpoint_type():
color_function = """
function (params) {
if (params.value > 0 && params.value < 50) {
return 'red';
} else if (params.value > 50 && params.value < 100) {
return 'blue';
}
return 'green';
}
"""
x = ['河马', '蟒蛇', '老虎', '大象', '兔子', '熊猫', '狮子']
y1 = [66, 39, 35, 85, 107, 126, 105]
y2 = [89, 86, 116, 51, 137, 76, 146]
c = (
Bar()
.add_xaxis(x)
.add_yaxis("商家A", y1, gap="0%",category_gap="80%", itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function)))
.add_yaxis(
"商家B",
y2,
gap="0%",
category_gap="80%",
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(name="自定义标记点1", coord=[x[2], y2[2]], value=y2[2])]),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function)))
#.reversal_axis()
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-MarkPoint(指定类型)"),
datazoom_opts=opts.DataZoomOpts(type_="inside"),
)
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
#opts.MarkPointItem(type_="min", name="最小值"),
#opts.MarkPointItem(type_="average", name="平均值"),
]
),
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_="min", name="最小值"),
opts.MarkLineItem(type_="max", name="最大值"),
opts.MarkLineItem(type_="average", name="平均值"),
]
),
)
)
return c
51.4 Bar综合例子3
def Bar_markpoint_type2():
x = ['河马', '蟒蛇', '老虎', '大象', '兔子', '熊猫', '狮子']
y1 = [66, 39, 35, 85, 107, 126, 105]
y2 = [89, 86, 116, 51, 137, 76, 146]
c = (
Bar()
.add_xaxis(x)
.add_yaxis("商家A", y1, gap="0%",category_gap="80%")
.add_yaxis(
"商家B",
y2,
gap="0%",
category_gap="80%",
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(name="自定义标记点1", coord=[x[2], y2[2]], value=y2[2])]),)
#.reversal_axis()
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar_markpoint_type2"),
datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")],
)
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
##### add_yaxis()的markpoint_opts=opts.MarkPointOpts()会让set_series_opts()的markpoint_opts=opts.MarkPointOpts()失效#####
#markpoint_opts=opts.MarkPointOpts(
# data=[
# opts.MarkPointItem(type_="max", name="最大值"),
# opts.MarkPointItem(type_="min", name="最小值"),
# opts.MarkPointItem(type_="average", name="平均值"),
# ]
#),
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_="min", name="最小值"),
opts.MarkLineItem(type_="max", name="最大值"),
opts.MarkLineItem(type_="average", name="平均值"),
opts.MarkLineItem(y=100, name="yAxis=100"),
]
),
)
)
return c
51.5 Bar综合例子4
def Bar_waterfall_plot_B():
#x_data = [f"{str(i)}月" for i in range(1, 13)]
x_data = ["1月期初","1月入职","1月离职","2月入职","2月离职","3月入职","3月离职","4月入职","4月离职","5月入职","5月离职","6月入职","6月离职"]
y_total = ["-", 4131,4134,4134,4107,4107,4076,4076,4037,4037,4025,4025,4016]
y_in = [4131,22, "-", 13,"-", 65, "-", 69, "-",84, "-", 67, "-"]
y_out = ["-", "-", 19, "-",40, "-", 96, "-", 108,"-", 96, "-",76]
bar = (
Bar()
.add_xaxis(xaxis_data=x_data)
# add_yaxis()的yaxis_data要变成y_axis
.add_yaxis(series_name="",y_axis=y_total,stack="总量",itemstyle_opts=opts.ItemStyleOpts(color="rgba(0,0,0,0)"),)
.add_yaxis(series_name="入职", y_axis=y_in, stack="总量")
.add_yaxis(series_name="离职", y_axis=y_out, stack="总量")
.set_global_opts(title_opts=opts.TitleOpts(title="瀑布图:new"),
xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=False),
axislabel_opts=opts.LabelOpts(rotate=-45)
),
yaxis_opts=opts.AxisOpts(
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
# 增加坐标轴的刻度值最小值设定
min_='3950'
),
)
)
return bar
51.6 Bar综合例子5
def Mixed_bar_and_line_ZZ():
x_data = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
bar = (
Bar(init_opts=opts.InitOpts(width="800px", height="400px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="入职人数",
y_axis=[22,13,65,69,84,67],
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
series_name="离职人数",
y_axis=[19,40,96,108,96,76],
label_opts=opts.LabelOpts(is_show=False),
)
.extend_axis(
yaxis=opts.AxisOpts(
name="总人数",
type_="value",
min_=3500,
max_=4200,
interval=100,
axislabel_opts=opts.LabelOpts(formatter="{value}"),
)
)
.set_global_opts(
title_opts = opts.TitleOpts(title='ZZ图表'),
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
name="月份",
name_location = "center",
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="变动人力",
type_="value",
min_=0,
#max_=100,
interval=10,
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_data)
.add_yaxis(
series_name="在职人数",
yaxis_index=1,
y_axis=[4131, 4144, 4105, 4079, 4058, 4065],
label_opts=opts.LabelOpts(is_show=False),
)
)
bar.overlap(line)
return bar
51.7 Bar综合例子6:看多y轴的要点
注意看多y轴的要点
def Mixed_bar_and_line():
x_data = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
bar = (
Bar(init_opts=opts.InitOpts(width="900px", height="400px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="蒸发量",
y_axis=[2.0,4.9,7.0,23.2,25.6,76.7,135.6,162.2,32.6,20.0,6.4,3.3,],
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
series_name="降水量",
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),
)
.extend_axis(
yaxis=opts.AxisOpts(
name="温度",
type_="value",
min_=0,
max_=25,
interval=5,
#多y轴的要点
position="right",
axislabel_opts=opts.LabelOpts(formatter="{value} °C"),
)
)
.extend_axis(
yaxis=opts.AxisOpts(
name="温度2",
type_="value",
min_=0,
max_=25,
interval=5,
#多y轴的要点
offset=40,
position="right",
axislabel_opts=opts.LabelOpts(formatter="{value} °C"),
)
)
.set_global_opts(
title_opts = opts.TitleOpts(title='降水量图表'),
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
name="月份",
name_location = "center",
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="降水量",
type_="value",
min_=0,
max_=250,
interval=50,
#多y轴的要点
position="left",
axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
)
line = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="平均温度",
yaxis_index=1,
y_axis=[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
label_opts=opts.LabelOpts(is_show=False),
)
)
bar.overlap(line)
return bar
51.8 Bar综合例子7:append()来添加opts.BarItem()以区别各条的颜色
- 最简单的例子
def Bar_histogram_season():
x = ['哈士奇', '萨摩耶', '泰迪', '金毛', '牧羊犬', '吉娃娃', '柯基', '河马', '蟒蛇', '老虎', '大象', '兔子', '熊猫', '狮子']
z = [10,20,30,40,50,60,70,80,90,100,110,100,90,80]
c = (
Bar()
.add_xaxis(x)
.add_yaxis("series0", z, category_gap=0)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-直方图"))
#.render("bar_histogram_color.html")
)
return c
- 复杂一点的例子
def Bar_histogram_season2():
x = ['哈士奇', '萨摩耶', '泰迪', '金毛', '牧羊犬', '吉娃娃', '柯基', '河马', '蟒蛇', '老虎', '大象', '兔子', '熊猫', '狮子']
z = [10,20,30,40,50,60,70,80,90,100,110,100,90,80]
y = []
for idx, ivalue in enumerate(z):
if ivalue > 80:
y.append(
opts.BarItem(
name=x[idx],
value=ivalue,
itemstyle_opts=opts.ItemStyleOpts(color="#749f83"),
)
)
else:
y.append(
opts.BarItem(
name=x[idx],
value=ivalue,
itemstyle_opts=opts.ItemStyleOpts(color="#d48265"),
)
)
c = (
Bar()
.add_xaxis(x)
.add_yaxis("series0", y, category_gap=0)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-直方图"))
#.render("bar_histogram_color.html")
)
return c