1 Bar Charts¶

More details to follow: https://pyecharts.org/#/zh-cn/rectangular_charts?id=bar%ef%bc%9a%e6%9f%b1%e7%8a%b6%e5%9b%be%e6%9d%a1%e5%bd%a2%e5%9b%be

1.1 柱状图堆叠¶

  • 不同系列的数据使用相同的stack值会堆叠在一起 ;
In [2]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker

def bar_stack():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    # Series with the same stack name will be mounted together.
    bar.add_yaxis('A', Faker.values(), stack='stack1')
    bar.add_yaxis('B', Faker.values(), stack='stack1')
    bar.add_yaxis('C', Faker.values(), stack='stack2')
    bar.add_yaxis('D', Faker.values(), stack='stack2')
    
    return bar

chart = bar_stack()
chart.render_notebook()
Out[2]:

1.2 关闭坐标轴显示¶

  • 碰上类目标签过长的时候,可以选择关闭坐标轴,将数据&标签直接显示在图形中;
In [2]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def bar_with_axis_off():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('', Faker.values(), itemstyle_opts=opts.ItemStyleOpts(color='red'))
    # Enable lable
    bar.set_series_opts(label_opts=opts.LabelOpts(position='insideLeft', formatter='{b}:{c}'))
    # Do not show axis
    bar.set_global_opts(xaxis_opts=opts.AxisOpts(is_show=False),
                        yaxis_opts=opts.AxisOpts(is_show=False))
    # Reverse axis
    bar.reversal_axis()
    return bar


chart = bar_with_axis_off()
chart.render_notebook()
Out[2]:

1.3 自定义坐标轴标签文本¶

In [3]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = list(range(2010, 2020))
y_data = [random.randint(20, 200) for _ in range(len(x_data))]


def bar_with_custom_axis_label():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    bar.add_yaxis('', y_data, is_show_background=True, itemstyle_opts=opts.ItemStyleOpts(color='#33FFFF'))

    bar.set_global_opts(xaxis_opts=opts.AxisOpts(
        # Add "Year" before each year
        axislabel_opts=opts.LabelOpts(formatter='Year {value}')))
    return bar


chart = bar_with_custom_axis_label()
chart.render_notebook()
Out[3]:

1.4 双Y轴¶

In [4]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ['Banana', 'Pear', 'Peach', 'Orange', 'Watermelon', 'Apple']
y_data_1 = [random.randint(10, 50) for _ in range(len(x_data))]
y_data_2 = [random.randint(100, 500) for _ in range(len(x_data))]


def bar_with_multiple_axis():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    # Add a Y-axis
    bar.extend_axis(yaxis=opts.AxisOpts())
    # Assign Y axis
    bar.add_yaxis('Left Y Axis', y_data_1, yaxis_index=0)
    bar.add_yaxis('Right Y Axis', y_data_2, yaxis_index=1)
    return bar


chart = bar_with_multiple_axis()
chart.render_notebook()
Out[4]:

1.5 自定义图例图形¶

  • 当前并不支持同一个图表针对不同的系列设置不同的icon
  • 方案是将多个系列的数据拆分到多个图表设置各自的icon,最后通过grid将图表组合起来
In [5]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

ios_icon = 'path://M24.734 17.003c-0.040-4.053 3.305-5.996 3.454-6.093-1.88-2.751-4.808-3.127-5.851-3.171-2.492-0.252-4.862 1.467-6.127 1.467-1.261 0-3.213-1.43-5.28-1.392-2.716 0.040-5.221 1.579-6.619 4.012-2.822 4.897-0.723 12.151 2.028 16.123 1.344 1.944 2.947 4.127 5.051 4.049 2.026-0.081 2.793-1.311 5.242-1.311s3.138 1.311 5.283 1.271c2.18-0.041 3.562-1.981 4.897-3.931 1.543-2.255 2.179-4.439 2.216-4.551-0.048-0.022-4.252-1.632-4.294-6.473zM20.705 5.11c1.117-1.355 1.871-3.235 1.665-5.11-1.609 0.066-3.559 1.072-4.713 2.423-1.036 1.199-1.942 3.113-1.699 4.951 1.796 0.14 3.629-0.913 4.747-2.264z'
android_icon = 'path://M28 12c-1.1 0-2 0.9-2 2v8c0 1.1 0.9 2 2 2s2-0.9 2-2v-8c0-1.1-0.9-2-2-2zM4 12c-1.1 0-2 0.9-2 2v8c0 1.1 0.9 2 2 2s2-0.9 2-2v-8c0-1.1-0.9-2-2-2zM7 23c0 1.657 1.343 3 3 3v0 4c0 1.1 0.9 2 2 2s2-0.9 2-2v-4h4v4c0 1.1 0.9 2 2 2s2-0.9 2-2v-4c1.657 0 3-1.343 3-3v-11h-18v11zM24.944 10c-0.304-2.746-1.844-5.119-4.051-6.551l1.001-2.001c0.247-0.494 0.047-1.095-0.447-1.342s-1.095-0.047-1.342 0.447l-1.004 2.009-0.261-0.104c-0.893-0.297-1.848-0.458-2.84-0.458s-1.947 0.161-2.84 0.458l-0.261 0.104-1.004-2.009c-0.247-0.494-0.848-0.694-1.342-0.447s-0.694 0.848-0.447 1.342l1.001 2.001c-2.207 1.433-3.747 3.805-4.051 6.551v1h17.944v-1h-0.056zM13 8c-0.552 0-1-0.448-1-1s0.447-0.999 0.998-1c0.001 0 0.002 0 0.003 0s0.001-0 0.002-0c0.551 0.001 0.998 0.448 0.998 1s-0.448 1-1 1zM19 8c-0.552 0-1-0.448-1-1s0.446-0.999 0.998-1c0 0 0.001 0 0.002 0s0.002-0 0.003-0c0.551 0.001 0.998 0.448 0.998 1s-0.448 1-1 1z'

x_data = ["2020/7/{}".format(i + 1) for i in range(10)]

# Generate random data
y_data_1 = [random.randint(10, 20) for i in range(len(x_data))]
y_data_2 = [random.randint(15, 25) for i in range(len(x_data))]


def bar_custom_legend_symbol():
    # 当前并不支持同一个图表针对不同的系列设置不同的icon
    # 方案是将多个系列的数据拆分到多个图表设置各自的icon,最后通过grid将图表组合起来
    ios_bar = Bar()
    ios_bar.add_xaxis(x_data)
    ios_bar.add_yaxis('IOS', y_data_1)
    ios_bar.set_global_opts(legend_opts=opts.LegendOpts(legend_icon=ios_icon,
                                                        pos_right='15%'))

    az_bar = Bar()
    az_bar.add_xaxis(x_data)
    az_bar.add_yaxis('Android', y_data_2)
    az_bar.set_global_opts(legend_opts=opts.LegendOpts(legend_icon=android_icon,
                                                       pos_right='5%'))

    # 将az_bar和ios_bar组合起来
    grid = Grid(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    grid.add(ios_bar, is_control_axis_index=True, grid_opts=opts.GridOpts())
    grid.add(az_bar, is_control_axis_index=True, grid_opts=opts.GridOpts())
    return grid


chart = bar_custom_legend_symbol()
chart.render_notebook()
Out[5]:

1.6 图例选择设置单选¶

In [6]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker

def bar_single_selected():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    # Single selection mode for legend
    bar.set_global_opts(legend_opts=opts.LegendOpts(selected_mode='single'))
    return bar


chart = bar_single_selected()
chart.render_notebook()
Out[6]:

1.7 缩略轴--inside组件¶

In [7]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ["2020/10/{}".format(i + 1) for i in range(30)]

# Generate random data
y_data = [random.randint(10, 20) for i in range(len(x_data))]


def bar_datazoom_inside():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    bar.add_yaxis('', y_data)
    bar.set_global_opts(datazoom_opts=opts.DataZoomOpts(type_='inside',
                                                        range_start=50,   # setup the starting and end point,50%-100%
                                                        range_end=100))
    return bar


chart = bar_datazoom_inside()
chart.render_notebook()
Out[7]:

1.8 缩略轴--slider组件¶

In [8]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ["2020/10/{}".format(i + 1) for i in range(30)]

# Generate random data
y_data = [random.randint(10, 20) for i in range(len(x_data))]


def bar_with_datazoom_slider():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    bar.add_yaxis('', y_data)
    bar.set_global_opts(datazoom_opts=opts.DataZoomOpts(range_start=50,   # setup the starting and end point,50%-100%
                                                        range_end=100))
    return bar


chart = bar_with_datazoom_slider()
chart.render_notebook()
Out[8]:

1.9 缩略轴--slider组件&inside组件¶

In [9]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ["2020/10/{}".format(i + 1) for i in range(30)]

# Generate random data
y_data = [random.randint(10, 20) for i in range(len(x_data))]


def bar_datazoom_both_way():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    bar.add_yaxis('', y_data)
    # transfer 2 datazoom_opts via list
    bar.set_global_opts(datazoom_opts=[opts.DataZoomOpts(),
                                       opts.DataZoomOpts(type_='inside', range_start=50, range_end=100)])
    return bar


chart = bar_datazoom_both_way()
chart.render_notebook()
Out[9]:

1.10 XY轴翻转¶

In [10]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def bar_reverse_axis():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    # Flip X, Y Axis
    bar.reversal_axis()
    return bar


chart = bar_reverse_axis()
chart.render_notebook()
Out[10]:

1.11 设置动画效果¶

  • 在图表加载前会有一段动画效果;
  • 更多动画效果可参见:https://echarts.apache.org/examples/zh/editor.html?c=line-easing
In [2]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker

def bar_with_animation():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px',
                                      animation_opts=opts.AnimationOpts(animation_delay=1000,   # animation delay
                                                                        animation_easing='bounceIn')
                                      )
              )
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    return bar


chart = bar_with_animation()
chart.render_notebook()
Out[2]:

1.12 直方图带视觉组件¶

In [12]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def bar_with_visualmap_color():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    # set visual map using different color to represent the data, data range from 0-100 using min_ and max_ to customize.
    bar.set_global_opts(visualmap_opts=opts.VisualMapOpts())
    return bar


chart = bar_with_visualmap_color()
chart.render_notebook()
Out[12]:

1.13 设置渐变色(线性渐变)¶

In [13]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.commons.utils import JsCode

color_js = """
            new echarts.graphic.LinearGradient(
                                0, 
                                1, 
                                0, 
                                0,
                                [{offset: 0, color: '#008B8B'}, 
                                 {offset: 1, color: '#FF6347'}], 
                                false)
           """


def bar_with_linear_gradient_color():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('', Faker.values(),
                  # using JsCode for color gradient
                  itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js)))

    return bar


chart = bar_with_linear_gradient_color()
chart.render_notebook()
Out[13]:

1.14 设置分割线¶

In [14]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def bar_with_custom_splitline():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    # Define split line
    bar.set_global_opts(yaxis_opts=opts.AxisOpts(
        splitline_opts=opts.SplitLineOpts(is_show=True,
                                          linestyle_opts=opts.LineStyleOpts(type_='dotted'))))
    return bar


chart = bar_with_custom_splitline()
chart.render_notebook()
Out[14]:

1.15 设置分割区域¶

In [15]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def bar_with_custom_splitarea():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    # Define split area
    bar.set_global_opts(yaxis_opts=opts.AxisOpts(
        splitarea_opts=opts.SplitAreaOpts(is_show=True,
                                          areastyle_opts=opts.AreaStyleOpts(opacity=1))))
    return bar


chart = bar_with_custom_splitarea()
chart.render_notebook()
Out[15]:

1.16 通过字典配置¶

In [16]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def bar_with_dict_config():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('', Faker.values())
    # configre itemstyle using dictionary
    bar.set_series_opts(itemstyle_opts=dict(color='#008B8B', opacity=0.8))
    return bar


chart = bar_with_dict_config()
chart.render_notebook()
Out[16]:

1.17 直方图带标记线(最大值,平均值等)¶

In [8]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def bar_with_guide_line():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis('A', Faker.values())
    bar.add_yaxis('B', Faker.values())
    # Configure mark line
    bar.set_series_opts(
        markline_opts=opts.MarkLineOpts(
            data=[
                opts.MarkLineItem(type_="min", name="Min"),
                opts.MarkLineItem(type_="max", name="Max"),
                opts.MarkLineItem(type_="average", name="Avg"),
            ]
        )
    )

    return bar


chart = bar_with_guide_line()
chart.render_notebook()
Out[8]:

2 Scatter Charts¶

More details to follow: https://pyecharts.org/#/zh-cn/rectangular_charts?id=scatter%ef%bc%9a%e6%95%a3%e7%82%b9%e5%9b%be

2.1 更改坐标轴数据类型¶

  • x轴默认数据类型是使用离散型,在使用散点图的时候可调整为数值型;
  • 轴索引:https://zhuanlan.zhihu.com/p/129339040
In [17]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = [random.randint(0, 20) for _ in range(100)]
y_data = [random.randint(0, 50) for _ in range(100)]


def scatter_with_value_xaxis():
    scatter = Scatter(init_opts=opts.InitOpts(theme='light',
                                              width='1000px',
                                              height='600px'))
    scatter.add_xaxis(x_data)
    scatter.add_yaxis('', y_data)
    # X轴默认数据类型为离散数据,设置为数值型
    scatter.set_global_opts(xaxis_opts=opts.AxisOpts(type_="value"))
    return scatter

chart = scatter_with_value_xaxis()
chart.render_notebook()
Out[17]:

2.2 设置渐变色(径向渐变)¶

In [18]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.commons.utils import JsCode

color_js = """new echarts.graphic.RadialGradient(
                    0.4, 0.3, 1,
                    [{offset: 0,
                      color: '#F8F8FF'},
                     {offset: 1,
                      color: '#00BFFF'}
                      ])"""


def scatter_with_radial_gradient_color():
    scatter = Scatter(init_opts=opts.InitOpts(theme='light',
                                              width='1000px',
                                              height='600px'))
    scatter.add_xaxis(Faker.choose())

    scatter.add_yaxis("", Faker.values(),
                      symbol_size=50,
                      itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js)))
    return scatter


chart = scatter_with_radial_gradient_color()
chart.render_notebook()
Out[18]:

2.3 散点图添加视觉组件(图形大小)¶

In [25]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def scatter_with_visualmap_size():
    scatter = Scatter(init_opts=opts.InitOpts(theme='light',
                                              width='1000px',
                                              height='600px'))
    scatter.add_xaxis(Faker.choose())
    scatter.add_yaxis('', Faker.values())
    # Configure visual options
    scatter.set_global_opts(visualmap_opts=opts.VisualMapOpts(type_='size'))
    return scatter


chart = scatter_with_visualmap_size()
chart.render_notebook()
Out[25]:

2.4 散点图添加视觉组件(图形大小&颜色)¶

In [26]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = [random.randint(0, 100) for _ in range(30)]
y_data = [(random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)) for _ in range(30)]


def scatter_with_visualmap_color_size():
    scatter = Scatter(init_opts=opts.InitOpts(theme='light',
                                              width='1000px',
                                              height='600px'))
    scatter.add_xaxis(x_data)
    scatter.add_yaxis('', y_data)
    # Define visual options using list, define dimension.
    scatter.set_global_opts(visualmap_opts=[opts.VisualMapOpts(is_show=True, type_='size', dimension=2, pos_top='20%'),
                                            opts.VisualMapOpts(is_show=True, type_='color', dimension=3,  pos_top='60%')
                                            ],
                            xaxis_opts=opts.AxisOpts(type_="value"))
    return scatter


chart = scatter_with_visualmap_color_size()
chart.render_notebook()
Out[26]:

2.5 散点图添加视觉组件(图形大小&颜色&透明度)¶

In [27]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = [random.randint(0, 100) for _ in range(30)]
y_data = [(random.randint(0, 100), random.randint(0, 100), random.randint(0, 100), random.randint(0, 100))
          for _ in range(30)]

def scatter_with_visualmap_color_size_opacity():
    scatter = Scatter(init_opts=opts.InitOpts(theme='light',
                                              width='1000px',
                                              height='600px'))
    scatter.add_xaxis(x_data)
    scatter.add_yaxis('', y_data)
    # Define visual options using list, define dimension.
    scatter.set_global_opts(visualmap_opts=[opts.VisualMapOpts(is_show=True, type_='size', dimension=2, pos_top='10%'),
                                            opts.VisualMapOpts(is_show=True, type_='color', dimension=3, pos_top='40%'),
                                            opts.VisualMapOpts(is_show=True, type_='opacity', dimension=4,
                                                               range_opacity=[0.2, 1], pos_top='70%')],
                            xaxis_opts=opts.AxisOpts(type_="value"))
    return scatter


chart = scatter_with_visualmap_color_size_opacity()
chart.render_notebook()
Out[27]:

2.6 自定义背景颜色¶

In [28]:
from pyecharts.charts import *
from pyecharts import options as opts
import random


x_data = [random.randint(0, 100) for _ in range(30)]
y_data = [(random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)) for _ in range(30)]


def scatter_with_custom_bgColor():
    scatter = Scatter(
        init_opts=opts.InitOpts(theme='light',
                                width='1000px',
                                height='600px',
                                # Define background color
                                bg_color='#008B8B'))
    scatter.add_xaxis(x_data)
    scatter.add_yaxis('', y_data)
    # Define visual options using list, define dimension.
    scatter.set_global_opts(visualmap_opts=[opts.VisualMapOpts(is_show=True, type_='size', dimension=2),
                                            opts.VisualMapOpts(is_show=True, type_='color', dimension=3)],
                            xaxis_opts=opts.AxisOpts(type_="value"))
    return scatter


chart = scatter_with_custom_bgColor()
chart.render_notebook()
Out[28]:

3 Line Charts¶

More details to follow: https://pyecharts.org/#/zh-cn/rectangular_charts?id=line%ef%bc%9a%e6%8a%98%e7%ba%bf%e9%9d%a2%e7%a7%af%e5%9b%be

3.1 双X轴【折线图】¶

In [19]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data_1 = ["2020/10/{}".format(i + 1) for i in range(30)]
x_data_2 = ["2019/10/{}".format(i + 1) for i in range(30)]
y_data_1 = [random.randint(10, 50) for _ in range(30)]
y_data_2 = [random.randint(20, 60) for _ in range(30)]


def line_with_two_xaxis():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(x_data_1)
    # Add a X-axis
    line.extend_axis(xaxis_data=x_data_2, xaxis=opts.AxisOpts())
    line.add_yaxis('Up X Axis', y_data_1)
    line.add_yaxis('Down X Axis', y_data_2)
    return line


chart = line_with_two_xaxis()
chart.render_notebook()
Out[19]:

3.2 双Y轴【直方图&折线图】¶

  • 实际是Bar和Line两个图表共用同一套坐标体系,将Bar和Line分别指向不同的Y轴;
In [20]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ['Banana', 'Pear', 'Peach', 'Orange', 'Watermellon', 'Apple']
y_data_1 = [random.randint(10, 50) for _ in range(len(x_data))]
y_data_2 = [random.randint(100, 500) for _ in range(len(x_data))]


def bar_line_combine_with_two_axis():
    bar = Bar(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    bar.add_xaxis(x_data)
    # Add a Y-axis
    bar.extend_axis(yaxis=opts.AxisOpts())
    bar.add_yaxis('Left Y Axis', y_data_1, yaxis_index=0)

    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(x_data)
    # Assign line data to the second Y axis
    line.add_yaxis('Right Y Axis', y_data_2, yaxis_index=1, itemstyle_opts=opts.ItemStyleOpts(color='red'))

    bar.overlap(line)
    return bar


chart = bar_line_combine_with_two_axis()
chart.render_notebook()
Out[20]:

3.3 面积图¶

  • 实现方式就是在折线图先填充区域;
In [21]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ["2020/10/{}".format(i + 1) for i in range(30)]

# Generate random values
y_data = [i + random.randint(10, 20) for i in range(len(x_data))]


def line_with_area():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(x_data)
    line.add_yaxis('', y_data)
    # opacity default 0
    line.set_series_opts(areastyle_opts=opts.AreaStyleOpts(opacity=0.5))

    return line


chart = line_with_area()
chart.render_notebook()
Out[21]:

3.4 堆叠面积图¶

In [3]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def line_stack_area():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(Faker.choose())
    line.add_yaxis('A',
                   Faker.values(),
                   stack='stack')
    line.add_yaxis('B',
                   Faker.values(),
                   stack='stack')
    line.add_yaxis('C',
                   Faker.values(),
                   stack='stack')
    # opacity default 0
    line.set_series_opts(areastyle_opts=opts.AreaStyleOpts(opacity=0.5))

    return line


chart = line_stack_area()
chart.render_notebook()
Out[3]:

3.5 自定义线样式(LineStyleOpts)¶

In [4]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def line_with_custom_linestyle():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(Faker.choose())
    line.add_yaxis('Pattern 1',
                   Faker.values(),
                   linestyle_opts=opts.LineStyleOpts(width=5,
                                                     curve=0,
                                                     opacity=0.7,
                                                     type_='solid',
                                                     color='red')
                   )
    line.add_yaxis('Pattern 2',
                   Faker.values(),
                   linestyle_opts=opts.LineStyleOpts(width=3,
                                                     curve=0.5,
                                                     opacity=0.9,
                                                     type_='dashed',
                                                     color='yellow')
                   )
    line.add_yaxis('Pattern 3',
                   Faker.values(),
                   linestyle_opts=opts.LineStyleOpts(width=1,
                                                     curve=1,
                                                     opacity=0.5,
                                                     type_='dotted',
                                                     color='green')
                   )
    return line


chart = line_with_custom_linestyle()
chart.render_notebook()
Out[4]:

3.6 带阴影效果折线图¶

  • linestyle_opts在默认参数中并不支持直接配置阴影效果,可选择通过dict传入shadowOffsetX等参数来实现;
In [5]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

line_style = {
    'normal': {
        'width': 4,  # line width
        'shadowColor': 'rgba(155, 18, 184, .3)',  # shadow color
        'shadowBlur': 10,  # shadow size
        'shadowOffsetY': 10,  # Y Axis shadow offset
        'shadowOffsetX': 10,  # x Axis shadow offset
        'curve': 0.5  # curve ratio
    }
}

x_data = ["2020/10/{}".format(i + 1) for i in range(30)]

# Generate random data
y_data_1 = [i + random.randint(10, 20) for i in range(len(x_data))]
y_data_2 = [i + random.randint(15, 25) for i in range(len(x_data))]


def line_with_shadow():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(x_data)
    line.add_yaxis("Android",
                   y_data_1,
                   is_symbol_show=False,
                   is_smooth=True,
                   # import line style parameters
                   linestyle_opts=line_style)
    line.add_yaxis("IOS",
                   y_data_2,
                   is_symbol_show=False,
                   is_smooth=True,
                   # import line style parameters
                   linestyle_opts=line_style)
    line.set_global_opts(title_opts=opts.TitleOpts(title="Terminal Daily Activity Trend"))
    return line


chart = line_with_shadow()
chart.render_notebook()
Out[5]:

3.7 折线图平滑处理¶

In [6]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def line_with_smooth_connect():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(Faker.choose())
    # configure is_smooth=True for smooth curve
    line.add_yaxis('Smooth Curve', Faker.values(), is_smooth=True)
    line.add_yaxis('Straight Curve', Faker.values(), is_smooth=False)

    return line


chart = line_with_smooth_connect()
chart.render_notebook()
Out[6]:

3.8 折线图带自定义标记点¶

In [9]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ["2020/10/{}".format(i + 1) for i in range(30)]

# Generate random data
y_data = [i + random.randint(10, 20) for i in range(len(x_data))]


def line_with_custom_mark_point():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(x_data)
    line.add_yaxis('', y_data)
    # Define mark point
    line.set_series_opts(
        markpoint_opts=opts.MarkPointOpts(
            data=[opts.MarkPointItem(name="Define mark point", coord=[x_data[2], y_data[2]], value=y_data[2])]
        )
    )

    return line


chart = line_with_custom_mark_point()
chart.render_notebook()
Out[9]:

3.9 折线图带标记区域¶

In [10]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

x_data = ["2020/10/{}".format(i + 1) for i in range(30)]

# Generate random data
y_data = [i + random.randint(10, 20) for i in range(len(x_data))]


def line_with_mark_area():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(x_data)
    line.add_yaxis('', y_data)
    # Define mark area
    line.set_series_opts(
        markarea_opts=opts.MarkAreaOpts(
            data=[
                opts.MarkAreaItem(name="Golden Week", x=("2020/10/1", "2020/10/8"))
            ]
        )
    )

    return line


chart = line_with_mark_area()
chart.render_notebook()
Out[10]:

3.10 折线图带百分号显示¶

In [11]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode


x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [0.98, 0.89, 0.92, 1.07, 0.81, 1.23]

js = """function (param) {return param.name +':'+ Math.floor(param.value[1])+'%';}"""


def line_with_per_show():
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='1000px',
                                        height='600px'))
    line.add_xaxis(x_data)
    # Multiple 100 on data before import
    line.add_yaxis('', [round(v * 100, 0) for v in y_data])
    # Execute js code to display %
    line.set_series_opts(label_opts=opts.LabelOpts(is_show=True,
                                                   formatter=JsCode(js)))

    return line


chart = line_with_per_show()
chart.render_notebook()
Out[11]:

4 Pictorial Bar Charts¶

More details to follow: https://pyecharts.org/#/zh-cn/rectangular_charts?id=pictorialbar%ef%bc%9a%e8%b1%a1%e5%bd%a2%e6%9f%b1%e7%8a%b6%e5%9b%be

4.1 象形图自定义图形¶

In [12]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def pictorialbar_with_custom_symbol():
    pictorialbar = PictorialBar(init_opts=opts.InitOpts(theme='light',
                                                        width='1000px',
                                                        height='600px'))
    pictorialbar.add_xaxis(Faker.choose())
    pictorialbar.add_yaxis('A',
                           Faker.values(),
                           symbol_size=20,
                           # Define symbol
                           symbol='path://M100,0 L41.22,180.90 L195.10,69.09 L4.89,69.09 L158.77,180.90 z',
                           symbol_repeat="fixed",
                           is_symbol_clip=True
                           )

    return pictorialbar


chart = pictorialbar_with_custom_symbol()
chart.render_notebook()
Out[12]:

5 Liquid Charts¶

More details to follow: https://pyecharts.org/#/zh-cn/basic_charts?id=liquid%ef%bc%9a%e6%b0%b4%e7%90%83%e5%9b%be

5.1 水球图自定义图形¶

In [13]:
from pyecharts.charts import *
from pyecharts import options as opts

ios_icon = 'path://M24.734 17.003c-0.040-4.053 3.305-5.996 3.454-6.093-1.88-2.751-4.808-3.127-5.851-3.171-2.492-0.252-4.862 1.467-6.127 1.467-1.261 0-3.213-1.43-5.28-1.392-2.716 0.040-5.221 1.579-6.619 4.012-2.822 4.897-0.723 12.151 2.028 16.123 1.344 1.944 2.947 4.127 5.051 4.049 2.026-0.081 2.793-1.311 5.242-1.311s3.138 1.311 5.283 1.271c2.18-0.041 3.562-1.981 4.897-3.931 1.543-2.255 2.179-4.439 2.216-4.551-0.048-0.022-4.252-1.632-4.294-6.473zM20.705 5.11c1.117-1.355 1.871-3.235 1.665-5.11-1.609 0.066-3.559 1.072-4.713 2.423-1.036 1.199-1.942 3.113-1.699 4.951 1.796 0.14 3.629-0.913 4.747-2.264z'


def liquid_with_custom_shape():
    liquid = Liquid(init_opts=opts.InitOpts(theme='light',
                                            width='1000px',
                                            height='600px'))
    liquid.add('', [0.4, 0.5], shape=ios_icon)

    return liquid


chart = liquid_with_custom_shape()
chart.render_notebook()
Out[13]:

6 Calendar Charts¶

More details to follow: https://pyecharts.org/#/zh-cn/basic_charts?id=calendar%ef%bc%9a%e6%97%a5%e5%8e%86%e5%9b%be

6.1 日历图自定义cell¶

In [14]:
from pyecharts.charts import *
from pyecharts import options as opts
import datetime

# Covid data in US
data = [(datetime.datetime(2020, 1, 21, 0, 0), 1), (datetime.datetime(2020, 1, 22, 0, 0), 1),
        (datetime.datetime(2020, 1, 23, 0, 0), 1), (datetime.datetime(2020, 1, 24, 0, 0), 2),
        (datetime.datetime(2020, 1, 25, 0, 0), 2), (datetime.datetime(2020, 1, 26, 0, 0), 5),
        (datetime.datetime(2020, 1, 27, 0, 0), 5), (datetime.datetime(2020, 1, 28, 0, 0), 5),
        (datetime.datetime(2020, 1, 29, 0, 0), 5), (datetime.datetime(2020, 1, 30, 0, 0), 5),
        (datetime.datetime(2020, 1, 31, 0, 0), 7), (datetime.datetime(2020, 2, 1, 0, 0), 8),
        (datetime.datetime(2020, 2, 2, 0, 0), 8), (datetime.datetime(2020, 2, 3, 0, 0), 11),
        (datetime.datetime(2020, 2, 4, 0, 0), 11), (datetime.datetime(2020, 2, 5, 0, 0), 11),
        (datetime.datetime(2020, 2, 6, 0, 0), 11), (datetime.datetime(2020, 2, 7, 0, 0), 11),
        (datetime.datetime(2020, 2, 8, 0, 0), 11), (datetime.datetime(2020, 2, 9, 0, 0), 11),
        (datetime.datetime(2020, 2, 10, 0, 0), 11), (datetime.datetime(2020, 2, 11, 0, 0), 12),
        (datetime.datetime(2020, 2, 12, 0, 0), 12), (datetime.datetime(2020, 2, 13, 0, 0), 13),
        (datetime.datetime(2020, 2, 14, 0, 0), 13), (datetime.datetime(2020, 2, 15, 0, 0), 13),
        (datetime.datetime(2020, 2, 16, 0, 0), 13), (datetime.datetime(2020, 2, 17, 0, 0), 13),
        (datetime.datetime(2020, 2, 18, 0, 0), 13), (datetime.datetime(2020, 2, 19, 0, 0), 13),
        (datetime.datetime(2020, 2, 20, 0, 0), 13), (datetime.datetime(2020, 2, 21, 0, 0), 15),
        (datetime.datetime(2020, 2, 22, 0, 0), 15), (datetime.datetime(2020, 2, 23, 0, 0), 15),
        (datetime.datetime(2020, 2, 24, 0, 0), 15), (datetime.datetime(2020, 2, 25, 0, 0), 15),
        (datetime.datetime(2020, 2, 26, 0, 0), 15), (datetime.datetime(2020, 2, 27, 0, 0), 16),
        (datetime.datetime(2020, 2, 28, 0, 0), 16), (datetime.datetime(2020, 2, 29, 0, 0), 24),
        (datetime.datetime(2020, 3, 1, 0, 0), 30), (datetime.datetime(2020, 3, 2, 0, 0), 53),
        (datetime.datetime(2020, 3, 3, 0, 0), 73), (datetime.datetime(2020, 3, 4, 0, 0), 104),
        (datetime.datetime(2020, 3, 5, 0, 0), 174), (datetime.datetime(2020, 3, 6, 0, 0), 222),
        (datetime.datetime(2020, 3, 7, 0, 0), 337), (datetime.datetime(2020, 3, 8, 0, 0), 451),
        (datetime.datetime(2020, 3, 9, 0, 0), 519), (datetime.datetime(2020, 3, 10, 0, 0), 711),
        (datetime.datetime(2020, 3, 11, 0, 0), 1109), (datetime.datetime(2020, 3, 12, 0, 0), 1561),
        (datetime.datetime(2020, 3, 13, 0, 0), 2157), (datetime.datetime(2020, 3, 14, 0, 0), 2870),
        (datetime.datetime(2020, 3, 15, 0, 0), 2968), (datetime.datetime(2020, 3, 16, 0, 0), 4360),
        (datetime.datetime(2020, 3, 17, 0, 0), 6141), (datetime.datetime(2020, 3, 18, 0, 0), 8914),
        (datetime.datetime(2020, 3, 19, 0, 0), 14153), (datetime.datetime(2020, 3, 20, 0, 0), 19479),
        (datetime.datetime(2020, 3, 21, 0, 0), 25818), (datetime.datetime(2020, 3, 22, 0, 0), 33756),
        (datetime.datetime(2020, 3, 23, 0, 0), 43845), (datetime.datetime(2020, 3, 24, 0, 0), 54108),
        (datetime.datetime(2020, 3, 25, 0, 0), 66044), (datetime.datetime(2020, 3, 26, 0, 0), 84080),
        (datetime.datetime(2020, 3, 27, 0, 0), 102254), (datetime.datetime(2020, 3, 28, 0, 0), 122054),
        (datetime.datetime(2020, 3, 29, 0, 0), 141194), (datetime.datetime(2020, 3, 30, 0, 0), 162690),
        (datetime.datetime(2020, 3, 31, 0, 0), 188701), (datetime.datetime(2020, 4, 1, 0, 0), 214194),
        (datetime.datetime(2020, 4, 2, 0, 0), 244593), (datetime.datetime(2020, 4, 3, 0, 0), 276535),
        (datetime.datetime(2020, 4, 4, 0, 0), 309699), (datetime.datetime(2020, 4, 5, 0, 0), 337573),
        (datetime.datetime(2020, 4, 6, 0, 0), 367210), (datetime.datetime(2020, 4, 7, 0, 0), 397992),
        (datetime.datetime(2020, 4, 8, 0, 0), 429686), (datetime.datetime(2020, 4, 9, 0, 0), 464442),
        (datetime.datetime(2020, 4, 10, 0, 0), 497943), (datetime.datetime(2020, 4, 11, 0, 0), 527958),
        (datetime.datetime(2020, 4, 12, 0, 0), 556517), (datetime.datetime(2020, 4, 13, 0, 0), 581810),
        (datetime.datetime(2020, 4, 14, 0, 0), 608845), (datetime.datetime(2020, 4, 15, 0, 0), 637974),
        (datetime.datetime(2020, 4, 16, 0, 0), 669272), (datetime.datetime(2020, 4, 17, 0, 0), 701996),
        (datetime.datetime(2020, 4, 18, 0, 0), 730317), (datetime.datetime(2020, 4, 19, 0, 0), 756375),
        (datetime.datetime(2020, 4, 20, 0, 0), 783716), (datetime.datetime(2020, 4, 21, 0, 0), 809213),
        (datetime.datetime(2020, 4, 22, 0, 0), 837414), (datetime.datetime(2020, 4, 23, 0, 0), 871617),
        (datetime.datetime(2020, 4, 24, 0, 0), 907908), (datetime.datetime(2020, 4, 25, 0, 0), 940829),
        (datetime.datetime(2020, 4, 26, 0, 0), 968517), (datetime.datetime(2020, 4, 27, 0, 0), 990993),
        (datetime.datetime(2020, 4, 28, 0, 0), 1015518), (datetime.datetime(2020, 4, 29, 0, 0), 1042926),
        (datetime.datetime(2020, 4, 30, 0, 0), 1072667)]


def calendar_custom_cell():
    calendar = Calendar(init_opts=opts.InitOpts(theme='light',
                                                width='1000px',
                                                height='600px'))
    calendar.add('Positive Cases',
                 yaxis_data=data,
                 label_opts=opts.LabelOpts(is_show=True),
                 calendar_opts=opts.CalendarOpts(
                     # Calendar position
                     pos_top='10%',
                     pos_left='10%',
                     # Calendar range
                     range_=['2020-01-21', '2020-04-30'],
                     # Calendar cell size
                     cell_size=50,
                     # Year Month Day Format
                     yearlabel_opts=opts.CalendarYearLabelOpts(margin=40,
                                                               label_font_size=20,
                                                               label_color='rgba(130,134,112,0.8)'),
                     daylabel_opts=opts.CalendarDayLabelOpts(label_color='#778633', label_font_weight='bold'),
                     monthlabel_opts=opts.CalendarMonthLabelOpts(label_color='#778633', label_font_weight='bold')
                 )
                 )
    # Configure visual options
    calendar.set_global_opts(visualmap_opts=opts.VisualMapOpts(
        max_=1000000,
        is_piecewise=True,
        pieces=[{"min": 1000000},
                {"min": 10000, "max": 1000000},
                {"min": 100, "max": 10000},
                {"max": 100}],
        range_color=["#CCD3D9", "#E6B6C2", "#D4587A", "#DC364C"]
    ))
    return calendar


chart = calendar_custom_cell()
chart.render_notebook()
Out[14]:

6.2 日历图显示中文标签¶

In [15]:
from pyecharts.charts import *
from pyecharts import options as opts
import datetime

# Covid data in US
data = [(datetime.datetime(2020, 1, 21, 0, 0), 1), (datetime.datetime(2020, 1, 22, 0, 0), 1),
        (datetime.datetime(2020, 1, 23, 0, 0), 1), (datetime.datetime(2020, 1, 24, 0, 0), 2),
        (datetime.datetime(2020, 1, 25, 0, 0), 2), (datetime.datetime(2020, 1, 26, 0, 0), 5),
        (datetime.datetime(2020, 1, 27, 0, 0), 5), (datetime.datetime(2020, 1, 28, 0, 0), 5),
        (datetime.datetime(2020, 1, 29, 0, 0), 5), (datetime.datetime(2020, 1, 30, 0, 0), 5),
        (datetime.datetime(2020, 1, 31, 0, 0), 7), (datetime.datetime(2020, 2, 1, 0, 0), 8),
        (datetime.datetime(2020, 2, 2, 0, 0), 8), (datetime.datetime(2020, 2, 3, 0, 0), 11),
        (datetime.datetime(2020, 2, 4, 0, 0), 11), (datetime.datetime(2020, 2, 5, 0, 0), 11),
        (datetime.datetime(2020, 2, 6, 0, 0), 11), (datetime.datetime(2020, 2, 7, 0, 0), 11),
        (datetime.datetime(2020, 2, 8, 0, 0), 11), (datetime.datetime(2020, 2, 9, 0, 0), 11),
        (datetime.datetime(2020, 2, 10, 0, 0), 11), (datetime.datetime(2020, 2, 11, 0, 0), 12),
        (datetime.datetime(2020, 2, 12, 0, 0), 12), (datetime.datetime(2020, 2, 13, 0, 0), 13),
        (datetime.datetime(2020, 2, 14, 0, 0), 13), (datetime.datetime(2020, 2, 15, 0, 0), 13),
        (datetime.datetime(2020, 2, 16, 0, 0), 13), (datetime.datetime(2020, 2, 17, 0, 0), 13),
        (datetime.datetime(2020, 2, 18, 0, 0), 13), (datetime.datetime(2020, 2, 19, 0, 0), 13),
        (datetime.datetime(2020, 2, 20, 0, 0), 13), (datetime.datetime(2020, 2, 21, 0, 0), 15),
        (datetime.datetime(2020, 2, 22, 0, 0), 15), (datetime.datetime(2020, 2, 23, 0, 0), 15),
        (datetime.datetime(2020, 2, 24, 0, 0), 15), (datetime.datetime(2020, 2, 25, 0, 0), 15),
        (datetime.datetime(2020, 2, 26, 0, 0), 15), (datetime.datetime(2020, 2, 27, 0, 0), 16),
        (datetime.datetime(2020, 2, 28, 0, 0), 16), (datetime.datetime(2020, 2, 29, 0, 0), 24),
        (datetime.datetime(2020, 3, 1, 0, 0), 30), (datetime.datetime(2020, 3, 2, 0, 0), 53),
        (datetime.datetime(2020, 3, 3, 0, 0), 73), (datetime.datetime(2020, 3, 4, 0, 0), 104),
        (datetime.datetime(2020, 3, 5, 0, 0), 174), (datetime.datetime(2020, 3, 6, 0, 0), 222),
        (datetime.datetime(2020, 3, 7, 0, 0), 337), (datetime.datetime(2020, 3, 8, 0, 0), 451),
        (datetime.datetime(2020, 3, 9, 0, 0), 519), (datetime.datetime(2020, 3, 10, 0, 0), 711),
        (datetime.datetime(2020, 3, 11, 0, 0), 1109), (datetime.datetime(2020, 3, 12, 0, 0), 1561),
        (datetime.datetime(2020, 3, 13, 0, 0), 2157), (datetime.datetime(2020, 3, 14, 0, 0), 2870),
        (datetime.datetime(2020, 3, 15, 0, 0), 2968), (datetime.datetime(2020, 3, 16, 0, 0), 4360),
        (datetime.datetime(2020, 3, 17, 0, 0), 6141), (datetime.datetime(2020, 3, 18, 0, 0), 8914),
        (datetime.datetime(2020, 3, 19, 0, 0), 14153), (datetime.datetime(2020, 3, 20, 0, 0), 19479),
        (datetime.datetime(2020, 3, 21, 0, 0), 25818), (datetime.datetime(2020, 3, 22, 0, 0), 33756),
        (datetime.datetime(2020, 3, 23, 0, 0), 43845), (datetime.datetime(2020, 3, 24, 0, 0), 54108),
        (datetime.datetime(2020, 3, 25, 0, 0), 66044), (datetime.datetime(2020, 3, 26, 0, 0), 84080),
        (datetime.datetime(2020, 3, 27, 0, 0), 102254), (datetime.datetime(2020, 3, 28, 0, 0), 122054),
        (datetime.datetime(2020, 3, 29, 0, 0), 141194), (datetime.datetime(2020, 3, 30, 0, 0), 162690),
        (datetime.datetime(2020, 3, 31, 0, 0), 188701), (datetime.datetime(2020, 4, 1, 0, 0), 214194),
        (datetime.datetime(2020, 4, 2, 0, 0), 244593), (datetime.datetime(2020, 4, 3, 0, 0), 276535),
        (datetime.datetime(2020, 4, 4, 0, 0), 309699), (datetime.datetime(2020, 4, 5, 0, 0), 337573),
        (datetime.datetime(2020, 4, 6, 0, 0), 367210), (datetime.datetime(2020, 4, 7, 0, 0), 397992),
        (datetime.datetime(2020, 4, 8, 0, 0), 429686), (datetime.datetime(2020, 4, 9, 0, 0), 464442),
        (datetime.datetime(2020, 4, 10, 0, 0), 497943), (datetime.datetime(2020, 4, 11, 0, 0), 527958),
        (datetime.datetime(2020, 4, 12, 0, 0), 556517), (datetime.datetime(2020, 4, 13, 0, 0), 581810),
        (datetime.datetime(2020, 4, 14, 0, 0), 608845), (datetime.datetime(2020, 4, 15, 0, 0), 637974),
        (datetime.datetime(2020, 4, 16, 0, 0), 669272), (datetime.datetime(2020, 4, 17, 0, 0), 701996),
        (datetime.datetime(2020, 4, 18, 0, 0), 730317), (datetime.datetime(2020, 4, 19, 0, 0), 756375),
        (datetime.datetime(2020, 4, 20, 0, 0), 783716), (datetime.datetime(2020, 4, 21, 0, 0), 809213),
        (datetime.datetime(2020, 4, 22, 0, 0), 837414), (datetime.datetime(2020, 4, 23, 0, 0), 871617),
        (datetime.datetime(2020, 4, 24, 0, 0), 907908), (datetime.datetime(2020, 4, 25, 0, 0), 940829),
        (datetime.datetime(2020, 4, 26, 0, 0), 968517), (datetime.datetime(2020, 4, 27, 0, 0), 990993),
        (datetime.datetime(2020, 4, 28, 0, 0), 1015518), (datetime.datetime(2020, 4, 29, 0, 0), 1042926),
        (datetime.datetime(2020, 4, 30, 0, 0), 1072667)]


def calendar_in_Chinese():
    calendar = Calendar(init_opts=opts.InitOpts(theme='light',
                                                width='1000px',
                                                height='600px'))
    calendar.add('确诊人数',
                 yaxis_data=data,
                 label_opts=opts.LabelOpts(is_show=True),
                 calendar_opts=opts.CalendarOpts(
                     # Calendar range
                     range_=['2020-01-21', '2020-04-30'],
                     # Calendar cell size
                     cell_size=50,
                     # Year Month Day Format
                     # Display Chinese for day and month label, default is English.
                     yearlabel_opts=opts.CalendarYearLabelOpts(margin=50),
                     daylabel_opts=opts.CalendarDayLabelOpts(name_map='cn'),
                     monthlabel_opts=opts.CalendarMonthLabelOpts(name_map='cn')
                 )
                 )
    # Configure visual map
    calendar.set_global_opts(visualmap_opts=opts.VisualMapOpts(
        max_=1000000,
        is_piecewise=True,
        pieces=[{"min": 1000000},
                {"min": 10000, "max": 1000000},
                {"min": 100, "max": 10000},
                {"max": 100}],
        range_color=["#CCD3D9", "#E6B6C2", "#D4587A", "#DC364C"]
    ))
    return calendar


chart = calendar_in_Chinese()
chart.render_notebook()
Out[15]:

7 Geo Charts¶

More details to follow: https://pyecharts.org/#/zh-cn/geography_charts

7.1 GEO添加自定义坐标¶

In [16]:
from pyecharts.charts import *
from pyecharts import options as opts

def geo_add_custom_coordinate():
    geo = Geo(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    # Add coordinates
    geo.add_coordinate('x', 116.397428, 39.90923)
    geo.add_coordinate('y', 112.398615, 29.91659)

    geo.add_schema(maptype="china")
    geo.add("", [('x', 1), ('y', 2)])

    return geo


chart = geo_add_custom_coordinate()
chart.render_notebook()
Out[16]:

7.2 GEO使用国外地图¶

In [17]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.datasets import register_url
import ssl


def geo_foreign_country():
    # Add other countries' map
    # noinspection PyBroadException
    try:
        register_url("https://echarts-maps.github.io/echarts-countries-js/")
    except Exception:
        ssl._create_default_https_context = ssl._create_unverified_context
        register_url("https://echarts-maps.github.io/echarts-countries-js/")

    geo = Geo(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))

    geo.add_schema(maptype="美国")

    return geo


chart = geo_foreign_country()
chart.render_notebook()
Out[17]:

7.3 GEO 带涟漪效果散点图¶

In [18]:
from pyecharts.charts import *
from pyecharts import options as opts


def geo_effect_scatter():
    geo = Geo(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))

    geo.add_schema(maptype="china")

    geo.add("",
            [("广州", 150), ("北京", 70), ("长沙", 64), ("上海", 74),  ("厦门", 63)],
            # Effect Scatter type
            type_='effectScatter')

    return geo


chart = geo_effect_scatter()
chart.render_notebook()
Out[18]:

7.4 GEO热力图¶

In [19]:
from pyecharts.charts import *
from pyecharts import options as opts


def geo_heatmap():
    geo = Geo(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))

    geo.add_schema(maptype="china")

    geo.add("",
            [("广州", 150), ("北京", 70), ("深圳", 64), ("上海", 74),  ("杭州", 63)],
            type_='heatmap')
    # Heatmap needs visualmap_opts
    geo.set_global_opts(visualmap_opts=opts.VisualMapOpts())
    return geo


chart = geo_heatmap()
chart.render_notebook()
Out[19]:

7.5 GEO 线图¶

In [21]:
from pyecharts.charts import *
from pyecharts import options as opts


def geo_lines():
    geo = Geo(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))

    geo.add_schema(maptype="china")

    geo.add("Departure Guangzhou",
            # format(from, to)
            [("广州", "上海"), ("广州", "北京"), ("广州", "西宁"), ("广州", "重庆")],
            type_='lines')
    geo.add("Departure Chengdu",
            # format(from, to)
            [("成都", '长沙'), ("成都", "武汉"), ("成都", "长春"), ("成都", "南京")],
            type_='lines')

    return geo


chart = geo_lines()
chart.render_notebook()
Out[21]:

8 Pie Charts¶

More details to follow:

8.1 饼图自定义图形半径范围¶

In [22]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def pie_custom_radius():
    pie = Pie(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    pie.add("",
            [list(z) for z in zip(Faker.choose(), Faker.values())],
            # Define radius,0%-100%
            radius=["40%", "75%"])

    return pie


chart = pie_custom_radius()
chart.render_notebook()
Out[22]:

8.2 饼图自定义数据标签¶

In [23]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker

def pie_with_custom_label():
    pie = Pie(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='600px'))
    pie.add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
    pie.set_series_opts(
        # Define data label
        label_opts=opts.LabelOpts(position='top',
                                  color='red',
                                  font_family='Arial',
                                  font_size=12,
                                  font_style='italic',
                                  interval=1,
                                  formatter='{b}:{d}%'
                                  )
    )

    return pie


chart = pie_with_custom_label()
chart.render_notebook()
Out[23]:

8.3 多饼图¶

In [24]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def pie_multiple():
    pie = Pie(init_opts=opts.InitOpts(theme='light',
                                      width='1000px',
                                      height='800px'))
    pie.add("",
            [list(z) for z in zip(Faker.choose(), Faker.values())],
            radius=["20%", "50%"],
            center=["25%", "50%"])
    # Add another Pie chart
    pie.add("",
            [list(z) for z in zip(Faker.choose(), Faker.values())],
            radius=["20%", "50%"],
            center=["75%", "50%"])

    return pie


chart = pie_multiple()
chart.render_notebook()
Out[24]:

9 Word Cloud Charts¶

More details to follow: https://pyecharts.org/#/zh-cn/basic_charts?id=wordcloud%ef%bc%9a%e8%af%8d%e4%ba%91%e5%9b%be

9.1 词云图自定义字体范围¶

In [29]:
from pyecharts.charts import *
from pyecharts import options as opts
import random

text = """
I used to rule the world
Seas would rise when I gave the word
Now in the morning I sleep alone
Sweep the streets I used to own
I used to roll the dice
Feel the fear in my enemy eyes
Listen as the crowd would sing
Now the old king is dead
Long live the king
One minute I held the key
Next the walls were closed on me
And I discovered that my castles stand
Upon pillars of salt and pillars of sand
I hear Jerusalem bells are ringing
Roman Cavalry choirs are singing
Be my mirror my sword and shield
My missionaries in a foreign field
For some reason I can't explain
Once you go there was never
Never an honest word
That was when I ruled the world
"""

word_list = set(text.split(' '))

words = [(word, random.randint(1, 100)) for word in word_list]


def wordcloud_custom_word_size():
    wordcloud = WordCloud(init_opts=opts.InitOpts(theme='light',
                                                  width='1000px',
                                                  height='600px'))
    wordcloud.add('',
                  words,
                  # Define word size range
                  word_size_range=[10, 50])

    return wordcloud


chart = wordcloud_custom_word_size()
chart.render_notebook()
Out[29]:

10 Map Charts¶

More details to follow: https://pyecharts.org/#/zh-cn/geography_charts?id=map%ef%bc%9a%e5%9c%b0%e5%9b%be

10.1 Map带视觉组件¶

In [35]:
from pyecharts.charts import *
from pyecharts import options as opts

data = [('广东省', 10430),
        ('山东省', 9579),
        ('河南省', 9402),
        ('四川省', 8041),
        ('江苏省', 7866),
        ('河北省', 7185),
        ('湖南省', 6568),
        ('安徽省', 5950),
        ('湖北省', 5724),
        ('浙江省', 5442),
        ('广西壮族自治区', 4603),
        ('云南省', 4597),
        ('江西省', 4457),
        ('辽宁省', 4375),
        ('黑龙江省', 3831),
        ('陕西省', 3733),
        ('山西省', 3571),
        ('福建省', 3552),
        ('贵州省', 3477),
        ('重庆市', 2884),
        ('吉林省', 2746),
        ('甘肃省', 2557),
        ('内蒙古自治区', 2471),
        ('台湾省', 2316),
        ('上海市', 2301),
        ('新疆维吾尔自治区', 2181),
        ('北京市', 1961),
        ('天津市', 1294),
        ('海南省', 867),
        ('香港特别行政区', 710),
        ('宁夏回族自治区', 630),
        ('青海省', 562),
        ('西藏自治区', 300),
        ('澳门特别行政区', 55)]


def map_with_viusalmap():
    map_chart = Map(init_opts=opts.InitOpts(theme='light',
                                            width='1000px',
                                            height='600px'))
    map_chart.add('Population(100K)',
                  data_pair=data,
                  maptype='china',
                  is_map_symbol_show=False)

    map_chart.set_global_opts(visualmap_opts=opts.VisualMapOpts(
        max_=10430,  
        is_piecewise=True,
        range_color=["#CCD3D9", "#E6B6C2", "#D4587A", "#DC364C"]
    ))
    return map_chart


chart = map_with_viusalmap()
chart.render_notebook()
Out[35]:

11 综合¶

11.1 Page更改布局¶

In [31]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def page_simple_layout():
    # Chart 1
    bar_1 = Bar(init_opts=opts.InitOpts(theme='light',
                                        width='500px',
                                        height='300px'))
    bar_1.add_xaxis(Faker.choose())
    bar_1.add_yaxis('A', Faker.values())
    bar_1.add_yaxis('B', Faker.values())

    # Chart 2
    bar_2 = Bar(init_opts=opts.InitOpts(theme='light',
                                        width='500px',
                                        height='300px'))
    bar_2.add_xaxis(Faker.choose())
    bar_2.add_yaxis('A', Faker.values())
    # X, Y Axis flirt
    bar_2.reversal_axis()

    # Chart 3
    line = Line(init_opts=opts.InitOpts(theme='light',
                                        width='500px',
                                        height='300px'))
    line.add_xaxis(Faker.choose())
    line.add_yaxis('A', Faker.values())
    line.add_yaxis('B', Faker.values())

    # Chart 4
    pie = Pie(init_opts=opts.InitOpts(theme='light',
                                      width='500px',
                                      height='300px'))
    pie.add("",
            [list(z) for z in zip(Faker.choose(), Faker.values())],
            radius=["40%", "75%"])

    page = Page(layout=Page.SimplePageLayout)
    # The position can be adjusted
    page.add(bar_1, bar_2, line, pie)

    return page


chart = page_simple_layout()
chart.render_notebook()
Out[31]:

11.2 一个Tab下带多个图表¶

  • 通过Grid将多个图表组合,然后再将Grid添加到Tab中;
In [32]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def tab_with_multiple_chart():
    # Chart 1
    bar = Bar()
    bar.add_xaxis(Faker.choose())
    bar.add_yaxis("商家A", Faker.values())
    bar.add_yaxis("商家B", Faker.values())

    # Chart 2
    line = Line()
    line.add_xaxis(Faker.choose())
    line.add_yaxis("商家A", Faker.values())
    line.add_yaxis("商家B", Faker.values())

    # Combine Chart 1 and Chart 2 using Grid
    grid = Grid()
    grid.add(bar, grid_opts=opts.GridOpts(pos_bottom="60%"))
    grid.add(line, grid_opts=opts.GridOpts(pos_top="60%"))

    # add Grid to Tab
    tab = Tab()
    tab.add(grid, 'Example')

    return tab


chart = tab_with_multiple_chart()
chart.render_notebook()
Out[32]:

11.3 Timeline 自动播放¶

In [33]:
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.faker import Faker


def timeline_auto_play():
    timeline = Timeline(init_opts=opts.InitOpts(theme='light',
                                                width='1000px',
                                                height='600px'))
    timeline.add_schema(is_auto_play=True,  # Auto Play
                        is_loop_play=True  # Loop Play
                        )
    for year in range(2000, 2020):
        bar = Bar()
        bar.add_xaxis(['香蕉', '梨子', '水蜜桃', '核桃', '西瓜', '苹果', '菠萝'])
        bar.add_yaxis('A', Faker.values())
        bar.add_yaxis('B', Faker.values())
        timeline.add(bar, '{}年'.format(year))
    return timeline


chart = timeline_auto_play()
chart.render_notebook()
Out[33]: