Use Matplotlib to draw simple charts

1. Draw line chart

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

x1 = np.arange(1, 10)
y1 = x1
y2 = x1 ** 2

plt.plot(x1, y1)
plt.plot(x1, y2)
plt.show()

Draw line chart

1
2
3
4
5
6
7
import matplotlib.pyplot as plt
import numpy as np

arr = np.arange(1, 13).reshape((4, 3))

plt.plot(arr[0], arr[1:])
plt.show()

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

x = np.arange(4, 19)
y_max = np.array([32, 33, 34, 34, 33, 31, 30, 29, 30, 29, 26, 23, 21, 25, 31])
y_min = np.array([19, 19, 20, 22, 22, 21, 22, 16, 18, 18, 17, 14, 15, 16, 16])

# Draw line
plt.plot(x, y_max)
plt.plot(x, y_min)
plt.show()

Maximum temperature and minimum temperature in the next 15 days

2. Draw column chart

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

x = np.arange(5)
y1 = np.array([10, 8, 7, 11, 13])
# width of bar
bar_width = 0.3
# draw bar
plt.bar(x, y1, tick_label=['a', 'b', 'c', 'd', 'e'], width=bar_width)
plt.show()

Draw a column chart with one set of columns

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

x = np.arange(5)
y1 = np.array([10, 8, 7, 11, 13])
y2 = np.array([9, 6, 5, 10, 12])
# width of bar
bar_width = 0.3
# draw bar
plt.bar(x, y1, tick_label=['a', 'b', 'c', 'd', 'e'], width=bar_width)
plt.bar(x + bar_width, y2, width=bar_width)
plt.show()

Draw a column chart with two set of columns

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

x = np.arange(5)
y1 = np.array([10, 8, 7, 11, 13])
y2 = np.array([9, 6, 5, 10, 12])
# Draw stacked column chart
bar_width = 0.3
plt.bar(x, y1, tick_label=['a', 'b', 'c', 'd', 'e'], width=bar_width)
plt.bar(x, y2, bottom=y1, width=bar_width)

plt.show()

Draw stacked column chart

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

x = np.arange(5)
y1 = np.array([10, 8, 7, 11, 13])
y2 = np.array([9, 6, 5, 10, 12])

# Draw stacked column chart
# Draw a bar chart with error bars
error = [2, 1, 2.5, 2, 1.5]

bar_width = 0.3
plt.bar(x, y1, tick_label=['a', 'b', 'c', 'd', 'e'], width=bar_width)
plt.bar(x, y2, bottom=y1, width=bar_width, yerr=error)

plt.show()

Draw a bar chart with error bars

1
2
3
4
5
6
7
8
import matplotlib.pyplot as plt
import numpy as np

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

plt.show()

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

3. Draw bar chart

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

y = np.arange(5)
x1 = np.array([10, 8, 7, 11, 13])

bar_height = 0.3

plt.barh(y, x1, tick_label=['a', 'b', 'c', 'd', 'e'], height=bar_height)
plt.show()

Draw a bar chart with one set of bars

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

y = np.arange(5)
x1 = np.array([10, 8, 7, 11, 13])
x2 = np.array([9, 6, 5, 10, 12])

bar_height = 0.3

plt.barh(y, x1, tick_label=['a', 'b', 'c', 'd', 'e'], height=bar_height)
plt.barh(y + bar_height, x2, height=bar_height)
plt.show()

Draw a bar chart with two set of bars

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

y = np.arange(5)
x1 = np.array([10, 8, 7, 11, 13])
x2 = np.array([9, 6, 5, 10, 12])

bar_height = 0.3

# Draw stacked bar chart
plt.barh(y, x1, tick_label=['a', 'b', 'c', 'd', 'e'], height=bar_height)
plt.barh(y, x2, left=x1, height=bar_height)

plt.show()

Draw stacked bar chart

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

y = np.arange(5)
x1 = np.array([10, 8, 7, 11, 13])
x2 = np.array([9, 6, 5, 10, 12])

bar_height = 0.3

# Draw a bar chart with error bars
# deviation data
error = [2, 1, 2.5, 2, 1.5]
# draw a bar chart with error bars
plt.barh(y, x1, tick_label=['a', 'b', 'c', 'd', 'e'], height=bar_height)
plt.barh(y, x2, left=x1, height=bar_height, xerr=error)

plt.show()

Draw a bar chart with error bars

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

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

x = np.array([0.959, 0.951, 0.935, 0.924, 0.893,
0.892, 0.865, 0.863, 0.860, 0.856,
0.854, 0.835, 0.826, 0.816, 0.798,
0.765, 0.763, 0.67])
y = np.arange(1, 19)
labels = ["家政、家教、保姆等生活服务", "飞机票、火车票", "家具", "手机、手机配件",
"计算机及其配套产品", "汽车用品", "通行充值、游戏充值", "个人护理用品",
"书报杂志及音像制品", "餐饮、旅游、住宿", "家用电器",
"食品、饮料、烟酒、保健品", "家用日杂用品", "保险、演出票务",
"服装、鞋帽、家用纺织品", "数码产品", "其他商品和服务", "工艺品、收藏品"]

plt.figure(figsize=(16, 9))
plt.barh(y, x, tick_label=labels, align='center', height=0.6)

plt.show()

Online shopping substitution rate

4. Draw stacking diagram

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

x = np.arange(6)
y1 = np.array([1, 4, 3, 5, 6, 7])
y2 = np.array([1, 3, 4, 2, 7, 6])
y3 = np.array([3, 4, 3, 6, 5, 5])
# Draw the stacking diagram with three filled areas
plt.stackplot(x, y1, y2, y3)
plt.show()

Draw the stacking diagram with three filled areas

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

x = np.arange(1, 13)
y_a = np.array([198, 215, 245, 222, 200, 236, 201, 253, 236, 200, 266, 290])
y_b = np.array([203, 236, 200, 236, 269, 216, 298, 333, 301, 349, 360, 368])
y_c = np.array([185, 205, 226, 199, 238, 200, 250, 209, 246, 219, 253, 288])
plt.stackplot(x, y_a, y_b, y_c)
plt.show()

Logistics cost statistics of logistics companies

5. Draw histogram

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

# Prepare 50 random test data
scores = np.random.randint(0, 100, 50)
# Draw histogram
plt.hist(scores, bins=8, histtype='stepfilled')
plt.show()

Draws a filled histogram

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

# 10000 random numbers
random_state = np.random.RandomState(19680801)
random_x = random_state.randn(10000)
# Draw a histogram containing 25 rectangular bars
plt.hist(random_x, bins=25)

plt.show()

face recognition using gray-scale histogram

6. Draw pie chart

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

data = np.array([20, 50, 10, 15, 30, 55])
pie_labels = np.array(['A', 'B', 'C', 'D', 'E', 'F'])
# Draw pie chart: the radius is 0.5, and the value retains 1 decimal place
plt.pie(data, radius=1.5, labels=pie_labels, autopct='%3.1f%%')

plt.tight_layout()
plt.show()

Draws a pie chart

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

data = np.array([20, 50, 10, 15, 30, 55])
pie_labels = np.array(['A', 'B', 'C', 'D', 'E', 'F'])
# Draw a ring chart
plt.pie(data, radius=1.5, wedgeprops={'width': 0.7}, labels=pie_labels, autopct='%3.1f%%', pctdistance=0.75)

plt.tight_layout()
plt.show()

Draws a ring chart

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

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)
plt.show()

Alipay monthly bill report

7. Draw scatter diagram

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

num = 50
x = np.random.rand(num)
y = np.random.rand(num)

plt.scatter(x, y)
plt.show()

Draws a scatter diagram

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

num = 50
x = np.random.rand(num)
y = np.random.rand(num)
area = (30 * np.random.rand(num)) ** 2

plt.scatter(x, y, s=area)
plt.show()

Draws a bubble diagram

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

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)
plt.show()

Relationship between vehicle speed and braking distance

8. Draw box diagram

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(100)

plt.boxplot(data, meanline=True, widths=0.3, patch_artist=True,
showfliers=False)
plt.show()

Draw a box diagram without outliers

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

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

data_2018 = np.array([5200, 5254.5, 5283.4, 5107.8, 5443.3, 5550.6,
6400.2, 6404.9, 5483.1, 5330.2, 5543, 6199.9])
data_2017 = np.array([4605.2, 4710.3, 5168.9, 4767.2, 4947, 5203,
6047.4, 5945.5, 5219.6, 5038.1, 5196.3, 5698.6])

# Draw box
plt.boxplot([data_2018, data_2017], labels=['2018年', '2017年'],
meanline=True, widths=0.5, vert=False, patch_artist=True)
plt.show()

National power generation statistics in 2017 and 2018

9. Draw radar chart

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 numpy as np
import matplotlib.pyplot as plt

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

dim_num = 6
data = np.array([[0.40, 0.32, 0.35, 0.30, 0.30, 0.88],
[0.85, 0.35, 0.30, 0.40, 0.40, 0.30],
[0.43, 0.89, 0.30, 0.28, 0.22, 0.30],
[0.30, 0.25, 0.48, 0.85, 0.45, 0.40],
[0.20, 0.38, 0.87, 0.45, 0.32, 0.28],
[0.34, 0.31, 0.38, 0.40, 0.90, 0.28]])
angles = np.linspace(0, 2 * np.pi, dim_num, endpoint=False)
angles = np.concatenate((angles, [angles[0]]))
data = np.concatenate((data, [data[0]]))
# Dim labels
radar_labels = ['研究型(I)', '艺术型(A)', '社会型(S)',
'企业型(E)', '传统型(C)', '现实型(R)']
radar_label = np.concatenate((radar_labels, [radar_labels[0]]))

# Draw a radar chart
plt.polar(angles, data)
# Set polar label
plt.thetagrids(angles * 180 / np.pi, labels=radar_label)
# Fill polygon
plt.fill(angles, data, alpha=0.25)
plt.show()

Holland career interest test

10. Draw error bar chart

1
2
3
4
5
6
7
8
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = (25, 32, 34, 20, 25)
y_offset = (3, 5, 2, 3, 3)
plt.errorbar(x, y, yerr=y_offset, capsize=3, capthick=2)
plt.show()

Draw error bar chart


Use Matplotlib to draw simple charts
https://www.hardyhu.cn/2022/03/11/Use-Matplotlib-to-draw-simple-charts/
Author
John Doe
Posted on
March 11, 2022
Licensed under