Pycharts常用配置项
1 | import os, re |
创建一个条形图
1 | outpath = r"C:\Users\xiaoyx\Desktop" |
链式调用
语法:
1 | a = (Bar() |
参数:
.add_xaxis()
- xaxis_data:指定 x 轴的数据
.add_yaxis()
- series_name: 指定系列名称
- y_axis :指定 y 轴的数据
- stack : 指定 y 轴名称
- color : 指定柱子的颜色
- category_gap:指定柱子的间距
1 | bar=(Bar().add_xaxis(xaxis_data=Faker.choose()) |
配置选项
pyecharts中包括图的标题,颜色主题等,都是通过选项Options配置的。
1 | bar = ( |
InitOpts:初始化配置项
- 可以配置诸如图像宽度,高度,图表主题,背景颜色等
- 图表的宽和高,单位像素
- 图表的主题,常用的有:
- 白色:
.LIGHT
- 黑色:
.DARK
- 暖色:
.INFOGRAPHIC
- 冷色:
.SHINE
- 白色:
1 | c = (Bar( |
1 | c = (Bar( |
1 | c = (Bar( |
1 | c = (Bar( |
全局配置项
使用 options 配置项,在 pyecharts 中,一切皆 Options。
- 全局配置项可通过 set_global_opts 方法设置
提示框配置
- axis_pointer_type=str ; axis_pointer_type=”cross”
- cross : 十字准星指示器
- line : 直线
- shadow: 阴影
- nonw :无
- is_show:是否显示提示框组件,包括提示框浮层和 axisPointer
- trigger:
- axis: 坐标轴触发
- item: 项目触发
- trigger_on:提示框触发的条件,
- mousemove: 鼠标移动时触发。
- click: 鼠标点击时触发。
- mousemove|click: 鼠标移动或点击时触发。
- none:不在 ‘mousemove’ 或 ‘click’ 时触发
- background_color:提示框浮层的背景颜色。
- border_color:提示框浮层的边框颜色。
- border_width:提示框浮层的边框宽
- 提示框的文字样式设置:
- color: 字体颜色
- font_size: 字体尺寸大小
- font_weight: 字体加粗
- font_family: 字体
1 | c = (Bar().add_xaxis(Faker.choose()) |
TitleOpts:标题配置项
语法:.set_global_opts(title_opts=opts.TitleOpts())
参数:
title: 主标题
subtitle: 副标题
pos_left: 离容器左侧的距离
- left/center/right:表示左对齐、居中、右对齐
- 20: 表示距离20个像素
- 20%:表示相对于图标宽度的20%
pos_top:离容器上侧的距离
- ‘top’, ‘middle’, ‘bottom’:靠上,居中,靠下
pos_right:
pos_bottom:
padding: 内边距
- 5:表示上下左右的内边距等于5
item_gap: 主副标题之间的间距
title_textstyle_opts=opts.TextStyleOpts()
- color:
- font_style:’normal’,’italic’,’oblique’
- font_weight:’normal’,’bold’,’bolder’,’lighter’
- font_family:
- font_size:
subtitle_textstyle_opts = opts.TextStyleOpts(…)
1 | c = Bar().add_xaxis(Faker.choose() |
LegendOpts:图例配置项
语法:.set_global_opts(legend_opts=opts.LegendOpts())
参数:
- is_show : 是否显示图例组件
True / False
- type_ : 图例类型
plain
缺省,普通图例。scroll
:可滚动的图例,当图例较多时使用 - selected_mode: 图例选择的模式
True
:默认,开启图例点选False
:关闭single
:单选multiple
:多选
- pos_left 配置方法与标题栏的配置类似
- orient 图例列表的布局朝向
- ‘horizontal’, ‘vertical’
- align:文本对齐方式
auto
,left
,right
- padding:文本的内边距
- item_gap:图例每项之间的间,隔横向布局时为水平间隔,纵向布局时为纵向间隔
- item_width:
- item_height:
- inactive_color = ‘#E6E61A’
- 图例关闭时的颜色。默认是 #ccc
- textstyle_opts:与提示框的配置相同
- legend_icon:
- ‘circle’, 圆
- ‘rect’, 矩形
- ‘roundRect’, 圆角矩形
- ‘triangle’, 三角形
- ‘diamond’, 钻石
- ‘pin’, 饼
- ‘arrow’, 箭头
- ‘none’ 无
- ‘image://url’设置为图片,其中 URL 为图片的链接
1 | c = (Bar().add_xaxis(Faker.choose()) |
DataZoomOpts:区域缩放配置项
图的底部的缩放配置项目。比如是否展示缩放,缩放过程中是否需要实时更新图等。
语法:.set_global_opts(datazoom_opts=[opts.DataZoomOpts(
参数:
- is_show :是否显示缩放组件
- type_ :组件类型,可选 “slider”, “inside
- range_start :数据窗口范围的起始百分比
- range_end :数据窗口范围的结束百分比
- orient :布局方式是横还是竖 ‘horizontal’, ‘vertical’
- xaxis_index:
- yaxis_index :
- is_zoom_lock:设置为 true 则锁定选择区域的大小,也就是说,只能平移,不能缩放9.
1 | c = (Bar().add_xaxis(Faker.choose()) |
ToolBoxFeatureOpts和ToolboxOpts:工具箱配置项
语法:.set_global_opts(title_opts=opts.TitleOpts( )
参数:
- is_show :是否显示工具箱
- orient :工具箱的布局
- pos_left:工具箱的定位
1 | c = (Bar().add_xaxis(Faker.choose()) |
系列配置-LabelOpts 标签配置项
属于系列配置选项
语法:.set_series_opts(label_opts=opts.LabelOpts())
参数:
- is_show=True 是否显示标签
- position 标签的位置 可选 ‘top’,’left’,’right’,’bottom’,’inside’,’insideLeft’,’insideRight’
- font_size 文字的字体大小
- color 文字的颜色
- font_style
- rotate = 90
1 | c = (Bar().add_xaxis(Faker.choose()) |
系列配置-MarkLineOpts标记配置
语法:
1 | .set_series_opts( |
参数:
- name:标注名称
- type_:标注类型 ‘min’ 最大值、’max’ 最大值 、’average’ 平均值
- value_index:指定在哪个维度上指定最大值最小值 ‘0’(xAxis, radiusAxis) 或 ‘1’ (yAxis, angleAxis),只有使用type_时有效
- value_dim:在使用 type 时有效,用于指定在哪个维度上指定最大值最小值。
- coord:标注的坐标。坐标格式视系列的坐标系而定,可以是直角坐标系上的 x, y
- value:标注值,可以不设
- symbol:标记的图形 ‘circle’, ‘rect’, ‘roundRect’, ‘triangle’, ‘diamond’, ‘pin’, ‘arrow’, ‘none’
- symbol_size:标记的大小
- itemstyle:标记点样式配置项,参考 `series_options.ItemStyleOpts
1 | c = (Bar().add_xaxis(Faker.choose()) |
实例配置
- 大小、主题
- 柱状图
- 工具箱
- 标题
- 图例
- 缩放
1 | c = (Bar(init_opts=opts.InitOpts( |