[pyecharts学习笔记]——Bar 柱状图/条形图

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,

    # 系列 label 颜色
    color: Optional[str] = None,

    # 数据堆叠,同个类目轴上系列配置相同的 stack 值可以堆叠放置。
    stack: Optional[str] = None,

    # 同一系列的柱间距离,默认为类目间距的 20%,可设固定值
    category_gap: Union[Numeric, str] = "20%",

    # 不同系列的柱间距离,为百分比(如 '30%',表示柱子宽度的 30%)。
    # 如果想要两个系列的柱子重叠,可以设置 gap 为 '-100%'。这在用柱子做背景的时候有用。
    gap: Optional[str] = None,

    # 标签配置项,参考 `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,
)

堆积柱状图

[pyecharts学习笔记]——Bar 柱状图/条形图_第1张图片

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)},  # 12+3  ===> 12
    {"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)},   # 12+3   ===> 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)},
]

# list2=[0.8,0.2,0.3,0.5]
# list3=[0.4,0.6,0.3,0.2]

c = (
     # 初始化配置项(主题)
    Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
    

    .add_xaxis([1, 2, 3, 4, 5])
    
    
    # 堆积柱状图
    # category_gap 同一系列的柱间距离,默认为类目间距的 20%,可设固定值
    # gap 不同系列的柱间距离
    .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="left",
            formatter=JsCode(
                "function(x){return Number(x.data.percent * 100).toFixed() + '%';}"
            ),
        )
    )
    .render("C:/stack_bar_percent.html")
)

[pyecharts学习笔记]——Bar 柱状图/条形图_第2张图片

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
c = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
    .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("C:/bar_stack0.html")
)

[pyecharts学习笔记]——Bar 柱状图/条形图_第3张图片

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType

c = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
    .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("C:/bar_stack1.html")
)

旋转X轴标签

[pyecharts学习笔记]——Bar 柱状图/条形图_第4张图片

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType

c = (
    Bar({"theme": ThemeType.MACARONS})
    .add_xaxis(
        [
            "名字很长的X轴标签1",
            "名字很长的X轴标签2",
            "名字很长的X轴标签3",
            "名字很长的X轴标签4",
            "名字很长的X轴标签5",
            "名字很长的X轴标签6",
        ]
    )
    .add_yaxis("商家A", Faker.values())
    .add_yaxis("商家B", Faker.values())
    #全局配置项
    .set_global_opts(
        #设置x轴  (轴标签旋转-15度(顺时针))
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
        #标题配置项
        title_opts=opts.TitleOpts(title="Bar-旋转X轴标签", subtitle="解决标签名字过长的问题"),
    )
    .render("C:/bar_rotate_xaxis_label.html")
)

Bar-Brush示例

[pyecharts学习笔记]——Bar 柱状图/条形图_第5张图片

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType

c = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
    .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("C:/bar_with_brush.html")
)

数据缩放滑块

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
c = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))#初始化
    .add_xaxis(Faker.days_attrs) #0天-29天
    .add_yaxis("商家A", Faker.days_values) 
    #全局配置项
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"),
        #设置数据缩放滑块
        datazoom_opts=opts.DataZoomOpts(),
    )
    .render("C:/bar_datazoom_slider.html")
)

工具箱

[pyecharts学习笔记]——Bar 柱状图/条形图_第6张图片

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
c = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
    .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")
)

柱状瀑布图

[pyecharts学习笔记]——Bar 柱状图/条形图_第7张图片

from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType

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(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
    .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")) #设置y轴
    .render("C:/bar_waterfall_plot.html")
)

你可能感兴趣的:(#,Pyecharts)