@@ -4278,6 +4278,375 @@ splitline_opts: Union[SplitLineOpts, dict] = SplitLineOpts(),
42784278
42794279OK !
42804280
4281+ # ### 23 pyecharts绘图属性设置方法(下)
4282+
4283+ 
4284+
4285+ ** 分步讲解如何配置为上图**
4286+
4287+ 1 )柱状图显示效果动画对应控制代码:
4288+
4289+ ```python
4290+ animation_opts=opts.AnimationOpts(
4291+ animation_delay = 500 , animation_easing = " cubicOut"
4292+ )
4293+ ```
4294+ 2 )柱状图显示主题对应控制代码:
4295+ ```python
4296+ theme=ThemeType.MACARONS
4297+ ```
4298+ 3 )添加x轴对应的控制代码:
4299+ ```python
4300+ add_xaxis( [" 草莓" , " 芒果" , " 葡萄" , " 雪梨" , " 西瓜" , " 柠檬" , " 车厘子" ]
4301+ ```
4302+ 4 )添加y轴对应的控制代码:
4303+ ```python
4304+ add_yaxis(" A" , Faker.values(),
4305+ ```
4306+ 5 )修改柱间距对应的控制代码:
4307+ ```python
4308+ category_gap=" 50%"
4309+ ```
4310+
4311+ 6 )A系列柱子是否显示对应的控制代码:
4312+ ```python
4313+ is_selected=True
4314+ ```
4315+
4316+ 7 )A系列柱子颜色渐变对应的控制代码:
4317+ ```python
4318+ itemstyle_opts={
4319+ " normal" : {
4320+ " color" : JsCode(""" new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
4321+ offset: 0,
4322+ color: 'rgba(0, 244, 255, 1)'
4323+ }, {
4324+ offset: 1,
4325+ color: 'rgba(0, 77, 167, 1)'
4326+ }], false)""" ),
4327+ " barBorderRadius" : [6 , 6 , 6 , 6 ],
4328+ " shadowColor" : ' rgb(0, 160, 221)' ,
4329+ }}
4330+ ```
4331+ 8 )A系列柱子最大和最小值`标记点` 对应的控制代码:
4332+ ```python
4333+ markpoint_opts=opts.MarkPointOpts(
4334+ data = [
4335+ opts.MarkPointItem(type_ = " max" , name = " 最大值" ),
4336+ opts.MarkPointItem(type_ = " min" , name = " 最小值" ),
4337+ ]
4338+ )
4339+ ```
4340+ 9 )A系列柱子最大和最小值`标记线` 对应的控制代码:
4341+ ```python
4342+ markline_opts=opts.MarkLineOpts(
4343+ data = [
4344+ opts.MarkLineItem(type_ = " min" , name = " 最小值" ),
4345+ opts.MarkLineItem(type_ = " max" , name = " 最大值" )
4346+ ]
4347+ )
4348+ ```
4349+ 10 )柱状图标题对应的控制代码:
4350+ ```python
4351+ title_opts=opts.TitleOpts(title = " Bar-参数使用例子"
4352+ ```
4353+ 11 )柱状图非常有用的toolbox显示对应的控制代码:
4354+ ```python
4355+ toolbox_opts=opts.ToolboxOpts()
4356+ ```
4357+
4358+ 12 )Y轴显示在右侧对应的控制代码:
4359+ ```python
4360+ yaxis_opts=opts.AxisOpts(position = " right" )
4361+ ```
4362+ 13 )Y轴名称对应的控制代码:
4363+ ```python
4364+ yaxis_opts=opts.AxisOpts(,name = " Y轴" )
4365+ ```
4366+ 14 )数据轴区域放大缩小设置对应的控制代码:
4367+ ```python
4368+ datazoom_opts=opts.DataZoomOpts()
4369+ ```
4370+
4371+ ** 完整代码**
4372+
4373+ ```python
4374+ def bar_border_radius():
4375+ c = (
4376+ Bar(init_opts = opts.InitOpts(
4377+ animation_opts = opts.AnimationOpts(
4378+ animation_delay = 500 , animation_easing = " cubicOut"
4379+ ),
4380+ theme = ThemeType.MACARONS ))
4381+ .add_xaxis( [" 草莓" , " 芒果" , " 葡萄" , " 雪梨" , " 西瓜" , " 柠檬" , " 车厘子" ])
4382+ .add_yaxis(" A" , Faker.values(),category_gap = " 50%" ,markpoint_opts = opts.MarkPointOpts(),is_selected = True )
4383+ .set_series_opts(itemstyle_opts = {
4384+ " normal" : {
4385+ " color" : JsCode(""" new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
4386+ offset: 0,
4387+ color: 'rgba(0, 244, 255, 1)'
4388+ }, {
4389+ offset: 1,
4390+ color: 'rgba(0, 77, 167, 1)'
4391+ }], false)""" ),
4392+ " barBorderRadius" : [6 , 6 , 6 , 6 ],
4393+ " shadowColor" : ' rgb(0, 160, 221)' ,
4394+ }}, markpoint_opts = opts.MarkPointOpts(
4395+ data = [
4396+ opts.MarkPointItem(type_ = " max" , name = " 最大值" ),
4397+ opts.MarkPointItem(type_ = " min" , name = " 最小值" ),
4398+ ]
4399+ ),markline_opts = opts.MarkLineOpts(
4400+ data = [
4401+ opts.MarkLineItem(type_ = " min" , name = " 最小值" ),
4402+ opts.MarkLineItem(type_ = " max" , name = " 最大值" )
4403+ ]
4404+ ))
4405+ .set_global_opts(title_opts = opts.TitleOpts(title = " Bar-参数使用例子" ), toolbox_opts = opts.ToolboxOpts(),yaxis_opts = opts.AxisOpts(position = " right" ,name = " Y轴" ),datazoom_opts = opts.DataZoomOpts(),)
4406+
4407+ )
4408+
4409+ return c
4410+
4411+ bar_border_radius().render()
4412+ ```
4413+
4414+ # ### 24 pyecharts原来可以这样快速入门(上)
4415+
4416+ 最近两天,翻看下`pyecharts` 的源码,感叹这个框架写的真棒,思路清晰,设计简洁,通俗易懂,推荐读者们有空也阅读下。
4417+
4418+ bee君是被pyecharts官档介绍- 五个特性所吸引:
4419+
4420+ 1 )简洁的 API 设计,使用如丝滑般流畅,支持链式调用;
4421+
4422+ 2 )囊括了 30 + 种常见图表,应有尽有;
4423+
4424+ 3 )支持主流 Notebook 环境,Jupyter Notebook 和 JupyterLab;
4425+
4426+ 4 )可轻松集成至 Flask,Django 等主流 Web 框架;
4427+
4428+ 5 )高度灵活的配置项,可轻松搭配出精美的图表
4429+
4430+ pyecharts 确实也如上面五个特性介绍那样,使用起来非常方便。那么,有些读者不禁好奇会问,pyecharts 是如何做到的?
4431+
4432+ 我们不妨从pyecharts官档`5 分钟入门pyecharts` 章节开始,由表(最高层函数)及里(底层函数也就是所谓的`源码` ),一探究竟。
4433+
4434+
4435+
4436+ ** 官方第一个例子**
4437+
4438+ 不妨从官档给出的第一个例子说起,
4439+
4440+ ```python
4441+ from pyecharts.charts import Bar
4442+
4443+ bar = Bar()
4444+ bar.add_xaxis([" 衬衫" , " 羊毛衫" , " 雪纺衫" , " 裤子" , " 高跟鞋" , " 袜子" ])
4445+ bar.add_yaxis(" 商家A" , [5 , 20 , 36 , 10 , 75 , 90 ])
4446+ # render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
4447+ # 也可以传入路径参数,如 bar.render("mycharts.html")
4448+ bar.render()
4449+ ```
4450+
4451+ 第一行代码:`from pyecharts.charts import Bar` ,先上一张源码中`包的结构图` :
4452+
4453+ 
4454+
4455+ `bar.py` 模块中定义了类`Bar(RectChart)` ,如下所示:
4456+
4457+ ```python
4458+ class Bar(RectChart):
4459+ """
4460+ <<< Bar Chart >>>
4461+
4462+ Bar chart presents categorical data with rectangular bars
4463+ with heights or lengths proportional to the values that they represent.
4464+ """
4465+ ```
4466+
4467+
4468+
4469+ 这里有读者可能会有以下两个问题:
4470+
4471+ 1 )为什么根据图1 中的包结构,为什么不这么写:`from pyecharts.charts.basic_charts import Bar`
4472+
4473+ 
4474+
4475+
4476+
4477+ 答:请看图2 中`__init__ .py` 模块,文件内容如下,看到导入`charts` 包,而非`charts.basic_charts`
4478+
4479+ ```python
4480+ from pyecharts import charts, commons, components, datasets, options, render, scaffold
4481+ from pyecharts._version import __author__, __version__
4482+ ```
4483+
4484+ 2 )`Bar(RectChart)` 是什么意思
4485+
4486+ 答:RectChart是Bar的子类
4487+
4488+ 下面4 行代码,很好理解,没有特殊性。
4489+
4490+ pyecharts主要两个大版本,0.5 基版本和1.0 基版本,从1.0 基版本开始全面支持`链式调用` ,bee君也很喜爱这种链式调用模式,代码看起来更加紧凑:
4491+
4492+ ```python
4493+ from pyecharts.charts import Bar
4494+
4495+ bar = (
4496+ Bar()
4497+ .add_xaxis([" 衬衫" , " 羊毛衫" , " 雪纺衫" , " 裤子" , " 高跟鞋" , " 袜子" ])
4498+ .add_yaxis(" 商家A" , [5 , 20 , 36 , 10 , 75 , 90 ])
4499+ )
4500+ bar.render()
4501+ ```
4502+
4503+ 实现`链式调用` 也没有多难,保证返回类本身`self ` 即可,如果非要有其他返回对象,那么要提到类内以便被全局共享,
4504+
4505+ add_xaxis函数返回`self `
4506+
4507+ ```python
4508+ def add_xaxis(self , xaxis_data: Sequence):
4509+ self .options[" xAxis" ][0 ].update(data = xaxis_data)
4510+ self ._xaxis_data = xaxis_data
4511+ return self
4512+ ```
4513+
4514+ add_yaxis函数同样返回`self ` .
4515+
4516+ # ### 25 pyecharts原来可以这样快速入门(中)
4517+
4518+ ** 一切皆options**
4519+
4520+
4521+
4522+ pyecharts用起来很爽的另一个重要原因,`参数配置项` 封装的非常nice,通过定义一些列基础的配置组件,比如`global_options.py` 模块中定义的配置对象有以下`27 ` 个
4523+
4524+ ```python
4525+ AngleAxisItem,
4526+ AngleAxisOpts,
4527+ AnimationOpts,
4528+ Axis3DOpts,
4529+ AxisLineOpts,
4530+ AxisOpts,
4531+ AxisPointerOpts,
4532+ AxisTickOpts,
4533+ BrushOpts,
4534+ CalendarOpts,
4535+ DataZoomOpts,
4536+ Grid3DOpts,
4537+ GridOpts,
4538+ InitOpts,
4539+ LegendOpts,
4540+ ParallelAxisOpts,
4541+ ParallelOpts,
4542+ PolarOpts,
4543+ RadarIndicatorItem,
4544+ RadiusAxisItem,
4545+ RadiusAxisOpts,
4546+ SingleAxisOpts,
4547+ TitleOpts,
4548+ ToolBoxFeatureOpts,
4549+ ToolboxOpts,
4550+ TooltipOpts,
4551+ VisualMapOpts,
4552+ ```
4553+
4554+ # ### 26 pyecharts原来可以这样快速入门(下)
4555+
4556+ ** 第二个例子**
4557+
4558+ 了解上面的配置对象后,再看官档给出的第二个例子,与第一个例子相比,增加了一行代码:`set_global_opts` 函数
4559+
4560+ ```python
4561+ from pyecharts.charts import Bar
4562+ from pyecharts import options as opts
4563+
4564+ # V1 版本开始支持链式调用
4565+ # 你所看到的格式其实是 `black` 格式化以后的效果
4566+ # 可以执行 `pip install black` 下载使用
4567+ bar = (
4568+ Bar()
4569+ .add_xaxis([" 衬衫" , " 羊毛衫" , " 雪纺衫" , " 裤子" , " 高跟鞋" , " 袜子" ])
4570+ .add_yaxis(" 商家A" , [5 , 20 , 36 , 10 , 75 , 90 ])
4571+ .set_global_opts(title_opts = opts.TitleOpts(title = " 主标题" , subtitle = " 副标题" ))
4572+
4573+ bar.render()
4574+ ```
4575+
4576+ `set_global_opts` 函数在pyecharts中被高频使用,它定义在底层基础模块`Chart.py` 中,它是前面说到的`RectChart` 的子类,`Bar` 类的孙子类。
4577+
4578+ 浏览下函数的参数:
4579+
4580+ ```python
4581+ def set_global_opts(
4582+ self ,
4583+ title_opts: types.Title = opts.TitleOpts(),
4584+ legend_opts: types.Legend = opts.LegendOpts(),
4585+ tooltip_opts: types.Tooltip = None ,
4586+ toolbox_opts: types.Toolbox = None ,
4587+ brush_opts: types.Brush = None ,
4588+ xaxis_opts: types.Axis = None ,
4589+ yaxis_opts: types.Axis = None ,
4590+ visualmap_opts: types.VisualMap = None ,
4591+ datazoom_opts: types.DataZoom = None ,
4592+ graphic_opts: types.Graphic = None ,
4593+ axispointer_opts: types.AxisPointer = None ,
4594+ ):
4595+ ```
4596+
4597+ 以第二个参数`title_opts` 为例,说明`pyecharts` 中参数赋值的风格。
4598+
4599+ 首先,`title_opts` 是`默认参数` ,默认值为`opts.TitleOpts()` ,这个对象在上一节中,我们提到过,是`global_options.py` 模块中定义的`27 ` 个配置对象种的一个。
4600+
4601+ 其次,pyecharts中为了增强代码可读性,参数的类型都显示的给出。此处它的类型为:`types.Title` . 这是什么类型?它的类型不是`TitleOpts` 吗?不急,看看Title这个类型的定义:
4602+
4603+ ```python
4604+ Title = Union[opts.TitleOpts, dict ]
4605+ ```
4606+
4607+ 原来`Title` 可能是`opts.TitleOpts` , 也可能是python原生的`dict ` . 通过`Union` 实现的就是这种`类型效果` 。所以这就解释了官档中为什么说也可以使用字典配置参数的问题,如下官档:
4608+
4609+ ```python
4610+ # 或者直接使用字典参数
4611+ # .set_global_opts(title_opts={"text": "主标题", "subtext": "副标题"})
4612+ )
4613+ ```
4614+
4615+ 最后,真正的关于图表的标题相关的属性都被封装到TitleOpts类中,比如`title` ,`subtitle` 属性,查看源码,TitleOpts对象还有更多属性:
4616+
4617+ ```python
4618+ class TitleOpts(BasicOpts):
4619+ def __init__ (
4620+ self ,
4621+ title: Optional[str ] = None ,
4622+ title_link: Optional[str ] = None ,
4623+ title_target: Optional[str ] = None ,
4624+ subtitle: Optional[str ] = None ,
4625+ subtitle_link: Optional[str ] = None ,
4626+ subtitle_target: Optional[str ] = None ,
4627+ pos_left: Optional[str ] = None ,
4628+ pos_right: Optional[str ] = None ,
4629+ pos_top: Optional[str ] = None ,
4630+ pos_bottom: Optional[str ] = None ,
4631+ padding: Union[Sequence, Numeric] = 5 ,
4632+ item_gap: Numeric = 10 ,
4633+ title_textstyle_opts: Union[TextStyleOpts, dict , None ] = None ,
4634+ subtitle_textstyle_opts: Union[TextStyleOpts, dict , None ] = None ,
4635+ ):
4636+ ```
4637+
4638+ OK . 到此跟随5 分钟入门的官档,结合两个例子实现的背后源码,探讨了:
4639+
4640+ 1 )与包结构组织相关的`__init__ .py` ;
4641+
4642+ 2 )类的继承关系:Bar-> RectChart-> Chart;
4643+
4644+ 3 )链式调用;
4645+
4646+ 4 )重要的参数配置包`options` ,以TitleOpts类为例,`set_global_opts` 将它装载到Bar类中实现属性自定义。
4647+
4648+
4649+
42814650# ## 六、 Python之坑
42824651
42834652# ### 1 含单个元素的元组
@@ -5252,7 +5621,7 @@ def bet():
52525621- `python解释器` 才是真正执行代码的工具,pycharm里可设置Python解释器,一般去python官网下载python3.7或python3.8版本;如果安装过`anaconda` ,它里面必然也包括一个某版本的Python解释器;pycharm配置python解释器选择哪一个都可以。
52535622- anaconda是python常用包的合集,并提供给我们使用`conda` 命令非常方便的安装各种Python包。
52545623- `conda安装` :我们安装过anaconda软件后,就能够使用conda命令下载anaconda源里(比如中科大镜像源)的包
5255- - `pip安装` :类似于conda安装的python安装包的方法
5624+ - `pip安装` :类似于conda安装的python安装包的方法,更加全面
52565625
52575626** 修改镜像源**
52585627
0 commit comments