Customization of chart auxiliary elements

1. The auxiliary elements commonly used in charts

Common auxiliary elements include following:

  • Coordinate axis: The single coordinate axis can be divided into horizontal coordinate axis (X axis) and vertical coordinate axis (Y axis);
  • Title: descriptive text representing the chart.
  • Legend: used to indicate the identification method of each group of graphs in the chart.
  • Grid: several lines running through the drawing area starting from the scale of the coordinate axis, which are used as the standard for estimating the values shown in the drawing.
  • Reference line: a line marking a special value on the coordinate axis.
  • Reference area: an area marking a special range on the coordinate axis.
  • Annotation text and table: indicates some notes and descriptions of the drawing.
  • Table: a table used to emphasize data that is difficult to understand.

The auxiliary elements commonly

2. Sets the label, scale range, and scale label for the axis

2.1 Setting labels for coordinate axes

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import matplotlib.pyplot as plt

# function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)

# label
plt.xlabel("x-axis")
plt.ylabel("y-axis")

plt.show()

Set labels for X axis and Y axis

2.2 Set scale range and scale label

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np
import matplotlib.pyplot as plt

# Image function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)

# Set x-axis scale range and scale label
# This x.min() is negative, x.max() is positive number
# so .....
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

plt.show()

Set scale range and scale label

2.3 Demo 1: 2019 Chinese film box office ranking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import matplotlib.pyplot as plt

# Chinese support
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

labels = ["哪吒之魔童降世", "流浪地球", "复仇者联盟4:终局之战",
"疯狂的外星人", "飞驰人生", "烈火英雄", "蜘蛛侠:英雄远征",
"速度与激情:特别行动", "扫毒2:天地对决", "大黄蜂", "惊奇队长",
"比悲伤更悲伤的故事", "哥斯拉2: 怪兽之王", "阿丽塔:战斗天使",
"银河补习班"]
bar_width = [48.57, 46.18, 42.05, 21.83, 17.03, 16.70, 14.01, 13.84,
12.85, 11.38, 10.25, 9.46, 9.27, 8.88, 8.64]

y_data = range(len(labels))
fig = plt.figure(figsize=(16, 9))
# It's means 1x1 1 posit, in order to
ax = fig.add_subplot(111)
ax.barh(y_data, bar_width, height=0.2, color="orange")
# set x-axes and y-axes label
ax.set_xlabel("总票房(亿元)")
ax.set_ylabel("电影名称")
# set y-axes scale range and scale label
ax.set_yticks(y_data)
ax.set_yticklabels(labels)

plt.show()

2019 Chinese film box office ranking

3. Add title and legend

3.1 Add title

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import numpy as np
import matplotlib.pyplot as plt

# function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)

# Set x-axis scale range and scale label
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

# Add title
plt.title("Sine and Cosine Curves")

plt.show()

Add title

3.2 Add legend

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import matplotlib.pyplot as plt
import numpy as np

# function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
lines = plt.plot(x, y1, x, y2)

# Set x-axis scale range and scale label
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

# Add title
plt.title("Sine and Cosine Curves")

# Add legend
# The shadow mean below the legend, fancybox means Fillet edge
plt.legend(lines, ['sin(x)', 'cos(x)'], shadow=True, fancybox=True)

plt.show()

Add legend

3.3 Demo 2: Alipay monthly bill Report

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import matplotlib.pyplot as plt

# support Chinese
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

kinds = ['购物', '人情来往', '餐饮美食', '通信物流', '生活日用', '交通出行', '休闲娱乐', '其他']

money_scale = [800 / 3000, 100 / 3000, 1000 / 3000, 200 / 3000, 300 / 3000,
200 / 3000, 200 / 3000, 200 / 3000]
dev_position = [.1, .1, .1, .1, .1, .1, .1, .1]

plt.pie(money_scale, labels=kinds, autopct='%3.1f%%', shadow=True, explode=dev_position, startangle=90)

# Add title
plt.title('支付宝月账单报告')
# Add legend
plt.legend(kinds, loc='best', bbox_to_anchor=[1.3, 1.1])

plt.tight_layout()
plt.show()

Alipay monthly bill Report

4. Display grid

4.1 Displays the grid of the specified style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import matplotlib.pyplot as plt
import numpy as np

# Function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
lines = plt.plot(x, y1, x, y2)

# Set x-axis scale range and scale label
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

# Add title
plt.title("Sine and Cosine Curves")

# Add legend
# The shadow mean below the legend, fancybox means Fillet edge
plt.legend(lines, ['sin(x)', 'cos(x)'], shadow=True, fancybox=True)

# Show grid
# Visible means Whether to show the grid lines.
plt.grid(visible=True, axis='y', linewidth=0.3, color='black')

plt.show()

Display horizontal grid

4.2 Demo 3: relationship between vehicle speed and braking distance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import matplotlib.pyplot as plt
import numpy as np

# Chinese support
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

x_speed = np.arange(10, 210, 10)
y_distance = np.array([0.5, 2.0, 4.4, 7.9, 12.3,
17.7, 24.1, 31.5, 39.9, 49.2,
59.5, 70.8, 83.1, 96.4, 110.7,
126.0, 142.2, 159.4, 177.6, 196.8])
plt.scatter(x_speed, y_distance, s=50, alpha=0.9, linewidths=0.3)
# set x-axis label and scale label
plt.xlabel('Speed(km/h)')
plt.ylabel('Braking distance(m)')
plt.xticks(x_speed)

# show grid
plt.grid(visible=True, linewidth=0.3, color="black")
plt.show()

Relationship between vehicle speed and braking distance

5. Add reference lines and areas

5.1 Add reference line

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import matplotlib.pyplot as plt
import numpy as np

# function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
lines = plt.plot(x, y1, x, y2)

# Set x-axis scale range and scale label
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

# Add title
plt.title("Sine and Cosine Curves")

# Add legend
# The shadow mean below the legend, fancybox means Fillet edge
plt.legend(lines, ['sin(x)', 'cos(x)'], shadow=True, fancybox=True)

# Show grid
# visible means Whether to show the grid lines.
plt.grid(visible=True, axis='y', linewidth=0.3, color='black')

# Add reference line
plt.axvline(x=0, linestyle='--')
plt.axhline(y=0, linestyle='--')

plt.show()

Draw horizontal/vertical reference line

5.2 Add reference area

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import matplotlib.pyplot as plt
import numpy as np

# function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
lines = plt.plot(x, y1, x, y2)

# Set x-axis scale range and scale label
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

# Add title
plt.title("Sine and Cosine Curves")

# Add legend
# The shadow mean below the legend, fancybox means Fillet edge
plt.legend(lines, ['sin(x)', 'cos(x)'], shadow=True, fancybox=True)

# Show grid
# visible means Whether to show the grid lines.
plt.grid(visible=True, axis='y', linewidth=0.3, color='black')

# Add reference line
plt.axvline(x=0, linestyle='--')
plt.axhline(y=0, linestyle='--')

# Add reference area
plt.axvspan(xmin=0.5, xmax=2.0, alpha=0.3)
plt.axhspan(ymin=0.5, ymax=1.0, alpha=0.3)

plt.show()

Add reference area

5.3 Demo 4: Evaluation of English performance of boys and girls in all classes of grade two in High School

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import matplotlib.pyplot as plt
import numpy as np

# Chinese support
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

men_means = (90.5, 89.5, 88.7, 88.5, 85.2, 86.6)
women_means = (92.7, 87.0, 90.5, 85.0, 89.5, 89.8)

ind = np.arange(len(men_means))
width = 0.2

fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(ind - width / 2, men_means, width, label="男生平均成绩")
ax.bar(ind + 0.2, women_means, width, label="女生平均成绩")
ax.set_title('高二各班男生/女生英语平均成绩')
ax.set_ylabel('分数')
ax.set_xticks(ind)
ax.set_xticklabels(['高二1班', '高二2班', '高二3班', '高二4班', '高二5班', '高二6班', ])

# add line
ax.axhline(88.5, ls='--', linewidth=1.0, label="全体平均成绩")
ax.legend(loc="lower right")

plt.show()

Evaluation of English performance of boys and girls in all classes of grade two in High School

6. Add annotation text

6.1 Add directional annotation text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
import matplotlib.pyplot as plt

# function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
lines = plt.plot(x, y1, x, y2)

# Set x-axis scale range and scale label
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

# Add title
plt.title("Sine and Cosine Curves")

# Add legend
# The shadow mean below the legend, fancybox means Fillet edge
plt.legend(lines, ['sin(x)', 'cos(x)'], shadow=True, fancybox=True)

# Show grid
# visible means Whether to show the grid lines.
plt.grid(visible=True, axis='y', linewidth=0.3, color='black')

# Add reference line
plt.axvline(x=0, linestyle='--')
plt.axhline(y=0, linestyle='--')

# Add reference area
plt.axvspan(xmin=0.5, xmax=2.0, alpha=0.3)
plt.axhspan(ymin=0.5, ymax=1.0, alpha=0.3)

plt.annotate("Minimum",
xy=(-np.pi / 2, -1.0),
xytext=(-np.pi / 2, -0.5),
arrowprops=dict(arrowstyle="->"))

plt.show()

Add directional annotation text

6.2 Add non-directional annotation text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import matplotlib.pyplot as plt
import numpy as np

# Chinese support
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
lines = plt.plot(x, y1, x, y2)

# Set x-axis scale range and scale label
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

# Add title
plt.title("Sine and Cosine Curves")

# Add legend
# The shadow mean below the legend, fancybox means Fillet edge
plt.legend(lines, ['sin(x)', 'cos(x)'], shadow=True, fancybox=True)

# Show grid
# visible means Whether to show the grid lines.
plt.grid(visible=True, axis='y', linewidth=0.3, color='black')

# Add reference line
plt.axvline(x=0, linestyle='--')
plt.axhline(y=0, linestyle='--')

# Add reference area
plt.axvspan(xmin=0.5, xmax=2.0, alpha=0.3)
plt.axhspan(ymin=0.5, ymax=1.0, alpha=0.3)

# Add directional annotation text
plt.annotate("Minimum",
xy=(-np.pi / 2, -1.0),
xytext=(-np.pi / 2, -0.5),
arrowprops=dict(arrowstyle="->"))

# Add non-directional annotation text
plt.text(3.10, 0.10, "y=sin(x)", bbox=dict(alpha=0.2))

plt.show()

Add non directional annotation text

6.3 Demo 5: GMV of Alibaba‘s Taobao and Tmall platforms from 2013 to 2019

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import matplotlib.pyplot as plt
import numpy as np

# Chinese support
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

x = np.arange(1, 8)
y = np.array([10770, 16780, 24440, 30920, 37670, 48200, 57270])
bar_rects = plt.bar(x, y, tick_label=["FY2013", "FY2014", "FY2015",
"FY2016", "FY2017", "FY2018", "FY2019"], width=0.5)


# Add no directional annotation text
def autolabel(rects):
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2, height + 300,
s='{}'.format(height),
ha='center', va='bottom')


autolabel(bar_rects)
plt.ylabel('GMV(Billion Yuan)')
plt.show()

GMV of Alibaba‘s Taobao and Tmall platforms from 2013 to 2019

7. Add table

7.1 Add custom style tables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import matplotlib.pyplot as plt
import numpy as np

# function
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
lines = plt.plot(x, y1, x, y2)

# Set x-axis scale range and scale label
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

# Add title
plt.title("Sine and Cosine Curves")

# Add legend
# The shadow mean below the legend, fancybox means Fillet edge
plt.legend(lines, ['sin(x)', 'cos(x)'], shadow=True, fancybox=True)

# Show grid
# visible means Whether to show the grid lines.
plt.grid(visible=True, axis='y', linewidth=0.3, color='black')

# Add reference line
plt.axvline(x=0, linestyle='--')
plt.axhline(y=0, linestyle='--')

# Add reference area
plt.axvspan(xmin=0.5, xmax=2.0, alpha=0.3)
plt.axhspan(ymin=0.5, ymax=1.0, alpha=0.3)

# Add directional annotation text
plt.annotate("Minimum",
xy=(-np.pi / 2, -1.0),
xytext=(-np.pi / 2, -0.5),
arrowprops=dict(arrowstyle="->"))

# Add non-directional annotation text
plt.text(3.10, 0.10, "y=sin(x)", bbox=dict(alpha=0.2))

# Add table
plt.table(cellText=[[6, 6, 6], [8, 8, 8]], colWidths=[0.1] * 3, rowLabels=['row 1', 'row 2'],
colLabels=['column 1', 'column 2', 'column 3'], loc='lower right')

plt.show()

Add custom style tables

7.2 Demo 6: Proportion of jam bread ingredients

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import matplotlib.pyplot as plt

# Chinese support
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

kinds = ['面粉', '全麦粉', '酵母', '苹果酱', '鸡蛋', '黄油', '盐', '白糖']
weight = [250, 150, 4, 250, 50, 30, 4, 20]
total_weight = 0
for i in weight:
total_weight += i
batching_scale = [i / total_weight for i in weight]
plt.pie(batching_scale, autopct="%3.1f%%")
plt.legend(kinds, loc='upper right', bbox_to_anchor=[1.1, 1.1])

# Add table
plt.table(cellText=[weight],
cellLoc='center',
rowLabels=['重量'],
colLabels=kinds,
loc='lower center')
plt.show()

Proportion of jam bread ingredients


Customization of chart auxiliary elements
https://www.hardyhu.cn/2022/03/14/Customization-of-chart-auxiliary-elements/
Author
John Doe
Posted on
March 14, 2022
Licensed under