@@ -4191,7 +4191,92 @@ def draw_chart():
41914191draw_chart()
41924192```
41934193
4194+ # ### 22 pyecharts绘图属性设置方法
41944195
4196+ 昨天一位读者朋友问我`pyecharts` 中,y轴如何显示在右侧。先说下如何设置,同时阐述例子君是如何找到找到此属性的。
4197+
4198+ 这是pyecharts中一般的绘图步骤:
4199+ ```python
4200+ from pyecharts.faker import Faker
4201+ from pyecharts import options as opts
4202+ from pyecharts.charts import Bar
4203+ from pyecharts.commons.utils import JsCode
4204+
4205+ def bar_base() -> Bar:
4206+ c = (
4207+ Bar()
4208+ .add_xaxis(Faker.choose())
4209+ .add_yaxis(" 商家A" , Faker.values())
4210+ .set_global_opts(title_opts = opts.TitleOpts(title = " Bar-基本示例" , subtitle = " 我是副标题" ))
4211+ )
4212+ return c
4213+
4214+ bar_base().render(' ./bar.html' )
4215+ ```
4216+ 那么,如何设置y轴显示在右侧,添加一行代码:
4217+ ```python
4218+ .set_global_opts(yaxis_opts = opts.AxisOpts(position = ' right' ))
4219+ ```
4220+ 也就是:
4221+ ```python
4222+ c = (
4223+ Bar()
4224+ .add_xaxis(Faker.choose())
4225+ .add_yaxis(" 商家A" , Faker.values())
4226+ .set_global_opts(title_opts = opts.TitleOpts(title = " Bar-基本示例" , subtitle = " 我是副标题" ))
4227+ .set_global_opts(yaxis_opts = opts.AxisOpts(position = ' right' ))
4228+ )
4229+ ```
4230+
4231+ 如何锁定这个属性,首先应该在set_global_opts函数的参数中找,它一共有以下`11 ` 个设置参数,它们位于模块`charts.py` :
4232+ ```python
4233+ title_opts: types.Title = opts.TitleOpts(),
4234+ legend_opts: types.Legend = opts.LegendOpts(),
4235+ tooltip_opts: types.Tooltip = None ,
4236+ toolbox_opts: types.Toolbox = None ,
4237+ brush_opts: types.Brush = None ,
4238+ xaxis_opts: types.Axis = None ,
4239+ yaxis_opts: types.Axis = None ,
4240+ visualmap_opts: types.VisualMap = None ,
4241+ datazoom_opts: types.DataZoom = None ,
4242+ graphic_opts: types.Graphic = None ,
4243+ axispointer_opts: types.AxisPointer = None ,
4244+ ```
4245+ 因为是设置y轴显示在右侧,自然想到设置参数`yaxis_opts` ,因为其类型为`types.Axis` ,所以再进入`types.py` ,同时定位到`Axis` :
4246+ ```python
4247+ Axis = Union[opts.AxisOpts, dict , None ]
4248+ ```
4249+ Union是pyecharts中可容纳多个类型的并集列表,也就是Axis可能为`opts.AxisOpt` , `dict ` , 或`None ` 三种类型。查看第一个`opts.AxisOpt` 类,它共定义以下`25 ` 个参数:
4250+ ```python
4251+ type_: Optional[str ] = None ,
4252+ name: Optional[str ] = None ,
4253+ is_show: bool = True ,
4254+ is_scale: bool = False ,
4255+ is_inverse: bool = False ,
4256+ name_location: str = " end" ,
4257+ name_gap: Numeric = 15 ,
4258+ name_rotate: Optional[Numeric] = None ,
4259+ interval: Optional[Numeric] = None ,
4260+ grid_index: Numeric = 0 ,
4261+ position: Optional[str ] = None ,
4262+ offset: Numeric = 0 ,
4263+ split_number: Numeric = 5 ,
4264+ boundary_gap: Union[str , bool , None ] = None ,
4265+ min_: Union[Numeric, str , None ] = None ,
4266+ max_: Union[Numeric, str , None ] = None ,
4267+ min_interval: Numeric = 0 ,
4268+ max_interval: Optional[Numeric] = None ,
4269+ axisline_opts: Union[AxisLineOpts, dict , None ] = None ,
4270+ axistick_opts: Union[AxisTickOpts, dict , None ] = None ,
4271+ axislabel_opts: Union[LabelOpts, dict , None ] = None ,
4272+ axispointer_opts: Union[AxisPointerOpts, dict , None ] = None ,
4273+ name_textstyle_opts: Union[TextStyleOpts, dict , None ] = None ,
4274+ splitarea_opts: Union[SplitAreaOpts, dict , None ] = None ,
4275+ splitline_opts: Union[SplitLineOpts, dict ] = SplitLineOpts(),
4276+ ```
4277+ 观察后尝试参数`position` ,结合官档:`https:// pyecharts.org/ # /zh-cn/global_options?id=axisopts%ef%bc%9a%e5%9d%90%e6%a0%87%e8%bd%b4%e9%85%8d%e7%bd%ae%e9%a1%b9`,介绍x轴设置position时有bottom, top, 所以y轴设置很可能就是left,right.
4278+
4279+ OK !
41954280
41964281# ## 六、 Python之坑
41974282
0 commit comments