pyecharts

from pyecharts.charts import *
from pyecharts.components import Table
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
import random
import datetime
pyecharts.__version__
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\ipykernel\ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)





'1.8.1'
from pyecharts.globals import CurrentConfig

CurrentConfig.ONLINE_HOST = "https://cdn.kesci.com/lib/pyecharts_assets/"

柱状图

x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [123, 153, 89, 107, 98, 23]


bar = (Bar()
       .add_xaxis(x_data)
       .add_yaxis( '',y_data)
      )

bar.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\ipykernel\ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="cf64fdc51590460ca58b05138b107833" style="width:900px; height:500px;"></div>

折线图

# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [123, 153, 89, 107, 98, 23]

line = (Line()
       .add_xaxis(x_data)
       .add_yaxis('', y_data)
      )

line.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\ipykernel\ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="bd9750c7d613433e98457d876c8d133f" style="width:900px; height:500px;"></div>

箱形图

# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [[random.randint(100, 200) for i in range(10)] for item in x_data]

Box = Boxplot()
Box.add_xaxis(x_data)
Box.add_yaxis("", Box.prepare_data(y_data))
Box.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\ipykernel\ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="c125b618b8ba4b18b08b014e4dcfbf37" style="width:900px; height:500px;"></div>

散点图

# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [123, 153, 89, 107, 98, 23]

scatter = (Scatter()
           .add_xaxis(x_data)
           .add_yaxis('', y_data)
           )

scatter.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\ipykernel\ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="84173591e40b4b9589f5600ca40685b7" style="width:900px; height:500px;"></div>

主题设置

所有主题

from pyecharts.globals import ThemeType

# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data_1 = [123, 153, 89, 107, 98, 23]
y_data_2 = [231, 321, 135, 341, 245, 167]

# 所有内置主题
theme_list = ['chalk',
              'dark',
              'essos',
              'infographic',
              'light',
              'macarons',
              'purple-passion',
              'roma',
              'romantic',
              'shine',
              'vintage',
              'walden',
              'westeros',
              'white',
              'wonderland']

page = Page()
for t in theme_list:

    bar = (
        Bar(init_opts=opts.InitOpts(
            # 设置主题
            theme=t))
        .add_xaxis(x_data)
        .add_yaxis('', y_data_1)
        .add_yaxis('', y_data_2)
        .set_global_opts(title_opts=opts.TitleOpts("Theme-{}".format(t)))
    )
    page.add(bar)

page.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="d6601501b49e4520835cda7943f16986" style="width:900px; height:500px;"></div>
    <div id="299625a1eec74bb59b5d48eea4a6f7a7" style="width:900px; height:500px;"></div>
    <div id="0b4b15606af845dba58f72abb0b20445" style="width:900px; height:500px;"></div>
    <div id="16a524c7771d44d69c73860bdac890ac" style="width:900px; height:500px;"></div>
    <div id="52ac2b7b3ee14ef8bd7157d85cd07491" style="width:900px; height:500px;"></div>
    <div id="2cbbfbc5d0ca46a4b523f9348a9b3163" style="width:900px; height:500px;"></div>
    <div id="46339e45709946fd908a22dcdef3571d" style="width:900px; height:500px;"></div>
    <div id="d0139269aa8444d0bedcfdf00910a088" style="width:900px; height:500px;"></div>
    <div id="96c2be441c104ea7b8e1a88ebd5fe154" style="width:900px; height:500px;"></div>
    <div id="f7cda839eb92497c85c7994f032f0347" style="width:900px; height:500px;"></div>
    <div id="5d3b4a1c199346cdb0854d915d303904" style="width:900px; height:500px;"></div>
    <div id="a0bbf00cb92d40fd9b0c6d80ad7c95d5" style="width:900px; height:500px;"></div>
    <div id="faaecf12a9464666b84ee7a6b66da747" style="width:900px; height:500px;"></div>
    <div id="777599fee1f14352873b06fda76dbfe6" style="width:900px; height:500px;"></div>
    <div id="39f846ed223c44998ceffd4bca35cd76" style="width:900px; height:500px;"></div>

配置项
🗣 分为全局配置项和系列配置项两个部分;

☁️ 为防止混淆,每个cell中只包含一项功能实现;

🌤 每个配置项均附上了官方的代码注释;

全局配置项

class InitOpts(  
    # 图表画布宽度,css 长度单位。  
    width: str = "900px",  

    # 图表画布高度,css 长度单位。  
    height: str = "500px",  

    # 图表 ID,图表唯一标识,用于在多图表时区分。  
    chart_id: Optional[str] = None,  

    # 渲染风格,可选 "canvas", "svg"  
    # # 参考 `全局变量` 章节  
    renderer: str = RenderType.CANVAS,  

    # 网页标题  
    page_title: str = "Awesome-pyecharts",  

    # 图表主题  
    theme: str = "white",  

    # 图表背景颜色  
    bg_color: Optional[str] = None,  

    # 远程 js host,如不设置默认为 https://assets.pyecharts.org/assets/  
    # 参考 `全局变量` 章节  
    js_host: str = "",  

    # 画图动画初始化配置,参考 `global_options.AnimationOpts`  
    animation_opts: Union[AnimationOpts, dict] = AnimationOpts(),  
)
  File "<ipython-input-24-a103c3473b58>", line 3
    width: str = "900px",
         ^
SyntaxError: invalid syntax
# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data_1 = [123, 153, 89, 107, 98, 23]
y_data_2 = [231, 321, 135, 341, 245, 167]

# 画布大小配置
bar = (
    Bar(init_opts=opts.InitOpts(width='600px', height='400px'))
    .add_xaxis(x_data)
    .add_yaxis('', y_data_1)
    .add_yaxis('', y_data_2)
)

bar.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="3b2cd7d607414689b1037c4d635e828b" style="width:600px; height:400px;"></div>

class TitleOpts(
# 主标题文本,支持使用 \n 换行。
title: Optional[str] = None,

# 主标题跳转 URL 链接  
title_link: Optional[str] = None,  

# 主标题跳转链接方式  
# 默认值是: blank  
# 可选参数: 'self', 'blank'  
# 'self' 当前窗口打开; 'blank' 新窗口打开  
title_target: Optional[str] = None,  

# 副标题文本,支持使用 \n 换行。  
subtitle: Optional[str] = None,  

# 副标题跳转 URL 链接  
subtitle_link: Optional[str] = None,  

# 副标题跳转链接方式  
# 默认值是: blank  
# 可选参数: 'self', 'blank'  
# 'self' 当前窗口打开; 'blank' 新窗口打开  
subtitle_target: Optional[str] = None,  

# title 组件离容器左侧的距离。  
# left 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,  
# 也可以是 'left', 'center', 'right'。  
# 如果 left 的值为'left', 'center', 'right',组件会根据相应的位置自动对齐。  
pos_left: Optional[str] = None,  

# title 组件离容器右侧的距离。  
# right 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。  
pos_right: Optional[str] = None,  

# title 组件离容器上侧的距离。  
# top 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,  
# 也可以是 'top', 'middle', 'bottom'。  
# 如果 top 的值为'top', 'middle', 'bottom',组件会根据相应的位置自动对齐。  
pos_top: Optional[str] = None,  

# title 组件离容器下侧的距离。  
# bottom 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。  
pos_bottom: Optional[str] = None,  

# 标题内边距,单位px,默认各方向内边距为5,接受数组分别设定上右下左边距。  
# // 设置内边距为 5  
# padding: 5  
# // 设置上下的内边距为 5,左右的内边距为 10  
# padding: [5, 10]  
# // 分别设置四个方向的内边距  
# padding: [  
#     5,  // 上  
#     10, // 右  
#     5,  // 下  
#     10, // 左  
# ]  
padding: Union[Sequence, Numeric] = 5,  

# 主副标题之间的间距。  
item_gap: Numeric = 10,  

# 主标题字体样式配置项,参考 `series_options.TextStyleOpts`  
title_textstyle_opts: Union[TextStyleOpts, dict, None] = None,  

# 副标题字体样式配置项,参考 `series_options.TextStyleOpts`  
subtitle_textstyle_opts: Union[TextStyleOpts, dict, None] = None,  

)

# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data_1 = [123, 153, 89, 107, 98, 23]
y_data_2 = [231, 321, 135, 341, 245, 167]

# 背景颜色配置
bar = (
    Bar()
    .add_xaxis(x_data)
    .add_yaxis('', y_data_1)
    .add_yaxis('', y_data_2)
    .set_global_opts(title_opts=opts.TitleOpts(title="我是主标题", subtitle='我是副标题'))
)

bar.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="e625ae868b204516989a19d4a4969318" style="width:900px; height:500px;"></div>

标题位置
pos_top, pos_bottom,pos_left, pos_right分别对应 上/下/左/右。

可以接受像 20 这样的具体像素值;

可以接受像 ‘20%’ 这样相对于容器高宽的百分比;

可以接受 ‘left’, ‘center’, ‘right’。

# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data_1 = [123, 153, 89, 107, 98, 23]
y_data_2 = [231, 321, 135, 341, 245, 167]

# 标题位置配置
bar = (
    Bar()
    .add_xaxis(x_data)
    .add_yaxis('', y_data_1)
    .add_yaxis('', y_data_2)
    .set_global_opts(title_opts=opts.TitleOpts(title="我是主标题",
                                               subtitle='我是副标题',
                                               pos_left='center',
                                               pos_top='10%'))
)

bar.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\ipykernel\ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="cc348adb28104c9da63555e52994a287" style="width:900px; height:500px;"></div>
# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data_1 = [123, 153, 89, 107, 98, 23]
y_data_2 = [231, 321, 135, 341, 245, 167]

# 标题样式配置
bar = (
    Bar()
    .add_xaxis(x_data)
    .add_yaxis('', y_data_1)
    .add_yaxis('', y_data_2)
    .set_global_opts(title_opts=opts.TitleOpts(title="我是主标题", subtitle='我是副标题',
                                               # 主标题样式
                                               title_textstyle_opts=opts.TextStyleOpts(
                                                   color='red'),
                                               # 副标题样式
                                               subtitle_textstyle_opts=opts.TextStyleOpts(
                                                   color='green')))
)


bar.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\ipykernel\ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="00e47150e469447396dd7ff3330a28b6" style="width:900px; height:500px;"></div>
# 虚假数据
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data_1 = [123, 153, 89, 107, 98, 23]
y_data_2 = [231, 321, 135, 341, 245, 167]

# 图例配置
bar = (
    Bar()
    .add_xaxis(x_data)
    .add_yaxis('图例1', y_data_1)
    .add_yaxis('图例2', y_data_2)
    .set_global_opts(legend_opts=opts.LegendOpts(is_show=True))
)

bar.render_notebook()
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\ipykernel\ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
E:\ProgramData\Anaconda3\envs\tf2.3\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  super().__init__(init_opts=init_opts)
    <div id="a8234dbfa2874758aa0dbaccdbaa72ef" style="width:900px; height:500px;"></div>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值