Popular all over the Internet! You can use Pyecharts to create “migration charts” and “carousel charts”

1. Review of pyecharts knowledge points

1) Knowledge review

We have already described how to use pyecharts to draw graphics, which involves the following four steps. Today we will follow the following steps to draw the migration chart and carousel chart.

  • ① Select the chart type;

  • ② Declare the graphics class and add data;

  • ③ Set global configuration items and series configuration items;

  • ④ Display and save charts;

2) Migration chart

Migration diagram is similar to the one below, using a curve to show the trajectory from one place to another.

Picture

Technical exchange

Technology must be shared and communicated, and it is not recommended to work behind closed doors. One person can go very fast, and a group of people can go further.

This article is summarized and shared by friends in the fan group. If you also want to learn, communicate, and obtain information, you can join the communication group to obtain it. The group has more than 2,000 members. The best way to make notes when adding is: source + direction of interest, which is easy to find. Like-minded friends.

Method ①, add WeChat ID: dkl88194, note: from CSDN + add group
Method ②, WeChat search public account: Python learning and data mining, background reply: join the group

3) Carousel image

Carousel image is the scrolling large screen effect on Taobao’s home page. On the same page, you can place multiple graphics and set a certain rotation time. It can scroll and play for you like a cartoon.

Picture

2. Drawing of migration map

1) Parameter explanation
① Geo() class

The Geo() class is a class used to draw regional maps. Since it is a class, it has its own initialization parameters and its own custom method, the general form of this class is like this.

class Geo(
    #Initialize configuration items
    init_opts: opts.InitOpts = opts.InitOpts()

    # Whether to ignore non-existent coordinates, the default value is False, that is, not ignored
    is_ignore_nonexistent_coord: bool = False
)
# This class has the following two methods.
# The main function of the add_schema() method: used to specify which place to draw the map;
def add_schema();
# The main function of the add() method: used to add data to graphics;
def add();

② init_opts parameter

init_optsThis parameter is very commonly used and is used to specify the size of the graphics canvas. No matter you draw any graphics, you can call this parameter in the following way.

  • Parameter addition location: Add this parameter when initializing the class name;

  • Graphics class name (init_opts=opts.InitOpts(width=,height=));

  • width: Specify the width of the canvas, the default is 900px;

  • height: Specify the height of the canvas, the default is 500px;

③ label_opts parameter

label_opts This parameter is used to set whether the graphic label is displayed. When there are too many labels in the graphic, the words may overlap. At this time, we can set the label not to display to solve this problem. .

  • Parameter addition location: Add this parameter when adding data using the add() method;

  • .add(…,…,label_opts=opts.LabelOpts(is_show=False));

  • is_show: The default is True to display labels, False to not display labels;

④ Description of the add_schema() method

This method involves two important parameters when mapping migration. One is maptype, and the other is itemstyle_opts. We will describe them separately below.

  • maptype: used to specify the map type, ‘china’ represents drawing a map of China, ‘Hubei’ represents drawing a map of Hubei, and ‘Guangdong’ represents drawing a map of Guangdong;

  • itemstyle_opts=opts.ItemStyleOpts(color=#323c48’, border_color=black’));

  • itemstyle_opts: Element style configuration item (official name). After hearing the name, I don’t know what it does. It is actually a parameter used to fill the map color.

  • color: used to specify the background fill color of the map;

  • border_color: used to specify the color of the border lines between regions;

⑤ effect_opts parameter

effect_opts is a ripple special effect configuration item. This parameter is used to display special effects. After setting this parameter, a certain point will spread out like ripples on water.

  • effect_opts=opts.EffectOpts(symbol=“arrow”, color=gold’, symbol_size=8));

  • symbol: Specify the shape of the point, here the arrow shape is specified;

  • color: Specify the color of the point, yellow is specified here;

  • symbol_size: Specify the size of the point;

⑥ set_global_opts() method

set_global_opts() is used to set global configuration items, often used to display title configuration items and visual mapping configuration items.

  • .set_global_opts(title_opts = opts.TitleOpts(title=”China Map”), visualmap_opts = opts.VisualMapOpts(min_=30, max_=110))

Picture

2) Drawing code
# 1. Import related libraries
from pyecharts.charts import Geo
import pyecharts.options as opts
# 2. Prepare data
city_num = [('Wuhan',105),('Chengdu',70),('Beijing',99),
            ('Xi'an',80),('Hangzhou',60),('Guiyang',34),
            ('Shanghai',65),('Shenzhen',54),('Urumqi',76),
            ('Harbin',47),('Lanzhou',56),('Xinyang',85)]
start_end = [('Wuhan','Chengdu'),('Wuhan','Beijing'),('Wuhan','Xi'an'),
             ('Wuhan','Hangzhou'),('Wuhan','Guiyang'),('Wuhan','Shanghai'),
             ('Wuhan','Shenzhen'),('Wuhan','Urumqi'),('Wuhan','Harbin'),
             ('Wuhan','Lanzhou'),('Wuhan','Xinyang')]

(
    # 3. Initialize the map class
    Geo(init_opts=opts.InitOpts(width="700px",height="300px",theme="blue"))
    .add_schema(maptype='china',
                itemstyle_opts=opts.ItemStyleOpts(color='#323c48', border_color='black'))
    # 4. Add data
    .add('', data_pair=city_num, color='white')
    .add('', data_pair=start_end, type_="lines",label_opts=opts.LabelOpts(is_show=False),
         effect_opts=opts.EffectOpts(symbol="arrow",
                                     color='gold',
                                     symbol_size=8))
    .set_global_opts(
        title_opts = opts.TitleOpts(title="China Map"),
        visualmap_opts = opts.VisualMapOpts(min_=30, max_=110))
).render_notebook() # 5. Graphic display

The result is as follows:

Picture

3. Drawing of carousel chart

1) Parameter explanation

Since other parameters have been explained in detail above, here we only describe the different parameters. There are actually quite a lot of parameters. We need to learn while using them and summarize them as we go, so that we can become more and more powerful.

① Timeline() class

The Timeline() class is used to draw carousels. Since it is also a class, it must have its own initialization properties and corresponding methods. The general form of the class is as follows.

class Timeline(
    # Initialize configuration items, refer to `global_options.InitOpts`
    init_opts: opts.InitOpts = opts.InitOpts()
)

# This class has the following two methods.
# The main function of the add_schema() method is to set the rotation time and whether to automatically rotate;
def add_schema();
# The main function of the add() method: used to add data to graphics;
def add();

② add() method
  • .add(chart=map1, time_point=“Hubei Province Epidemic Map”);

  • chart: Which chart do you want to add;

  • time_point: used to specify the starting point and key name of the carousel axis;

③ add_schema() method
  • .add_schema(is_auto_play=True, play_interval=3000);

  • is_auto_play: Whether to automatically rotate, the default is False, no automatic rotation;

  • play_interval: Carousel time, unit is millisecond, 3000 milliseconds means carousel once every 3 seconds;

2) Drawing code
from pyecharts.charts import Map, Timeline
from pyecharts import options

# 1. Accurate data
hubei_city = ["Wuhan City","Xiaogan City","Huanggang City","Jingzhou City","Ezhou City","Suizhou City",
        "Xiangyang City","Huangshi City","Yichang City","Jingmen City","Xianning City","Shiyan City",
        "Xiantao City","Tianmen City","Enshi Tujia and Miao Autonomous Prefecture","Qianjiang City","Shennongjia Forest District"]
hubei_data = [3214,628,722,287,224,304,321,202,269,217,206,177,97,82,103,27,7]

guangdong_city = ["Shenzhen City","Guangzhou City","Zhuhai City","Foshan City","Dongguan City","Zhongshan City",
        "Huizhou City","Shantou City","Zhanjiang City","Jiangmen City","Zhaoqing City","Yangjiang City",
        "Meizhou City","Maoming City","Qingyuan City","Jieyang City","Shaoguan City",
        "Chaozhou City","Shantou City","Heyuan City"]
guangdong_data = [375,317,86,70,62,58,53,25,21,20,15,13,13,11,10,8,7,5,5,3]

# 2. Draw Hubei epidemic map: Format 1
map1 = (
    Map(init_opts=opts.InitOpts(width="700px",height="300px",theme="blue"))
    .add('', [(i,j) for i,j in zip(hubei_city,hubei_data)], 'Hubei')
    .set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=4000))
)

# 3. Drawing an epidemic map in Guangdong: Format 2
map2 = (
    Map()
    .add('', [(i,j) for i,j in zip(guangdong_city,guangdong_data)], 'Guangdong')
    .set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=400,is_piecewise=True))
)

# 4. Create a composite class object
timeline = Timeline(init_opts=opts.InitOpts(width='720px', height='350px'))

# 5. Add the chart objects that need to be combined in the combination object

timeline.add(chart=map1, time_point="Hubei Province Epidemic Map")
timeline.add(chart=map2, time_point="Guangdong Province Epidemic Map")
timeline.add_schema(is_auto_play=True, play_interval=3000)

# 6. Render data
timeline.render_notebook()

The result is as follows:

Picture