Please enable Javascript to view the contents

《Matplotlib精进》

 ·  ☕ 29 分钟  ·  👽 Metasphinx

颜色填充

多边形的颜色填充

规则多边形的颜色填充

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

x = [0, 0, 5, 10, 15, 15, 10, 5]
y = [5, 10, 15, 15, 10, 5, 0, 0]

plt.fill(x, y, color = 'cornflowerblue')

plt.xlim(-1, 16)
plt.ylim(-1, 16)

plt.xticks(np.arange(0, 16, 5))
plt.yticks(np.arange(0, 16, 5))

plt.show()

不规则多边形的颜色填充

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
x = np.linspace(0, 2*np.pi, 500)
y = np.sin(x)

plt.fill(x, y, color = 'cornflowerblue', alpha = 0.4)

plt.plot(x, y, color = 'cornflowerblue', alpha = 0.8)
plt.plot([x[0], x[-1]], [y[0], y[-1]], color = 'cornflowerblue', alpha = 0.8)

plt.xlim(0, 2*np.pi)
plt.ylim(-1.1, 1.1)

plt.show()

交叉曲线的颜色填充

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
x = np.linspace(0, 2, 500)
y1 = np.sin(2*np.pi*x)
y2 = 1.1*np.sin(3*np.pi*x)

fig, ax = plt.subplots(3, 1, sharex = 'all')

# "between y2 and 0"
ax[0].fill_between(x, 0, y2, alpha = 0.5)
ax[0].set_ylim(-1.2, 1.2)

# "between y2 and 1.1"
ax[1].fill_between(x, y2, 1.1, alpha = 0.5)
ax[1].set_ylim(-1.2, 1.2)

# "between y1 and y20"
ax[2].fill_between(x, y1, y2, alpha = 0.5)
ax[2].set_ylim(-1.2, 1.2)

plt.show()

延伸阅读

水平方向的交叉曲线的颜色填充方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
x = np.linspace(0, 2, 500)
y1 = np.sin(2*np.pi*x)
y2 = 1.1 * np.sin(3*np.pi*x)

fig = plt.figure()
ax = fig.add_subplot(111)

# plot yq and plot y2
ax.plot(x, y1, color = 'k', lw = 1, ls = '-')
ax.plot(x, y2, color = 'k', lw = 1, ls = '-')

# 'where y1<=y2'
ax.fill_between(x, y1, y2, where = y2>=y1, interpolate = True, facecolor = 'cornflowerblue', alpha = 0.7)

# 'where y1>=y2'
ax.fill_between(x, y1, y2, where = y2<=y1, interpolate = True, facecolor = 'darkred', alpha = 0.7)

ax.set_xlim(0, 2)
ax.set_ylim(-1.2, 1.2)

ax.grid(ls = ':', lw = 1, color = 'gray', alpha = 0.5)

plt.show()

垂直方向的交叉曲线的颜色填充方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
y = np.linspace(0, 2, 500)
x1 = np.sin(2*np.pi*y)
x2 = 1.1*np.sin(3*np.pi*y)

fig = plt.figure()
ax = fig.add_subplot(111)

# plot x1 and x2
ax.plot(x1, y, color = 'k', lw = 1, ls = '-')
ax.plot(x2, y, color = 'k', lw = 1, ls = '-')

# 'where x1<=x2'
ax.fill_betweenx(y, x1, x2, where = x2>=x1, facecolor = 'cornflowerblue', alpha=0.7)

# 'where x1>=x2'
ax.fill_betweenx(y, x1, x2, where = x2<=x1, facecolor = 'darkred', alpha=0.7)

ax.set_xlim(-1.2, 1.2)
ax.set_ylim(0, 2)

ax.grid(ls = ':', lw = 1, color = 'gray', alpha = 0.5)

plt.show()

综合案例:交叉间断型曲线的颜色填充

 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
47
fig, ax = plt.subplots(1, 2)

# sunplot(121) data
x = np.linspace(0, 2, 500)
y1 = np.sin(2*np.pi*x)
y2 = 1.2*np.sin(3*np.pi*x)

y2 = np.ma.masked_greater(y2, 1.0)

# plot y1 and y2
ax[0].plot(x, y1, color = 'k', lw = 1, ls = '-')
ax[0].plot(x, y2, color = 'k', lw = 1, ls = '-')

# 'where ya <= y2'
ax[0].fill_between(x, y1, y2, where = y2>=y1, facecolor = 'cornflowerblue', alpha = 0.7) 

# 'where y1>=y2'
ax[0].fill_between(x, y1, y2, where = y2<=y1, facecolor = 'darkred', alpha = 0.5) 

ax[0].set_xlim(0, 2)
ax[0].set_ylim(-1.2, 1.2)

ax[0].grid(ls = ':', lw = 1, color = 'gray', alpha = 0.5)

# subplot(122) data
y = np.linspace(0, 2, 500)
x1 = np.sin(2*np.pi*y)
x2 = 1.2*np.sin(3*np.pi*y)

x2 = np.ma.masked_greater(x2, 1.0)

# plot x1 and x2
ax[1].plot(x1, y, color = 'k', lw = 1, ls = '-')
ax[1].plot(x2, y, color = 'k', lw = 1, ls = '-')

# 'where x1<=x2'
ax[1].fill_betweenx(y, x1, x2, where = x2>=x1, facecolor = 'cornflowerblue', alpha = 0.7)

# 'where x1>=x2
ax[1].fill_betweenx(y, x1, x2, where = x2<=x1, facecolor = 'darkred', alpha = 0.7)

ax[1].set_xlim(-1.2, 1.2)
ax[1].set_ylim(0, 2)

ax[1].grid(ls = ':', lw = 1, color = 'gray', alpha = 0.5)

plt.show()

patches模块

圆的实现方法

1
2
3
4
5
6
7
import numpy as np
# import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Arc, Circle, Ellipse, Rectangle, Wedge, Shadow 
# from jupyterthemes import jtplot
# jtplot.style()
# mpl.style.use('ggplot')
 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
fig, ax = plt.subplots(2, 2)

# subplot(221)
circle = Circle((2,2), radius = 2, facecolor = 'white', edgecolor = 'cornflowerblue')
ax[0, 0].add_patch(circle)

ax[0, 0].set_xlim(-1, 5)
ax[0, 0].set_ylim(-1, 5)

# subplot(222)
rectangle = ax[0, 1].patch
rectangle.set_facecolor('gold')

circle = Circle((2,2), radius = 2, facecolor = 'white', edgecolor = 'cornflowerblue')
ax[0, 1].add_patch(circle)

ax[0, 1].set_xlim(-1, 5)
ax[0, 1].set_ylim(-1, 5)
ax[0, 1].set_aspect('equal', 'box')

# subplot(223)
rectangle = ax[1,0].patch
rectangle.set_facecolor("palegreen")
circle = Circle((2, 2), radius=2, facecolor = "white", edgecolor = "cornflowerblue")
ax[1, 0].add_patch(circle)
ax[1, 0].axis("equal")

# subplot(224)
rectangle = ax[1,1].patch
rectangle.set_facecolor("lightskyblue")
circle = Circle((2, 2), radius = 2, facecolor = "white",edgecolor = "cornflowerblue")
ax[1, 1].add_patch(circle)
ax[1, 1].axis([-1,5, -1,5])
ax[1, 1].set_yticks(np.arange(-1, 6, 1))
ax[1, 1].axis("equal")
plt.subplots_adjust(left = 0.1)
plt.show()

 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
fig, ax = plt.subplots(1, 2, subplot_kw={'aspect':'equal'})

# subplot(121)
angles = np.linspace(0, 135, 4)
ellipse = [Ellipse((2, 2), 4, 2, a) for a in angles]

for elle in ellipse:
    ax[0].add_patch(elle)
    elle.set_alpha(0.4)
    elle.set_color("cornflowerblue")

ax[0].axis([-1, 5, -1, 5])

# subplot(122)
num = np.arange(0, 100, 1)
ellipse = [Ellipse(xy = np.random.rand(2)*10, width = np.random.rand(1), height = np.random.rand(1),
           angle = np.random.rand(1)*360) for i in num]

for elle in ellipse:
    ax[1].add_patch(elle)
    elle.set_alpha(float(np.random.rand(1)))
    elle.set_color(np.random.rand(3))

ax[1].axis([-1, 11, -1, 11])
plt.tight_layout()
plt.show()

矩形实现方法

 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
fig, ax = plt.subplots(subplot_kw={'aspect':'equal'})

x1 = np.arange(1, 2.6, 0.1)
y1 = x1 + 2

x2 = np.arange(2.5, 4.1, 0.1)
y2 = -x2 + 7

# set background color
rectangle = ax.patch
rectangle.set_facecolor('lightskyblue')

# house
rectangle1 = Rectangle((1, 0), 3, 3, facecolor = 'w', edgecolor = 'rosybrown')

# door
rectangle2 = Rectangle((1.5, 0), 1, 1.5, facecolor = 'w', edgecolor = 'rosybrown', hatch = '|||')

# window
rectangle3 = Rectangle((2.9, 1.7), 0.6, 0.6, edgecolor = 'rosybrown')

rectangle_list = [rectangle1, rectangle2, rectangle3]

# roof line
ax.plot([1, 2.5, 4], [3, 4.5, 3], color = 'rosybrown')

# window line
ax.plot([3.2, 3.2], [1.7, 2.3], color = 'rosybrown')
ax.plot([2.9, 3.5], [2.0, 2.0], color = 'rosybrown')

# roof filled color
ax.fill_between(x1, 3, y1, color = 'w', interpolate = True)
ax.fill_between(x2, 3, y2, color = 'w', interpolate = True)

for rect in rectangle_list:
    ax.add_patch(rect)
    
ax.axis([0, 5, 0, 6])
plt.grid(False)
plt.show()

圆弧和楔形的绘制方法

 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
47
48
49
50
51
52
53
54
55
fig, ax = plt.subplots(subplot_kw = {'aspect':'equal'})

# shadow
shadow = Ellipse((2.5, 0.5), 4.2, 0.5, color = 'silver', alpha = 0.2)
''
# base
ax.plot([1, 4], [1, 1.3], color = 'k')
base = Arc((2.5, 1.1), 3, 1, angle = 10, theta1 = 0, theta2 = 180, color= 'k', alpha = 0.8)

# wheel 
left_wheel = Ellipse((1, 1), 0.7, 0.4, angle = 95, color = 'k')
right_wheel = Ellipse((4, 1.3), 0.7, 0.4, angle = 85, color = 'k')

# joinstyle
bottom_joinstyle1 = Ellipse((2.5, 2), 1, 0.3, facecolor = 'silver', edgecolor = 'w')
bottom_joinstyle2 = Ellipse((2.5, 1.7), 1, 0.3, facecolor = 'silver', edgecolor = 'w')
left_joinstyle = Ellipse((1.5, 0.75), 0.5, 0.25, angle = 90, color = 'k')
left_arm_jointstyle1 = Wedge((0.3, 4.55), 0.1, 0, 360, color = 'k')
left_arm_jointstyle2 = Wedge((0, 4.0), 0.2, 290, 250, color = 'k')
right_jointstyle = Ellipse((4, 5.75), 0.5, 0.25, angle = 90, color = 'k')
right_arm_jointstyle1 = Wedge((4.3, 6.95), 0.1, 0, 360, color = 'k')
right_arm_jointstyle2 = Wedge((4.3, 7.45), 0.2,110, 70, color = 'k')
top_jointstyle1 = Ellipse((2.5, 6.2), 0.5, 0.2, facecolor = 'silver', edgecolor = 'w')
top_jointstyle2 = Ellipse((2.5, 6.3), 0.5, 0.2, facecolor = 'silver', edgecolor = 'w')

# body
body = Rectangle((1, 2.1), 3, 4, color = 'steelblue')

# arms
left_arm1 = ax.plot([0.3, 1-0.125], [4.55, 5.75], color = "silver", lw = 4)
left_arm2 = ax.plot([0, 0.3], [4.2,4.55], color = "silver", lw = 4)
right_arm1 = ax.plot([4+0.125, 4.3], [5.75, 6.95], color = "silver", lw = 4)
right_arm2 = ax.plot([4.3, 4.3], [6.95, 7.25], color = "silver", lw = 4)

# head
ax.plot([1 ,4], [6.4, 6.4], color = "steelblue")
ax.plot([1,4],[6.4,6.4],color="steelblue")
head = Arc((2.5, 6.4), 3, 2.5, angle = 0, theta1 = 0, theta2 = 180, color = "steelblue")

# eyes
left_eye = Wedge((2, 7), 0.4, 0, 360, color = "gold")
left_eye_center = Wedge((2, 7), 0.3, 15, 345, color = "k")
right_eye = Wedge((3, 7), 0.4, 0, 360, color = "k")
right_eye_center = Wedge((3, 7), 0.3, 165, 195, color = "darkred")

polygon = [shadow, base, left_wheel, right_wheel, bottom_joinstyle1, bottom_joinstyle2, left_joinstyle,
          left_arm_jointstyle1, left_arm_jointstyle2, right_jointstyle, right_arm_jointstyle1,
          right_arm_jointstyle2, top_jointstyle1, top_jointstyle1, body, head, left_eye, left_eye_center,
          right_eye, right_eye_center]

for pln in polygon:
    ax.add_patch(pln)

ax.axis([-1, 6, 0, 10])
plt.show()

延伸阅读

使用折线绘制圆

 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
fig, ax = plt.subplots(2, 2)

x = np.linspace(0, 2*np.pi, 500)
y1 = 2 * np.cos(x)
y2 = 2 * np.sin(x)

# subplot(221)
ax[0, 0].plot(y1, y2, color = 'cornflowerblue', lw = 2)
ax[0, 0].set_xlim(-3, 3)
ax[0, 0].set_ylim(-3, 3)

# subplot(222)
rectangle = ax[0, 1].patch
rectangle.set_facecolor('gold')
ax[0, 1].plot(y1, y2, color = 'cornflowerblue', lw = 2)
ax[0, 1].set_xlim(-3, 3)
ax[0, 1].set_ylim(-3, 3)
ax[0, 1].set_aspect('equal', 'box')

# subplot(223)
rectangle = ax[1, 0].patch
rectangle.set_facecolor("palegreen")
ax[1, 0].plot(y1, y2, color = "cornflowerblue", lw = 2)
ax[1, 0].axis("equal")

# subplot(224)
rectangle = ax[1, 1].patch
rectangle.set_facecolor("lightskyblue")
ax[1, 1].plot(y1, y2, color = "cornflowerblue", lw = 2)
ax[1, 1].axis([-3, 3, -3, 3])
ax[1, 1].set_yticks(np.arange(-3, 4, 1))
ax[1, 1].axis("equal")
plt.subplots_adjust(left = 0.1)
plt.show()

使用椭圆绘制圆

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
fig, ax = plt.subplots(1, 1, subplot_kw = {'aspect':'equal'})
circle = Circle((2, 2), radius = 1)
angles = np.linspace(0, 135, 4)
ellipse = [Ellipse((2, 2), 2, 2, a) for a in angles]
ellipse.append(circle)
polygon = ellipse

for pln in polygon:
    ax.add_patch(pln)
    pln.set_alpha(float(np.random.rand(1)))
    pln.set_color(np.random.rand(3))

ax.axis([0, 4, 0, 4])
ax.set_xticks(np.arange(0, 5, 1))
ax.set_yticks(np.arange(0, 5, 1))

plt.show()

使用楔形绘制饼图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fig, ax = plt.subplots(subplot_kw = {'aspect':'equal'})
font_style = {'family':'serif', 'size':12, 'style':'italic', 'weight':'black'}
sample_data = [350, 150, 200, 300]
total = sum(sample_data)
percents = [i/float(total) for i in sample_data]
angles = [360*i for i in percents]
delta = 45
wedge1 = Wedge((2,2), 1, delta, delta+sum(angles[0:1]), color = 'orange')
wedge2 = Wedge((2,1.9), 1, delta+sum(angles[0:1]), delta+sum(angles[0:2]), facecolor = 'steelblue', edgecolor = 'white')
wedge3 = Wedge((2,1.9), 1, delta+sum(angles[0:2]), delta+sum(angles[0:3]), facecolor = 'darkred', edgecolor = 'white')
wedge4 = Wedge((2,1.9), 1, delta+sum(angles[0:3]), delta+sum(angles[0:4]), facecolor = 'lightgreen', edgecolor = 'white')

wedges = [wedge1, wedge2, wedge3, wedge4]
for wedge in wedges:
    ax.add_patch(wedge)
    
ax.text(1.5, 2.5, "%3.1f%%" % (percents[0]*100), font_style)
ax.text(1.0, 1.7, "%3.1f%%" % (percents[1]*100), font_style)
ax.text(1.5, 1.2, "%3.1f%%" % (percents[2]*100), font_style)    
ax.text(2.2, 1.7, "%3.1f%%" % (percents[3]*100), font_style)    
    
ax.axis([0, 4, 0, 4])
plt.show()

使用楔形绘制圆环式饼图

 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
fig, ax = plt.subplots(subplot_kw = {'aspect':'equal'})
font_style = {'family':'serif', 'size':9, 'style':'italic', 'weight':'black'}
sample_data = [350, 150, 200, 300]
total = sum(sample_data)
percents = [i/float(total) for i in sample_data]
angles = [360*i for i in percents]
delta = 45
wedge1 = Wedge((2,1.9), 1, delta, delta+sum(angles[0:1]), facecolor = 'orange',
              edgecolor = 'white', width = 0.3)
wedge2 = Wedge((2,1.9), 1, delta+sum(angles[0:1]), delta+sum(angles[0:2]),
               facecolor = 'steelblue', edgecolor = 'white', width = 0.3)
wedge3 = Wedge((2,1.9), 1, delta+sum(angles[0:2]), delta+sum(angles[0:3]),
               facecolor = 'darkred', edgecolor = 'white', width = 0.3)
wedge4 = Wedge((2,1.9), 1, delta+sum(angles[0:3]), delta+sum(angles[0:4]),
               facecolor = 'lightgreen', edgecolor = 'white', width = 0.3)
rectangle = Rectangle((3.0, 0.0), 1.3, 1.3, facecolor = 'w', edgecolor = 'rosybrown')
rectangle1 = Rectangle((3.2, 0.1), 0.3, 0.2, facecolor = 'orange')
rectangle2 = Rectangle((3.2, 0.4), 0.3, 0.2, facecolor = 'steelblue')
rectangle3 = Rectangle((3.2, 0.7), 0.3, 0.2, facecolor = 'darkred')
rectangle4 = Rectangle((3.2, 1.0), 0.3, 0.2, facecolor = 'lightgreen')
                       
wedges = [wedge1, wedge2, wedge3, wedge4, rectangle, rectangle1, rectangle2, rectangle3, rectangle4]
for wedge in wedges:
    ax.add_patch(wedge)

ax.text(3.6, 0.1, "%3.1f%%" % (percents[0]*100), font_style)
ax.text(3.6, 0.4, "%3.1f%%" % (percents[1]*100), font_style)
ax.text(3.6, 0.7, "%3.1f%%" % (percents[2]*100), font_style)    
ax.text(3.6, 1.0, "%3.1f%%" % (percents[3]*100), font_style)    
    
ax.axis([0, 4.5, -0.5, 4])
plt.show()

组合图形

机器学习中的判别分析示意图

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

fig, ax = plt.subplots()

num = 50

# new sample
sample = 10*np.random.rand(num, 2)
var1 = sample[:, 0]
var2 = sample[:, 1]

# threshold value
td = 12

# discriminant function
df = 2*var1+var2

cates11 = np.ma.masked_where(df>=td, var1)
cates12 = np.ma.masked_where(df>=td, var2)

cates21 = np.ma.masked_where(df<=td, var1)
cates22 = np.ma.masked_where(df<=td, var2)

ax.scatter(var1, var2, s = cates11*50, marker = 's', c = cates11)
ax.scatter(var1, var2, s = cates21*50, marker = 'o', c = cates21)

ax.plot(var1, -2*var1+12, lw = 1, color = 'b', alpha = 0.65)
ax.axis([-1, 11, -1, 11])
plt.show()

日期型时间序列图

 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
import datetime
import matplotlib.dates as mdates

fig, ax = plt.subplots()
months = mdates.MonthLocator()  # a Locator instance
dateFmt = mdates.DateFormatter('%m/%d/%y')  # a Formatter instance

# format the ticks
ax.xaxis.set_major_formatter(dateFmt)
ax.xaxis.set_minor_locator(months)

# set appearance parameters for ticks, ticklabels, and gridlines
ax.tick_params(axis = 'both', direction = 'out', labelsize = 10) 

date1 = datetime.date(2008, 4, 17)
date2 = datetime.date(2017, 5, 4)
delta = datetime.timedelta(days = 5)
dates = mdates.drange(date1, date2, delta)

y = np.random.normal(100, 15, len(dates))

ax.plot_date(dates, y, 'b-', alpha = 0.7)

fig.autofmt_xdate()

plt.show()

向直方图中添加概率密度曲线

 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 as mpl

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

mu = 60.2
sigma = 2.0
x = mu + sigma*np.random.randn(500)

bins = 50

fig, ax = plt.subplots(1, 1)

n, bins, patches = ax.hist(x, bins, density = True, stacked = True, histtype = 'bar',
                           facecolor = 'cornflowerblue', edgecolor = 'white', alpha = 0.75)
y = ((1/(np.power(2*np.pi, 0.5)*sigma))*np.exp(-0.5*np.power((bins-mu)/sigma, 2)))

ax.plot(bins, y, color= 'orange', ls = '--', lw = 2)

ax.grid(ls = ':', lw = 1, color = 'gray', alpha = 0.2)

ax.text(54, 0.2, r"$y=\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{{x-\mu}^2}{2\sigma^2}}$",
        {'color':'r', 'fontsize':20})
ax.set_xlabel('体重')
ax.set_ylabel('概率密度')
ax.set_title(r'体重的直方图:$\mu=60.0$, $\sigma=2.0$', fontsize = 16)

plt.show()

 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
from matplotlib.patches import Polygon

mu = 60.2
sigma = 2.0
x = mu + sigma*np.random.randn(500)

bins = 50

fig, ax = plt.subplots(1, 1)

n, bins, patches = ax.hist(x, bins, density = True, stacked = True, histtype = 'bar',
                           facecolor = 'cornflowerblue', edgecolor = 'white', alpha = 0.75)

y = ((1/(np.power(2*np.pi, 0.5)*sigma))*np.exp(-0.5*np.power((bins-mu)/sigma, 2)))

ax.plot(bins, y, color= 'orange', ls = '--', lw = 2)

integ_x = np.linspace(mu-2*sigma, mu+2*sigma, 1000)
integ_y = ((1/(np.power(2*np.pi, 0.5)*sigma))*np.exp(-0.5*np.power((integ_x-mu)/sigma, 2)))
area = [(mu-2*sigma, 0), *zip(integ_x, integ_y), (mu+2*sigma, 0)]

poly = Polygon(area, facecolor = 'gray', edgecolor = 'k', alpha = 0.6, closed = False)
ax.add_patch(poly)

ax.grid(ls = ':', lw = 1, color = 'gray', alpha = 0.2)

ax.text(54, 0.2, r"$y=\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{{x-\mu}^2}{2\sigma^2}}$",
        {'color':'r', 'fontsize':20})
ax.set_xlabel('体重')
ax.set_ylabel('概率密度')
ax.set_title(r'体重的直方图:$\mu=60.0$, $\sigma=2.0$', fontsize = 16)

plt.text(0.45, 0.2, r"$\int_{\mu-2\sigma}^{\mu+2\sigma} y\mathrm{d}x$", fontsize = 20,
         transform = ax.transAxes)

plt.show()

绘图区域嵌套子绘图区域

 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
mu = 75.0
sigma = 15.0
bins = 20
x = np.linspace(1, 100, 200)
y = np.random.normal(mu, sigma, 200)

fig, ax = plt.subplots()

# the main axes
ax.plot(x, y, ls = '-', lw = 2, color = 'steelblue')
ax.set_ylim(10, 170)

# this is an inset axes over the main axes
plt.axes([0.2, 0.6, 0.2, 0.2], facecolor = 'k')
count, bins, patches = plt.hist(y, bins, color = 'cornflowerblue')
plt.ylim(0, 28)
plt.xticks([])
plt.yticks([])

# this is an inset axes over the inset axes
plt.axes([0.21, 0.72, 0.05, 0.05])
y1 = (1/(sigma*np.sqrt(2*np.pi)))*np.exp(-(bins-mu)**2/(2*sigma**2))
plt.plot(bins, y1, ls = '-', color = 'r')
plt.xticks([])
plt.yticks([])

# this is another inset axes over the main axes
plt.axes([0.65, 0.6, 0.2, 0.2], fc = 'k')
aount, bins, patches = plt.hist(y, bins, color = 'cornflowerblue', density = True, stacked = True,
                                cumulative = True, histtype = 'step')
plt.ylim(0, 1.0)
plt.xticks([])
plt.yticks([])

# this is another inset axes over another inset axes
plt.axes([0.66, 0.72, 0.05, 0.05])
y2 = (1/(sigma*np.sqrt(2*np.pi)))*np.exp(-(bins-mu)**2/(2*sigma**2))
y2 = y2.cumsum()
y2 = y2/y2[-1]
plt.plot(bins, y2, ls = '-', color = 'r')
plt.xticks([])
plt.yticks([])

plt.show()

延伸阅读:设置一般化的日期刻度线

 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
fig, ax = plt.subplots()

# tick every 5th easter
rule = mdates.rrulewrapper(mdates.YEARLY, byeaster = 0, interval = 2)
loc = mdates.RRuleLocator(rule)  # a Locator instance

dateFmt = mdates.DateFormatter('%m/%d/%y')  # a Formatter instance

# format the ticks
ax.xaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(dateFmt)

# set appearance parameters for ticks, ticklabels, and fridlines
ax.tick_params(axis = 'both', direction = 'out', labelsize = 10)

date1 = datetime.date(2004, 5, 17)
date2 = datetime.date(2016, 6, 4)
delta = datetime.timedelta(days = 5)
dates = mdates.drange(date1, date2, delta)

y = np.random.normal(120, 12, len(dates))

ax.plot_date(dates, y, 'b-', alpha = 0.7)

fig.autofmt_xdate()

plt.show()

文本样式布局

文本注解的展示样式

文本框的样式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
from matplotlib.patches import Rectangle
import matplotlib.patches as patches
import matplotlib.pyplot as plt
from jupyterthemes import jtplot
jtplot.style(theme='grade3', context='paper')

fig = plt.figure(1, figsize = (8, 9), dpi = 72)
fontsize = 0.25*fig.dpi

# subplot(111)
ax = fig.add_subplot(1, 1, 1, frameon = False, xticks = [], yticks = [])

boxStyles = patches.BoxStyle.get_styles()
boxStyleNames = list(boxStyles.keys())
boxStyleNames.sort()

for i, name in enumerate(boxStyleNames):
    ax.text(float(i + 0.5)/len(boxStyleNames), (float(len(boxStyleNames))-0.5-i)/len(boxStyleNames),
            name, ha = 'center', size = fontsize, transform = ax.transAxes,
            bbox = dict(boxstyle = name, fc = 'w', ec =  'k'))
plt.show()

文本注释箭头的样式

 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
fig = plt.figure(1, figsize = (8, 9), dpi = 72)
fontsize = 0.23*fig.dpi

# subplot(111)
ax = fig.add_subplot(1, 1, 1, frameon = False)
arrowStyles = patches.ArrowStyle.get_styles()
arrowStyleNames = list(arrowStyles)
arrowStyleNames.sort()

ax.set_xlim(0, len(arrowStyleNames)+2.5)
ax.set_ylim(-2, len(arrowStyleNames))

for i, name in enumerate(arrowStyleNames):
    p = patches.Circle((float(len(arrowStyleNames))+1.2-i, float(len(arrowStyleNames))-2.8-i),
                        0.2, color = 'steelblue', alpha = 1.0)
    ax.add_patch(p)
    ax.annotate(name,
                (float(len(arrowStyleNames))+1.2-i, float(len(arrowStyleNames))-2.8-i),
                (float(len(arrowStyleNames))-1-i, float(len(arrowStyleNames))-3-i),
                xycoords = 'data', ha = 'center', size = fontsize,
                arrowprops = dict(arrowstyle = name, facecolor = 'k', edgecolor = 'k', patchB = p,
                                  shrinkA = 5, shrinkB = 5, connectionstyle = 'arc3'),
                bbox = dict(boxstyle = 'round', fc = 'w', ec = 'k'))
    
    ax.xaxis.set_visible(False)  # ax.set_xticks([])
    ax.yaxis.set_visible(False)  # ax.set_yticks([])
plt.savefig('xxx.tif', dpi = 300)

文本内容的布局

 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
47
48
49
50
51
52
53
54
55
fig = plt.figure(1, figsize = (8, 8), dpi = 80, facecolor = 'w')
fontsize = 0.3*fig.dpi
font_style = {'family':'sans-serif', 'fontsize':fontsize, 'weight':'black'}

# add axes in axis coords
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], fc = 'gold')

left = 0.2
bottom= 0.2
right = 0.8
top = 0.8
width = right-left
height = top-bottom

# add a rectangle in axis coords
rect = Rectangle((left, bottom), width, height, transform = ax.transAxes, facecolor = 'w', edgecolor = 'k')
ax.add_patch(rect)

# add text in axis coords
#  left-bottom
ax.text(left, bottom, 'left bottom', ha = 'left', va = 'bottom', transform = ax.transAxes, **font_style)

# left top
ax.text(left, bottom, 'left top', ha = 'left', va = 'top', transform = ax.transAxes, **font_style)

# right-bottom
ax.text(right, top, 'right bottom', ha = 'right', va = 'bottom', transform = ax.transAxes, **font_style)

# right top
ax.text(right, top, 'right top', ha = 'right', va = 'top', transform = ax.transAxes, **font_style)

# center top
ax.text(right, bottom, 'center top', ha = 'center', va = 'top', transform = ax.transAxes, **font_style)

# center bottom
ax.text(right, bottom, 'center bottom', ha = 'center', va = 'bottom', transform = ax.transAxes, **font_style)

# left center
ax.text(left, top, 'left center', ha = 'left', va = 'center', transform = ax.transAxes, **font_style)

# right center
ax.text(right, 0.5, 'right center', ha = 'right', va = 'center', transform = ax.transAxes, rotation = 90, **font_style)

# center center
ax.text(left, 0.5, 'center center', ha = 'center', va = 'center', transform = ax.transAxes, rotation = 90,  **font_style)

# middle
ax.text(0.5, 0.5, 'middle', ha = 'center', va = 'center', transform = ax.transAxes, color = 'r', **font_style)

# rotated center-center
# left-center
ax.text(left*0.7, top + 0.05, 'rotated\ncenter center', ha = 'center', va = 'center', transform = ax.transAxes,
        rotation = 45,  **font_style)

plt.show()

延伸阅读

文本自动换行

 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
fig = plt.figure(1, figsize = (8, 8), dpi = 80, facecolor = 'w')

# add axes in axis coords
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], fc = 'gold')

left = 0.2
bottom= 0.2
right = 0.8
top = 0.8
width = right-left
height = top-bottom

the_zen_of_Python = """Explicit is better than implicit. Complex is better than complicated."""

# add a rectangle in axis coords
rect = Rectangle((left, bottom), width, height, transform = ax.transAxes, facecolor = 'w', edgecolor = 'k')
ax.add_patch(rect)                 
                 
ax.text(0.5, 1, the_zen_of_Python, transform = ax.transAxes, fontsize = 18, weight = 'black', ha = 'center',
              va = 'top', wrap = True)._get_wrap_line_width = lambda : 200.

ax.text(0.5*width, 0.5, the_zen_of_Python, transform = ax.transAxes, fontsize = 10, weight = 'black', ha = 'right',
              rotation = 20, style = 'italic', family = 'monospace', wrap = True)._get_wrap_line_width = lambda : 150.

ax.text(left, bottom, the_zen_of_Python, transform = ax.transAxes, fontsize = 15, weight = 'bold', ha = 'left', va = 'center',
              rotation = -15, style = 'oblique', family = 'serif', wrap = True)._get_wrap_line_width = lambda : 600.

ax.text(width, 0.5*height, the_zen_of_Python, transform = ax.transAxes, fontsize = 18, weight = 'bold', ha = 'left',
              rotation = 15, style = 'normal', family = 'sans-serif', wrap = True)._get_wrap_line_width = lambda : 250.

plt.show()

文本内容的旋转角度

 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
import matplotlib.text as mtext
import matplotlib.transforms as mtransforms

class RotationAwareAnnotation(mtext.Annotation):
    def __init__(self, s, xy, p, pa=None, ax=None, **kwargs):
        self.ax = ax or plt.gca()
        self.p = p
        if not pa:
            self.pa = xy
        self.calc_angle_data()
        kwargs.update(rotation_mode=kwargs.get("rotation_mode", "anchor"))
        mtext.Annotation.__init__(self, s, xy, **kwargs)
        self.set_transform(mtransforms.IdentityTransform())
        if 'clip_on' in kwargs:
            self.set_clip_path(self.ax.patch)
        self.ax._add_text(self)

    def calc_angle_data(self):
        ang = np.arctan2(self.p[1]-self.pa[1], self.p[0]-self.pa[0])
        self.angle_data = np.rad2deg(ang)

    def _get_rotation(self):
        return self.ax.transData.transform_angles(np.array((self.angle_data,)), 
                            np.array([self.pa[0], self.pa[1]]).reshape((1, 2)))[0]

    def _set_rotation(self, rotation):
        pass

    _rotation = property(_get_rotation, _set_rotation)


fig, ax = plt.subplots()
t = np.arange(0.0, 1.0, 0.01)
line, = ax.plot(t, t, color='blue', lw=2)

ra = RotationAwareAnnotation("ticklabel Rule", xy=(.5,.5), p=(.6,.6), ax=ax,
                             xytext=(2,-1), textcoords="offset points", va="top")

文本内容的旋转模式

 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
fig,ax = plt.subplots(1,2)

# subplot(121)
# ha-va:top 
ax[0].text(0.5, 2.5, "text45", ha = "left", va = "top", rotation = 45, rotation_mode = "default", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[0].text(1.5, 2.5, "text45", ha = "center", va = "top", rotation = 45, rotation_mode = "default", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[0].text(2.5, 2.5, "text45", ha = "right", va = "top", rotation = 45, rotation_mode = "default", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0,"alpha":0.5})
# ha-va:center 
ax[0].text(0.5, 1.5, "text45", ha = "left", va = "center", rotation = 45, rotation_mode = "default", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[0].text(1.5, 1.5, "text45", ha = "center", va = "center", rotation = 45, rotation_mode = "default", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[0].text(2.5, 1.5, "text45", ha = "right", va = "center", rotation=45, rotation_mode = "default", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
# ha-va:bottom 
ax[0].text(0.5, 0.5, "text45", ha = "left", va = "bottom", rotation = 45, rotation_mode = "default", 
           bbox={"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[0].text(1.5, 0.5, "text45", ha = "center", va = "bottom", rotation = 45, rotation_mode = "default", 
           bbox={"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[0].text(2.5, 0.5, "text45", ha = "right", va = "bottom",rotation = 45, rotation_mode = "default", 
           bbox={"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 

# set text point 
ax[0].scatter([0.5, 1.5, 2.5, 0.5, 1.5, 2.5, 0.5, 1.5, 2.5], 
              [2.5 ,2.5, 2.5, 1.5, 1.5, 1.5, 0.5, 0.5, 0.5],
              c = "r", s = 50, alpha = 0.5) 
# ticklabel and tickline limit 
ax[0].set_xticks([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]) 
ax[0].set_xticklabels(["", "left", "", "center", "", "right", ""], fontsize = 15) 
ax[0].set_yticks([3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0]) 
ax[0].set_yticklabels(["", "top", "", "center", "", "bottom", ""], rotation = 90, fontsize = 15) 
ax[0].set_xlim(0.0, 3.0) 
ax[0].set_ylim(0.0, 3.0) 
ax[0].grid(ls = "-", lw = 2, color = "b", alpha = 0.5) 
ax[0].set_title("default", fontsize = 18) 

# subplot(122)
# ha-va:top 
ax[1].text(0.5, 2.5, "text45", ha = "left", va = "top", rotation = 45, rotation_mode = "anchor", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[1].text(1.5, 2.5, "text45", ha = "center", va = "top", rotation = 45, rotation_mode = "anchor", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[1].text(2.5, 2.5, "text45", ha = "right", va = "top", rotation = 45, rotation_mode = "anchor", 
           bbox={"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
# ha-va:center 
ax[1].text(0.5, 1.5, "text45", ha = "left", va = "center", rotation = 45, rotation_mode = "anchor", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[1].text(1.5, 1.5, "text45", ha = "center", va = "center", rotation = 45, rotation_mode = "anchor", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[1].text(2.5, 1.5, "text45", ha = "right", va = "center", rotation = 45, rotation_mode = "anchor", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
# ha-va:bottom 
ax[1].text(0.5, 0.5, "text45", ha = "left", va = "bottom", rotation = 45, rotation_mode = "anchor", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[1].text(1.5, 0.5, "text45", ha = "center", va = "bottom", rotation = 45, rotation_mode = "anchor",
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 
ax[1].text(2.5, 0.5, "text45", ha = "right", va = "bottom", rotation = 45, rotation_mode = "anchor", 
           bbox = {"boxstyle":"square", "facecolor":"gray", "edgecolor":"w", "pad":0, "alpha":0.5}) 

# set text point 
ax[1].scatter([0.5, 1.5, 2.5, 0.5, 1.5, 2.5, 0.5, 1.5, 2.5], 
              [2.5, 2.5, 2.5, 1.5, 1.5, 1.5, 0.5, 0.5, 0.5], 
              c = "r", s = 50, alpha = 0.5)
# ticklabel and tickline limit 
ax[1].set_xticks([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]) 
ax[1].set_xticklabels(["", "left", "", "center", "", "right", ""], fontsize = 15) 
ax[1].set_yticks([3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0]) 
ax[1].set_yticklabels(["", "top", "", "center", "", "bottom", ""], rotation = 90, fontsize = 15) 
ax[1].set_xlim(0.0, 3.0) 
ax[1].set_ylim(0.0, 3.0) 
ax[1].grid(ls = "-", lw = 2, color = "b", alpha = 0.5) 
ax[1].set_title("anchor", fontsize = 18) 
plt.show()

多行文本的对齐方式

 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
fig,ax = plt.subplots(1,1) 
 
# ha:left--va:baseline--multialignment: left,center, and right 
ax.text(0.5, 2.5, "text0\nTEXT0\nalignment", ha = "left", va = "baseline", multialignment = "left", 
        bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k", alpha = 0.5)) 
ax.text(0.5, 1.5, "text0\nTEXT0\nalignment", ha = "left", va = "baseline", multialignment = "center", 
        bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k", alpha = 0.5)) 
ax.text(0.5, 0.5, "text0\nTEXT0\nalignment", ha = "left", va = "baseline", multialignment = "right", 
        bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k", alpha = 0.5)) 
# ha:center--va:baseline--multialignment: left,center, and right 
ax.text(1.5, 2.5, "text0\nTEXT0\nalignment", ha = "center", va = "baseline", multialignment = "left", 
        bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k", alpha = 0.5)) 
ax.text(1.5, 1.5, "text0\nTEXT0\nalignment", ha = "center", va = "baseline", multialignment = "center", 
        bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k", alpha = 0.5)) 
ax.text(1.5, 0.5, "text0\nTEXT0\nalignment", ha = "center", va = "baseline", multialignment = "right", 
        bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k", alpha = 0.5))
# ha:right--va:baseline--multialignment: left,center, and right 
ax.text(2.5, 2.5, "text0\nTEXT0\nalignment", ha = "right", va = "baseline", multialignment = "left", 
        bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k", alpha = 0.5)) 
ax.text(2.5, 1.5, "text0\nTEXT0\nalignment", ha = "right", va ="baseline", multialignment = "center", 
        bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k", alpha = 0.5)) 
ax.text(2.5, 0.5, "text0\nTEXT0\nalignment", ha = "right", va = "baseline", multialignment = "right",
        bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k", alpha = 0.5))

# set text point 
ax.scatter([0.5, 1.5, 2.5, 0.5, 1.5, 2.5, 0.5, 1.5, 2.5], 
           [2.5, 2.5, 2.5, 1.5, 1.5, 1.5, 0.5, 0.5, 0.5], 
           c = "r", s = 50, alpha = 0.6) 
# ticklabel and tickline limit 
ax.set_xticks([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]) 
ax.set_xticklabels(["", "left", "", "center", "", "right", ""], fontsize = 15) 
ax.set_yticks([3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0]) 
ax.set_yticklabels(["", "baseline", "", "baseline", "", "baseline", ""], rotation = 90, fontsize = 15) 
ax.set_xlim(0.0, 3.0) 
ax.set_ylim(0.0, 3.0) 
ax.grid(ls = "-", lw = 2, color = "b", alpha = 0.5)

文本注释箭头的连接风格

  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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
fig, ax = plt.subplots(3, 5, figsize = (16, 10), sharex = True, sharey = True)
x1, y1 = 0.3, 0.2
x2, y2 = 0.8, 0.6
bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k")

# subplot(3,5,1)
ax[0, 0].plot([x1, x2], [y1, y2], ".")
ax[0, 0].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data",
                 arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3,
                                   shrinkA = 5, shrinkB = 5, patchA = None, patchB = None,
                                   connectionstyle = "angle, angleA = 0, angleB = 90, rad = 5"))
ax[0, 0].text(0.05, 0.95, "angle, \nangleA = 0, \nangleB = 90, \nrad = 5", ha = "left", va = "top", bbox = bbox)
ax[0, 0].set_xlim(0, 1)
ax[0, 0].set_ylim(0, 1)
ax[0, 0].set_xticklabels([])
ax[0, 0].set_yticklabels([])

# subplot(3,5,6)
ax[1, 0].plot([x1, x2], [y1, y2], ".")
ax[1,0].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                 arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                   shrinkA = 5, shrinkB = 5, patchA = None, patchB = None,
                                   connectionstyle = "angle, angleA = 20, angleB = 50, rad = 10"))
ax[1, 0].text(0.05, 0.95, "angle, \nangleA=20, \nangleB = 50, \nrad = 10", ha = "left", va = "top", bbox = bbox)
ax[1, 0].set_xlim(0, 1)
ax[1, 0].set_ylim(0, 1)
ax[1, 0].set_xticklabels([])
ax[1, 0].set_yticklabels([])

# subplot(3,5,11)
ax[2, 0].plot([x1, x2], [y1, y2], ".")
ax[2, 0].annotate("", xy=(x1, y1), xycoords = "data", xytext = (x2,y2), textcoords = "data", 
                  arrowprops=dict(arrowstyle = "->", color = "gray", lw = 3,
                                  shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                  connectionstyle = "angle, angleA = -90, angleB = 0, rad = 0.0"))
ax[2, 0].text(0.05, 0.95, "angle, \nangleA=-90, \nangleB=0, \nrad = 0.0", ha = "left", va = "top" ,bbox = bbox)
ax[2, 0].set_xlim(0, 1)
ax[2, 0].set_ylim(0, 1)
ax[2, 0].set_xticklabels([])
ax[2, 0].set_yticklabels([])

# subplot(3,5,2)
ax[0, 1].plot([x1, x2], [y1, y2], ".")
ax[0, 1].annotate("", xy=(x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data",
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "angle3, angleA = 90, angleB = 0"))
ax[0, 1].text(0.05, 0.95, "angle3, \nangleA=90, \nangleB = 0", ha = "left", va = "top", bbox = bbox)
ax[0, 1].set_xlim(0, 1)
ax[0, 1].set_ylim(0, 1)
ax[0, 1].set_xticklabels([])
ax[0, 1].set_yticklabels([])

# subplot(3,5,7)
ax[1, 1].plot([x1, x2], [y1, y2], ".")
ax[1, 1].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                 arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3,  
                                   shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                   connectionstyle = "angle3, angleA = 0, angleB = 90"))
ax[1, 1].text(0.05, 0.95, "angle3, \nangleA = 0, \nangleB = 90", ha = "left", va = "top", bbox = bbox)
ax[1, 1].set_xlim(0, 1)
ax[1, 1].set_ylim(0, 1)
ax[1, 1].set_xticklabels([])
ax[1, 1].set_yticklabels([])

# subplot(3,5,12)
ax[2, 1].plot([x1, x2], [y1, y2], ".")
ax[2, 1].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                  arrowprops = dict(arrowstyle = "->", color="gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None,
                                    connectionstyle = "angle3, angleA = 20, angleB = 60"))
ax[2, 1].text(0.05, 0.95, "angle3, \nangleA = 20, \nangleB = 60", ha = "left", va = "top", bbox = bbox)
ax[2, 1].set_xlim(0, 1)
ax[2, 1].set_ylim(0, 1)
ax[2, 1].set_xticklabels([])
ax[2, 1].set_yticklabels([])

# subplot(3,5,3)
ax[0, 2].plot([x1, x2], [y1, y2], ".")
ax[0, 2].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "arc, angleA = -90, armA = 30, angleB = 0, \
                                                       armB = 30, rad = 0.0"))
ax[0, 2].text(0.05, 0.95, "arc, \nangleA = -90, \narmA = 30, \nangleB = 0, \narmB = 30, \nrad = 0.0", 
              ha = "left", va = "top", bbox = bbox)
ax[0, 2].set_xlim(0, 1)
ax[0, 2].set_ylim(0, 1)
ax[0, 2].set_xticklabels([])
ax[0, 2].set_yticklabels([])

# subplot(3,5,8)
ax[1, 2].plot([x1, x2], [y1, y2], ".")
ax[1, 2].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data",
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "arc, angleA = -90, armA = 30, angleB = 0,  \
                                                       armB = 30, rad = 3"))
ax[1, 2].text(0.05, 0.95, "arc, \nangleA = -90, \narmA = 30, \nangleB = 0, \narmB = 30, \nrad = 3", 
              ha = "left", va = "top", bbox = bbox)
ax[1, 2].set_xlim(0, 1)
ax[1, 2].set_ylim(0, 1)
ax[1, 2].set_xticklabels([])
ax[1, 2].set_yticklabels([])

# subplot(3,5,13)
ax[2, 2].plot([x1, x2], [y1, y2], ".")
ax[2, 2].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "arc, angleA = 100, armA = 0, angleB = 20, \
                                                       armB = 50, rad = 10"))
ax[2, 2].text(0.05, 0.95, "arc, \nangleA = 100, \narmA = 0, \nangleB = 20, \narmB = 50, \nrad = 10", 
              ha = "left", va = "top", bbox = bbox)
ax[2, 2].set_xlim(0, 1)
ax[2, 2].set_ylim(0, 1)
ax[2, 2].set_xticklabels([])
ax[2, 2].set_yticklabels([])

# subplot(3,5,4)
ax[0, 3].plot([x1, x2], [y1, y2], ".")
ax[0, 3].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "arc3, rad = 0.0"))
ax[0, 3].text(0.05, 0.95, "arc3, \nrad = 0.0", ha = "left", va = "top", bbox = bbox)
ax[0, 3].set_xlim(0, 1)
ax[0, 3].set_ylim(0, 1)
ax[0, 3].set_xticklabels([])
ax[0, 3].set_yticklabels([])

# subplot(3,5,9)
ax[1, 3].plot([x1, x2], [y1, y2], ".")
ax[1, 3].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "arc3, rad = 0.5"))
ax[1, 3].text(0.05, 0.95, "arc3, \nrad = 0.5", ha = "left", va = "top", bbox = bbox)
ax[1, 3].set_xlim(0, 1)
ax[1, 3].set_ylim(0, 1)
ax[1, 3].set_xticklabels([])
ax[1, 3].set_yticklabels([])

# subplot(3,5,14)
ax[2, 3].plot([x1, x2], [y1, y2], ".")
ax[2, 3].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "arc3, rad = -0.5"))
ax[2, 3].text(0.05, 0.95, "arc3, \nrad = -0.5", ha = "left", va = "top", bbox = bbox)
ax[2, 3].set_xlim(0, 1)
ax[2, 3].set_ylim(0, 1)
ax[2, 3].set_xticklabels([])
ax[2, 3].set_yticklabels([])

# subplot(3,5,5)
ax[0, 4].plot([x1, x2], [y1, y2], ".")
ax[0, 4].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "bar, armA = 30, armB = 30, fraction = 0.0"))
ax[0, 4].text(0.05, 0.95, "bar, \narmA = 30, \narmB = 30, \nfraction = 0.0", ha = "left", va = "top", bbox = bbox)
ax[0, 4].set_xlim(0, 1)
ax[0, 4].set_ylim(0, 1)
ax[0, 4].set_xticklabels([])
ax[0, 4].set_yticklabels([])

# subplot(3,5,10)
ax[1, 4].plot([x1, x2], [y1, y2], ".")
ax[1, 4].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "bar, fraction = -0.3"))
ax[1, 4].text(0.05, 0.95, "bar, \nfraction = -0.3", ha = "left", va = "top", bbox = bbox)
ax[1, 4].set_xlim(0, 1)
ax[1, 4].set_ylim(0, 1)
ax[1, 4].set_xticklabels([])
ax[1, 4].set_yticklabels([])

# subplot(3,5,15)
ax[2, 4].plot([x1, x2], [y1, y2], ".")
ax[2, 4].annotate("", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
                  arrowprops = dict(arrowstyle = "->", color = "gray", lw = 3, 
                                    shrinkA = 5, shrinkB = 5, patchA = None, patchB = None, 
                                    connectionstyle = "bar, armB = 60, angle = 90, fraction = 0.4"))
ax[2, 4].text(0.05, 0.95, "bar, \narmB = 60, \nangle = 90, \nfraction = 0.4",
              ha = "left", va = "top", bbox = bbox)
ax[2, 4].set_xlim(0, 1)
ax[2, 4].set_ylim(0, 1)
ax[2, 4].set_xticklabels([])
ax[2, 4].set_yticklabels([])

fig.subplots_adjust(left = 0.05, right = 0.95, bottom = 0.05, top = 0.95, wspace = 0.02, hspace = 0.02)

 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
fig, ax = plt.subplots(1, 3, figsize = (12, 6), sharex = True, sharey = True) 
x1, y1 = 0.3, 0.3 
x2, y2 = 0.7, 0.7 
bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k") 
 
# subplot(131)
ax[0].annotate("simple", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
               bbox = dict(boxstyle = "round", fc = "w", ec = "k"),
               arrowprops = dict(arrowstyle = "simple, head_length = 0.7, head_width = 0.6, tail_width = 0.3",
                                 color = "gray", shrinkA = 5, shrinkB = 5, patchB = None, 
                                 connectionstyle = "angle3, angleA = 0, angleB = 90"), 
               size = 25, ha = "center", va = "center") 
ax[0].text(0.05, 0.95, "angle3, \nangleA = 0, \nangleB = 90", ha = "left", va = "top", bbox = bbox, size = 20) 
ax[0].set_xlim(0, 1) 
ax[0].set_ylim(0, 1) 
ax[0].set_xticklabels([]) 
ax[0].set_yticklabels([]) 

# subplot(132) 
ax[1].annotate("fancy", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
               bbox = dict(boxstyle = "round", fc = "w", ec = "k"), 
               arrowprops = dict(arrowstyle = "fancy, head_length = 0.4, head_width = 0.4, tail_width = 0.6", 
                                 color = "gray", shrinkA = 5, shrinkB = 5, patchB = None, 
                                 connectionstyle = "arc3, rad = 0.5"), 
               size = 25, ha = "center", va = "center") 
ax[1].text(0.05, 0.95, "arc3, \nrad = 0.5", ha = "left",va = "top", bbox = bbox, size = 20) 
ax[1].set_xlim(0, 1) 
ax[1].set_ylim(0, 1) 
ax[1].set_xticklabels([]) 
ax[1].set_yticklabels([]) 

# subplot(133) 
ax[2].annotate("wedge", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
               bbox = dict(boxstyle = "round", fc = "w", ec = "k"),  
               arrowprops = dict(arrowstyle = "wedge, tail_width = 0.5", color = "gray", 
                            shrinkA = 5, shrinkB = 5, patchB = None, 
                            connectionstyle = "arc3, rad = -0.3"), 
               size = 25, ha = "center", va = "center") 
ax[2].text(0.05, 0.95, "arc3, \nrad = -0.3", ha = "left", va = "top", bbox = bbox, size = 20) 
ax[2].set_xlim(0, 1) 
ax[2].set_ylim(0, 1) 
ax[2].set_xticklabels([]) 
ax[2].set_yticklabels([]) 

fig.subplots_adjust(left = 0.05, right = 0.95, bottom = 0.05, top = 0.95, wspace = 0.02, hspace = 0.02)

 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
fig,ax = plt.subplots(1, 3, figsize = (12, 6), sharex = True, sharey = True) 
x1, y1 = 0.3, 0.3 
x2, y2 = 0.7, 0.7 
bbox = dict(boxstyle = "square", facecolor = "w", edgecolor = "k") 
 
# subplot(131) 
ax[0].annotate("relpos = (0.0, 0.0)", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
               bbox = dict(boxstyle = "round4", fc = "w", ec = "k"), 
               arrowprops = dict(arrowstyle = "-|>", color = "k", relpos = (0.0, 0.0), 
                                 connectionstyle = "arc3, rad = 0.3"), 
               size = 15, ha = "center", va = "center") 
ax[0].text(0.05, 0.95, "arc3, \nrad = 0.3", ha = "left", va = "top", bbox = bbox, size = 20) 
ax[0].set_xlim(0, 1) 
ax[0].set_ylim(0, 1) 
ax[0].set_xticklabels([]) 
ax[0].set_yticklabels([]) 

# subplot(132) 
ax[1].annotate("relpos = (1.0, 0.0)", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
               bbox = dict(boxstyle = "round4", fc = "w", ec="k"), 
               arrowprops = dict(arrowstyle = "-|>", color = "k", relpos = (1.0, 0.0), 
                                 connectionstyle = "arc3, rad = -0.3"), 
               size = 15, ha = "center", va = "center") 
ax[1].text(0.05, 0.95, "arc3, \nrad = -0.3", ha = "left", va = "top", bbox = bbox, size = 20) 
ax[1].set_xlim(0, 1) 
ax[1].set_ylim(0, 1) 
ax[1].set_xticklabels([]) 
ax[1].set_yticklabels([]) 
 
# subplot(133) 
ax[2].annotate("relpos = (0.2, 0.8)", xy = (x1, y1), xycoords = "data", xytext = (x2, y2), textcoords = "data", 
               bbox = dict(boxstyle = "round4", fc = "w", ec = "k"), 
               arrowprops = dict(arrowstyle = "-|>", color = "k", relpos = (0.3, 0.7), 
                                 connectionstyle = "arc3, rad = -0.3"), 
               size=15, ha = "center", va = "center") 
ax[2].text(0.05, 0.95, "arc3, \nrad = -0.3", ha = "left", va = "top", bbox = bbox, size = 20) 
ax[2].set_xlim(0, 1) 
ax[2].set_ylim(0, 1) 
ax[2].set_xticklabels([]) 
ax[2].set_yticklabels([]) 
fig.subplots_adjust(left = 0.05, right = 0.95, bottom = 0.05, top = 0.95, wspace = 0.02, hspace = 0.02) 

计量单位方法

不同剂量单位的实现方法

弧度和角度的实现方法

 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
from basic_units import radians, degrees, cos

x = np.linspace(0, 9.5, 500)
rad_x = [i*radians for i in x]

fig, ax = plt.subplots(2, 1)

ax[0].plot(rad_x, cos(rad_x), ls = '-', lw = 3, color = 'k', xunits = radians)
ax[0].set_xlabel('')

ax[1].plot(rad_x, cos(rad_x), ls = '--', lw = 3, color = 'cornflowerblue', xunits = degrees)
ax[1].set_xlabel('')

fig.subplots_adjust(hspace = 0.3)
plt.show()

 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
from basic_units import cm, inch

x = np.linspace(0, 10, 6)
cm_x = [i*cm for i in x]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(cm_x, cm_x, ls = '-', lw = 3, color = 'k', xunits = cm, yunits = cm)
ax[0, 0].set_ylabel("")
ax[0, 0].set_xlabel("")

ax[0, 1].plot(cm_x, cm_x, ls = '--', lw = 3, color = 'cornflowerblue', xunits = cm, yunits = inch)
ax[0, 1].set_ylabel("")
ax[0, 1].set_xlabel("")
ax[0, 1].set_xlim(2, 8)

ax[1, 0].plot(cm_x, cm_x, ls = "-.", lw = 3, color = "gold", xunits = inch, yunits = cm)
ax[1, 0].set_ylabel("")
ax[1, 0].set_xlabel("")
ax[1, 0].set_xlim(2*cm, 8*cm)

ax[1, 1].plot(cm_x, cm_x, ls = ":", lw = 3, color = "purple", xunits = inch, yunits = inch)
ax[1, 1].set_ylabel("")
ax[1, 1].set_xlabel("")

plt.show()

秒、赫兹和分钟的实现方法

 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
from basic_units import secs, minutes, hertz

t = [2, 4, 3, 5, 8, 6, 7, 9]
secs_t = [time*secs for time in t]

fig, ax = plt.subplots(3, 1, sharex = True)

ax[0].scatter(secs_t, secs_t, s = 10*np.max(t), c = 'steelblue', marker = 'o')
ax[0].set_xlabel('')
ax[0].set_ylabel('')

ax[1].scatter(secs_t, secs_t, s = 10*np.max(t), c = 'gold', marker = 'D', yunits = hertz)
ax[1].set_xlabel('')
ax[1].set_ylabel('')
ax[1].axis([1, 10, 0, 1])

ax[2].scatter(secs_t, secs_t, s = 10*np.max(t), c = 'brown', marker = '^', yunits = hertz)
ax[2].yaxis.set_units(minutes)
ax[2].set_xlabel('')
ax[2].set_ylabel('')
ax[2].axis([1, 10, 0, 1])

fig.subplots_adjust(hspace = 0.2)

plt.show()

文本注释位置的坐标系统的设置方法

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

fig, ax = plt.subplots(1, 1)
font_style = {'family':'monospace', 'fontsize':15, 'weight':'bold'}
ellipse = Ellipse((2*cm, 1*cm), 0.05*cm, 0.05*cm, color = 'blue')
ax.add_patch(ellipse)

ax.annotate('fancy', xy = (2*cm, 1*cm), xycoords = 'data', xytext = (0.8*cm, 0.85*cm), textcoords = 'data',
            bbox = dict(boxstyle = 'round', fc = 'w', ec = 'k'),
            arrowprops = dict(arrowstyle = 'fancy, head_length = 0.4, head_width = 0.4, tail_width = 0.6', fc = 'gray',
                              connectionstyle = 'arc3, rad = 0.3', shrinkA = 5, patchB = ellipse, shrinkB = 5),
            ha = 'right', va = 'top', **font_style)

ax.annotate('fancy', xy = (2*cm, 1*cm), xycoords = 'data', xytext = (0.8, 0.85), textcoords = 'axes fraction',
            bbox = dict(boxstyle = 'round', fc = 'w', ec = 'k'),
            arrowprops = dict(arrowstyle = 'fancy, head_length = 0.4, head_width = 0.4, tail_width = 0.6', fc = 'gray',
                              connectionstyle = 'arc3, rad = 0.3', shrinkA = 5, patchB = ellipse, shrinkB = 5),
            ha = 'right', va = 'top', **font_style)

ax.set_xlim(0*cm, 3*cm)
ax.set_ylim(0*cm, 3*cm)
plt.show()

不同计量方法的操作原理

 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
x = np.linspace(1, 10, 1000)
y1 = [2**j for j in x]
y2 = [0.09*j for j in x]

fig, ax = plt.subplots(2, 2)

# linear
ax[0, 0].plot(x, y1)
ax[0, 0].set_yscale('linear')
ax[0, 0].set_title('linear')
ax[0, 0].grid(True, ls = '-', lw = 1, color = 'gray')

# log
ax[0, 1].plot(x, y1)
ax[0, 1].set_yscale('log')
ax[0, 1].set_title('log')
ax[0, 1].grid(True, ls = '-', lw = 1, color = 'gray')

# logit
ax[1, 0].plot(x, y2)
ax[1, 0].set_yscale('logit')
ax[1, 0].set_title('logit')
ax[1, 0].grid(True, ls = '-', lw = 1, color = 'gray')
ax[1, 0].set_ylim(0.1, 0.9)

# symlog
ax[1, 1].plot(x, y2-np.average(y2))
ax[1, 1].set_yscale('symlog', linthresh = 0.02)
ax[1, 1].set_title('symlog')
ax[1, 1].grid(True, ls = '-', lw = 1, color = 'gray')

fig.subplots_adjust(hspace = 0.5, wspace = 0.3)

plt.show()

刻度和轴脊

刻度线和刻度标签及轴标签的位置调整

1
2
import matplotlib.pyplot as plt
import numpy as np
 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
fig = plt.figure(1, figsize = (8, 8), dpi = 80, facecolor = 'w')
fontsize = 1.5*0.1*fig.dpi
font_style = {'family':'sans-serif', 'fontsize':fontsize, 'weight':'black'}

# xaxis separate tick and ticklabel
ax0_0 = fig.add_subplot(2, 2, 1, fc = 'yellowgreen', alpha = 0.1)
ax0_0.xaxis.set_ticks_position('top')
ax0_0.xaxis.set_label_position('top')
ax0_0.set_xlabel('separate tick and ticklabel', **font_style)

# xaxis universal tick and ticklabel
ax0_1 = fig.add_subplot(2, 2, 2, fc = 'yellowgreen', alpha = 0.1)
ax0_1.xaxis.tick_top()
ax0_1.xaxis.set_label_position('top')
ax0_1.set_xlabel('universal tick and ticklabel', **font_style)

# yaxis separate tick and ticklabel
ax1_0 = fig.add_subplot(2, 2, 3, fc = 'yellowgreen', alpha = 0.1)
ax1_0.yaxis.set_ticks_position('right')
ax1_0.yaxis.set_label_position('right')
ax1_0.set_ylabel('separate tick and ticklabel', **font_style)

# yaxis universal tick and ticklabel
ax1_1 = fig.add_subplot(2, 2, 4, fc = 'yellowgreen', alpha = 0.1)
ax1_1.yaxis.tick_right()
ax1_1.yaxis.set_label_position('right')
ax1_1.set_ylabel('universal tick and ticklabel', **font_style)

fig.subplots_adjust(wspace = 0.3)
plt.show()

刻度线的位置和数值的动态调整

 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
from matplotlib.ticker import FuncFormatter, MaxNLocator

fig, ax = plt.subplots()

ticklabels = 'stage1 stage2 stage3 stage4 stage5'
ticklabels_list = ticklabels.split(" ")

x = np.linspace(0, len(ticklabels_list)-1, len(ticklabels_list))
y = np.exp(-x)

ax.plot(x, y, lw = 3, color = 'steelblue', marker = 's', mfc = 'r', mec = 'g')

def tick_controller(value, position):
    if int(value) in x:
        return ticklabels_list[int(value)]
    else:
        return " "

ax.xaxis.set_major_formatter(FuncFormatter(tick_controller))  # 设置主刻度线所在位置的数值
ax.xaxis.set_major_locator(MaxNLocator(integer = True))  # 设置主刻度线所在位置

xticklabel_text = ax.get_xticklabels()
for i, j in enumerate(xticklabel_text):
    j.set_family('monospace')
    j.set_fontsize(12)
    j.set_weight('bold')
    j.set_rotation(30)

ax.margins(0.25)
plt.show()

主要刻度线和次要刻度线的调整

 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
from matplotlib.ticker import MultipleLocator, FormatStrFormatter, AutoMinorLocator, NullFormatter, FixedLocator

fig, ax = plt.subplots(3, 1)

# subplot(3, 1)
majorLocator = MultipleLocator(1.5)
majorFormatter = FormatStrFormatter('%1.1f')
minorLocator = MultipleLocator(0.5)
minorFormatter = NullFormatter()

x = np.linspace(0, 2*np.pi, 500)
y = np.cos(2*np.pi*x)*np.exp(-x)

ax[0].plot(x, y, lw = 3, color = 'cornflowerblue')
ax[0].xaxis.set_major_locator(majorLocator)
ax[0].xaxis.set_major_formatter(majorFormatter)

ax[0].xaxis.set_minor_locator(minorLocator)
ax[0].xaxis.set_minor_formatter(minorFormatter)

# subplot(3, 1, 2)
minorLocator = AutoMinorLocator()

ax[1].plot(x, y, lw = 3, color = 'cornflowerblue')
ax[1].xaxis.set_minor_locator(minorLocator)

ax[1].tick_params(axis = 'x', which = 'major', length = 6, width = 1.5)
ax[1].tick_params(axis = 'x', which = 'minor', length = 4, width = 1, color = 'r')

# subplot(3, 1, 3)
majorLocator = FixedLocator([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi])
minorLocator = AutoMinorLocator(5)

ax[2].plot(x, y, lw = 3, color = 'cornflowerblue')
ax[2].xaxis.set_major_locator(majorLocator)
ax[2].xaxis.set_minor_locator(minorLocator)
ax[2].tick_params(which = 'major', length = 6, width = 1.5)
ax[2].tick_params(which = 'minor', length = 4, width = 1, color = 'r')
ax[2].set_xticklabels(["0", r"$\pi/2$", r"$\pi$", r"$3\pi/2$", r"$2\pi$"])

fig.subplots_adjust(hspace = 0.4)
plt.show()

轴脊的显示与隐藏

 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
x = np.linspace(0, 2*np.pi, 500)
y = 1.85*np.sin(x)

fig, ax = plt.subplots(3, 1)

# subplot(3, 1, 1)
ax[0].plot(x, y, lw = 3, color = 'dodgerblue')
ax[0].set_ylim(-2, 2)
ax[0].set_facecolor('lemonchiffon')

# subplot(3, 1, 2)
ax[1].plot(x, y, lw = 3, color = 'dodgerblue')
ax[1].spines['right'].set_visible(False)
ax[1].spines['top'].set_visible(False)
ax[1].xaxis.set_ticks_position('bottom')
ax[1].yaxis.set_ticks_position('left')
ax[1].set_ylim(-3, 3)
ax[1].set_facecolor('lemonchiffon')

# subplot(3, 1, 3)
ax[2].plot(x, y, lw = 3, color = 'dodgerblue')
ax[2].spines['right'].set_color('none')
ax[2].spines['top'].set_color('none')
ax[2].yaxis.tick_left()
ax[2].xaxis.tick_bottom()
ax[2].spines['left'].set_bounds(-1, 1)
ax[2].spines['bottom'].set_bounds(0, 2*np.pi)
ax[2].set_ylim(-2, 2)
ax[2].set_facecolor('lemonchiffon')

fig.subplots_adjust(hspace = 0.4)
plt.show()

轴脊位置的调整

 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
x = np.linspace(0, 2, 1000)
y = 0.9*np.sin(np.pi*x)

fig, ax = plt.subplots(2, 3)

# subplot(2, 3, 1)
ax[0, 0].plot(x, y, lw = 3, color = 'steelblue')
ax[0, 0].spines['right'].set_visible(False)
ax[0, 0].spines['top'].set_visible(False)
# set left and bottom spines position
ax[0, 0].spines['left'].set_position(('data', 0.5))
ax[0, 0].spines['bottom'].set_position(('data', 1))
# set tickline position of bottom adn left spines
ax[0, 0].xaxis.set_ticks_position('bottom')
ax[0, 0].yaxis.set_ticks_position('left')
ax[0, 0].set_ylim(-1, 1)
ax[0, 0].set_facecolor('lemonchiffon')

# subplot (2, 3, 4)
ax[1, 0].plot(x, y, lw = 3, color = 'steelblue')
ax[1, 0].spines['right'].set_color('none')
ax[1, 0].spines['top'].set_color('none')
# set left and bottom spines position
ax[1, 0].spines['left'].set_position('zero')
ax[1, 0].spines['bottom'].set_position('zero')
# set tickline position of bottom adn left spines
ax[1, 0].xaxis.tick_bottom()
ax[1, 0].yaxis.tick_left()
ax[1, 0].set_ylim(-1, 1)
ax[1, 0].set_facecolor('lemonchiffon')

# subplot(2, 3, 2)
ax[0, 1].plot(x, y, lw = 3, color = 'steelblue')
ax[0, 1].spines['right'].set_visible(False)
ax[0, 1].spines['top'].set_visible(False)
# set left and bottom spines position
ax[0, 1].spines['left'].set_position(('axes', 0.25))
ax[0, 1].spines['bottom'].set_position(('axes', 0.75))
ax[0, 1].xaxis.set_ticks_position('bottom')
ax[0, 1].yaxis.set_ticks_position('left')
ax[0, 1].set_ylim(-1, 1)
ax[0, 1].set_facecolor('lemonchiffon')

# subplot(2, 3, 5)
ax[1, 1].plot(x, y, lw = 3, color = 'steelblue')
ax[1, 1].spines['right'].set_color('none')
ax[1, 1].spines['top'].set_color('none')
# set left and bottom spines position
ax[1, 1].spines['left'].set_position('center')
ax[1, 1].spines['bottom'].set_position('center')
# set tickline position of bottom adn left spines
ax[1, 1].xaxis.tick_bottom()
ax[1, 1].yaxis.tick_left()
ax[1, 1].set_ylim(-1, 1)
ax[1, 1].set_facecolor('lemonchiffon')

# subplot(2, 3, 3)
ax[0, 2].plot(x, y, lw = 3, color = 'steelblue')
ax[0, 2].spines['right'].set_visible(False)
ax[0, 2].spines['top'].set_visible(False)
# set left and bottom spines position
ax[0, 2].spines['left'].set_position(('outward', 3))
ax[0, 2].spines['bottom'].set_position(('outward', 2))
ax[0, 2].xaxis.set_ticks_position('bottom')
ax[0, 2].yaxis.set_ticks_position('left')
ax[0, 2].set_ylim(-1, 1)
ax[0, 2].set_facecolor('lemonchiffon')

# subplot(2, 3, 6)
ax[1, 2].plot(x, y, lw = 3, color = 'steelblue')
ax[1, 2].spines['right'].set_color('none')
ax[1, 2].spines['top'].set_color('none')
# set left and bottom spines position
ax[1, 2].spines['left'].set_position(('outward', -3))
ax[1, 2].spines['bottom'].set_position(('outward', -2))
# set tickline position of bottom adn left spines
ax[1, 2].xaxis.tick_bottom()
ax[1, 2].yaxis.tick_left()
ax[1, 2].set_ylim(-1, 1)
ax[1, 2].set_facecolor('lemonchiffon')

fig.subplots_adjust(wspace = 0.35, hspace = 0.2)
plt.show()

动画

使用模块animation绘制动画

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

fig, ax = plt.subplots(1, 1)

x = np.linspace(0, 2*np.pi, 5000)
y = np.exp(-x)*np.cos(2*np.pi*x)
line, = ax.plot(x, y, color = 'cornflowerblue', lw = 3)
ax.set_ylim(-1.0, 1.0)

# to clear current frame
def init():
    line.set_ydata([np.nan]*len(x))
    return line,

# to update the data
def animate(data):
    line.set_ydata(np.exp(-x)*np.cos(2*np.pi*x+float(data)/100))
    return line,

# to call calsss FuncAnimation which connects animate and init
ani = FuncAnimation(fig, animate, init_func = init, frames = 200, interval = 2, blit = True)

# to save the animation
# ani.save('./mymovie.mp4', fps = 20, writer = 'ffmpeg')
ani.save('./ch07_1.gif', fps = 20, writer = 'pillow')

调用模块pyplot的API绘制动画

 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
47
48
49
50
51
52
53
54
55
from matplotlib.patches import Circle
from warnings import filterwarnings

%matplotlib qt
# ignore warning
filterwarnings('ignore', '.*GUI is implemented')

# set several variables
word = 'kaleidoscope'
row = int(len(word)/4)
col = int(len(word)/4)
num = int(len(word)/4)

data = np.random.random((row, col, num))
colorMap = ['spring', 'summer', 'autumn', 'winter']

subplot_row = int(len(word)/6)
subplot_col = int(len(word)/6)

font = dict(family = 'monospace', weight = 'bold', style = 'italic', fontsize = 10)
subplot_kw = dict(aspect = 'equal', frame_on = False, xticks = [], yticks = [])

# create subplots
fig, ax = plt.subplots(subplot_row, subplot_col, subplot_kw = subplot_kw)

# generate a subplot
def rowcolgenerator(r, c, season):
    index = colorMap.index(season)
    t = index*num
    subtitle = "No.{} '{}' Theme of the {}"
    for j in range(len(data)):
        ax[r, c].cla()
        collection = ax[r, c].pcolor(data[j, :], cmap = colorMap[index])
        patch = Circle((1.5, 1.5), radius = 1.5, transform = ax[r, c].transData)
        collection.set_clip_path(patch)
        element = colorMap[index].capitalize()
        ax[r, c].set_title(subtitle.format((j+1), word[t:t+3], element), font)
        ax[r, c].set_axis_off()
        plt.pause(0.15)

# create animation
def animation():
    i = 0
    for r in range(subplot_row):
        for c in range(subplot_col):
            rowcolgenerator(r, c, colorMap[i])
            i += 1

    title = 'Life Kaleidoscope Consists of Four Seasons'
    plt.suptitle(title, family = 'serif', weight = 'black', fontsize = 20)

    plt.subplots_adjust(wspace = 0.05, hspace = 0.2)

if __name__ == "__main__":
    animation()

GUI效果

类RadioButtons的使用方法

 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
47
48
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons

%matplotlib auto

x = np.linspace(0.0, 2.0, 1000)
y1 = 1.5*np.cos(2*np.pi*x)
y2 = 1.0*np.cos(2*np.pi*x)
y3 = 0.8*np.cos(2*np.pi*x)

fig, ax = plt.subplots(1, 1)
line, = ax.plot(x, y1, color = 'red', lw = 2)
plt.subplots_adjust(left = 0.35)

axesbgcolor = 'cornflowerblue'

# a set of radionbuttons about amplitude
axl = plt.axes([0.1, 0.7, 0.15, 0.15], fc = axesbgcolor)
radio1 = RadioButtons(axl, ('1.5 A', '1.0 A', '0.8 A'))

def amplitudefunc(label):
    hzdict = {'1.5 A':y1, '1.0 A':y2, '0.8 A':y3}
    ydata = hzdict[label]
    line.set_ydata(ydata)
    plt.draw()

radio1.on_clicked(amplitudefunc)

# a set of radiobuttons about color
ax2 = plt.axes([0.1,0.4,0.15,0.15],fc=axesbgcolor)
radio2 = RadioButtons(ax2,("red","green","orange"))

def colorfunc(label):
    line.set_color(label)
    plt.draw()
    
radio2.on_clicked(colorfunc)

# a set of radionbuttons about linestyle
ax3 = plt.axes([0.1,0.1,0.15,0.15],fc=axesbgcolor)
radio3 = RadioButtons(ax3,("-","--","-.",":"))
def linestylefunc(label):
    line.set_linestyle(label)
    plt.draw()
radio3.on_clicked(linestylefunc)

plt.show()
Using matplotlib backend: Qt5Agg

类Cursor的使用方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from matplotlib.widgets import Cursor

lineprops=dict(color="red",lw=2)

fig,ax = plt.subplots(1,1,subplot_kw=dict(fc="lemonchiffon"))

x = np.random.random(100)
y = np.random.random(100)
ax.scatter(x,y,marker="o")
ax.set_xlim(-0.02,1.02)
ax.set_ylim(-0.02,1.02)

cursor = Cursor(ax,useblit=True,**lineprops)

plt.show()

类 CheckButtons 的使用方法

 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
from matplotlib.widgets import CheckButtons

x = np.linspace(0.0,2.0,1000)
y1 = 1.2*np.cos(2*np.pi*x)
y2 = 1.0*np.cos(2*np.pi*x)
y3 = 0.8*np.cos(2*np.pi*x)

fig,ax = plt.subplots(1,1)
line1, = ax.plot(x,y1,color="red",lw=2,visible=False,label="1.2 A")
line2, = ax.plot(x,y2,color="green",lw=2,label="1.0 A")
line3, = ax.plot(x,y3,color="orange",lw=2,label="0.8 A")
plt.subplots_adjust(left=0.30)

axesbgcolor = "cornflowerblue"
cax = plt.axes([0.1,0.4,0.1,0.15],fc=axesbgcolor)

lines = [line1,line2,line3]
labels = [str(line.get_label()) for line in lines]
visibility = [line.get_visible() for line in lines]
check = CheckButtons(cax,labels,visibility)

def func(label):
    index = labels.index(label)
    lines[index].set_visible(not lines[index].get_visible())
    plt.draw()
    
check.on_clicked(func)
plt.show()

事件处理效果

单击关闭画布后出现事件结果提示

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

%matplotlib auto

def handle_event(close):
    print("Handling Event: Closed Figure!")
    
font_style = dict(family="serif",weight="black",size=40)

fig = plt.figure()
fig.canvas.mpl_connect("close_event", handle_event)

plt.text(0.15, 0.5,"close_event",**font_style)
Using matplotlib backend: Qt5Agg





Text(0.15, 0.5, 'close_event')

画布局部放大效果的实现方法

 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
import numpy as np

fig1,ax1 = plt.subplots(1,1)
fig2,ax2 = plt.subplots(1,1)

ax1.set_xlim(0,1)
ax1.set_ylim(0,1)
ax1.set_autoscale_on(False)
ax1.set_title("Click to zoom")

ax2.set_xlim(0.0,0.4)
ax2.set_ylim(0.0,0.4)
ax2.set_autoscale_on(False)
ax2.set_title("Zoom window")

x = np.random.rand(100)
y = np.random.rand(100)
s = np.random.rand(100)*100
c = np.random.rand(100)

ax1.scatter(x,y,s,c)
ax2.scatter(x,y,s,c)

def clicktozoom(event):
    if event.button != 1:
        return
    x,y = event.xdata,event.ydata
    ax2.set_xlim(x-0.15,x+0.15)
    ax2.set_ylim(y-0.15,y+0.15)
    fig2.canvas.draw()
    
fig1.canvas.mpl_connect("button_press_event", clicktozoom)

plt.show()

导入图像

外部图像的多样化展示

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import matplotlib.pyplot as plt
from matplotlib.cbook import get_sample_data
from matplotlib.patches import Circle

imageArray = plt.imread("./Ch10_sunflower.png")
fig, ax = plt.subplots(1,2, figsize=(12,6))
ax[0].imshow(imageArray)
ax[0].set_axis_off()
ai = ax[1].imshow(imageArray)
patch = Circle((605, 360),radius=350, transform=ax[1].transData)
ai.set_clip_path(patch)
ax[1].set_axis_off()

地势图

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

fontstyle = dict(fontsize=25,weight="bold",family="serif")
filePath = get_sample_data("jacksboro_fault_dem.npz",asfileobj=False)

with np.load(filePath) as jfdem:
    elev = jfdem["elevation"]
    
fig,ax = plt.subplots(1,2)
ls = LightSource(azdeg=315,altdeg=45)
ai1 = ax[0].imshow(elev,cmap=plt.cm.gist_earth)
fig.colorbar(ai1,ax=ax[0],orientation="horizontal")

rgba = ls.shade(elev,cmap=plt.cm.gist_earth,vert_exag=0.05,blend_mode="soft")
ai2 = ax[1].imshow(rgba)
fig.colorbar(ai1,ax=ax[1],orientation="horizontal")

fig.suptitle("shaded relief plot blending with 'soft'",y=0.92,**fontstyle)
plt.show()

热力图

 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
golfer_stats = ["GIR","Scrambling","Bounce Back","Ball Striking","Sand Saves","Birdie Conversion"]
golfer_names = ["Golfer %d" % i for i in range(1,7)]

# we use the normalized percentages.
golfer_percentages = np.random.randn(6,6)
shape = golfer_percentages.shape

fig,ax = plt.subplots()
im = ax.imshow(golfer_percentages,cmap="Greens")
colorbar = fig.colorbar(im,ax=ax)
colorbar.set_label("Golfer normalized percentages",rotation=-90,va="bottom")
ax.set_xticks(np.arange(0,shape[1],1))
ax.set_yticks(np.arange(0,shape[0],1))
ax.set_xticklabels(golfer_names)
ax.set_yticklabels(golfer_stats)

# add text to each area of heatmap
for i in range(len(golfer_stats)):
    for j in range(len(golfer_names)):
        text = ax.text(i,j,round(golfer_percentages[j,i],1),ha="center",va="center",color="w")

        # turn tick line off and set tick label position
ax.tick_params(direction="out",bottom=False,right=False,labeltop=True,labelbottom=False)        
        
# set tick label format
plt.setp(ax.get_xticklabels(),rotation=-30,ha="right",rotation_mode="anchor")

# turn spines off
spinesTupleList = list(ax.spines.items())
for i,each in enumerate(spinesTupleList):
    spine = each[1]
    spine.set_visible(False)

# set minor tick line position and create white grid
ax.set_xticks(np.arange(0.5,shape[1]-1,1),minor=True)
ax.set_yticks(np.arange(0.5,shape[0]-1,1),minor=True)
ax.grid(which="minor",color="w",linestyle="-",linewidth=3)

# turn minor tick line off
ax.tick_params(which="minor",top=False,bottom=False,left=False,right=False)

fig.tight_layout()

设置图片具有超链接功能

 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
from matplotlib.offsetbox import AnnotationBbox,OffsetImage,TextArea

fig = plt.figure()
# set background of picture in the axes
ax0 = plt.axes([0.0,0.0,1.0,1.0],frameon=True,aspect="equal")
backgroundData = plt.imread("./Ch10_map.jpg")
im0 = ax0.imshow(backgroundData)
ax0.set_axis_off()

# set point links with urls
pc = plt.scatter([351,823],[343,163],c="r",edgecolors="r",s=50,alpha=0.8)
pc.set_urls(["https://www.trip.com","https://www.wunderground.com"])

# add arrow to the background of picture
ax0.annotate("",xy=(351,343),xytext=(823,163),xycoords="data",textcoords="data",
             arrowprops=dict(arrowstyle="fancy,head_length=0.6,head_width=0.6,tail_width=0.5",
                             shrinkA=10,shrinkB=10,connectionstyle="arc3,rad=0.3",color="b"))

#annotate 1st position with a image box
imageData = plt.imread("./Ch10_das-Auto.png")
imagebox = OffsetImage(imageData,zoom=0.035)

ab_image = AnnotationBbox(imagebox,xy=(351,343),xybox=(-50,40),xycoords="data",
                          boxcoords="offset points",pad=0.05,frameon=True,
                          arrowprops=dict(arrowstyle="-",shrinkA=0,shrinkB=5,relpos=(1.0, 0.0)))
ax0.add_artist(ab_image)

# annotate 2rd position with a linked image
ax1 = plt.axes([0.63,0.8,0.1,0.1],frameon=True,aspect="equal")
imageData = plt.imread("./Ch10_pilot.png")
im = ax1.imshow(imageData,url="https://www.guankui.name")
#im.set_url("https://www.lufthansa.com")
ax1.set_axis_off()

# annotate 2rd position with a text box
textprops=dict(fontsize=10,weight="bold",color="b")
textbox = TextArea("TRAVEL",textprops=textprops)

ab_text = AnnotationBbox(textbox,xy=(823,163),xybox=(-40,52),xycoords="data",boxcoords="offset points",
                         pad=0.2,bboxprops=dict(facecolor="gray",alpha=0.5),
                         arrowprops=dict(arrowstyle="-",shrinkA=0,shrinkB=5,relpos=(0.5, 0.0)))
ax0.add_artist(ab_text)

fig.savefig("./Ch10_hyperlink_image.svg")

添加画布层面的外部图像

 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
# create a new figure
fig = plt.figure()

# figure picture
# use "r" to avoid escape sequence \f
imageData1 = plt.imread(r"./Ch10_captain-bird.png")
# add an image to the figure
fig.figimage(imageData1,200,100,origin="upper",alpha=0.05,resize=True,zorder=1)

# axes pictrue
imageData2 = plt.imread(r"./Ch10_treasure-map.png")
# display an image i.e. data on a 2D raster
plt.imshow(imageData2,alpha=1.0)
plt.axis("off")

# set several points
plt.scatter([348,445,657],[387,523,415],c="black",edgecolor="w",s=50)

bboxs=dict(facecolor="navy",edgecolor="navy",alpha=0.8)
plt.text(299,369,r"#1$\ \Re\Game\Im$",fontsize=15,color="w",bbox=bboxs)
plt.text(404,504,r"#2$\ \ss\ell\wp$",fontsize=15,color="w",bbox=bboxs)
plt.text(614,399,r"#3$\ \hslash\imath\jmath$",fontsize=15,color="w",bbox=bboxs)

plt.annotate("where to go...",xy=(552,363),xycoords="data",xytext=(555,308),textcoords="data",
             weight="black",color="#000000",arrowprops=dict(arrowstyle="<|-",relpos=(0.2,0.0)))

# set a diagram describing the points
diagramContent = r"#1$\ \ $"+r"$\bigstar$"*3+"\n"+\
                   r"#2$\ \ $"+r"$\bigstar$"*4+"\n"+\
                   r"#3$\ \ $"+r"$\bigstar$"*1+"\n"+\
                   r"#?$\ \ $"+"???"
bbox={"boxstyle":"round","facecolor":"#F3F0ED","edgecolor":"#453B34","linewidth":2,"linestyle":"--","alpha":0.8}
plt.text(688,162,diagramContent,fontsize=25,color="#453B34",rotation=-5,bbox=bbox)
plt.text(663,62,"Potential Treasure",fontsize=20,color="k",weight="bold",rotation=-5)

# save a SVG file
fig.savefig(r".\Ch10_figure_image.svg")

颜色的翻转

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import scipy.misc as msc

fig,ax = plt.subplots(1,2)
font = dict(family="serif",weight="bold")
ascent = msc.ascent()
inverted_ascent = 255 - ascent

# show source image
ax[0].imshow(ascent)
ax[0].set_title("source_image",**font)
ax[0].set_axis_off()

# show inverted image
ax[1].imshow(inverted_ascent)
ax[1].set_title("inverted_image",**font)
ax[1].set_axis_off()
plt.show()

RGB 通道 NumPy 数组转换成单通道 NumPy 数组

 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
fig,ax = plt.subplots(2,2)

font = dict(family="monospace",weight="bold")

# get the array of RGB channels
outfile = plt.imread(r"./Ch10_tree_image.jpg")
print("the array 'outfile' shape: {}".format(outfile.shape))

# get the arrays of the red, green and blue channels
rCh = outfile[:,:,0]
print("the array 'rCh' shape: {}".format(rCh.shape))
gCh = outfile[:,:,1]
print("the array 'gCh' shape: {}".format(gCh.shape))
bCh = outfile[:,:,2]
print("the array 'bCh' shape: {}".format(bCh.shape))

# show source image
ax[0,0].imshow(outfile)
ax[0,0].set_title("source_image",**font)
ax[0,0].set_axis_off()

# show the images of the red, green and blue channels
ax[0,1].imshow(rCh)
ax[0,1].set_title("rCh_image",**font)
ax[0,1].set_axis_off()

ax[1,0].imshow(gCh)
ax[1,0].set_title("gCh_image",**font)
ax[1,0].set_axis_off()

ax[1,1].imshow(bCh)
ax[1,1].set_title("bCh_image",**font)
ax[1,1].set_axis_off()
the array 'outfile' shape: (1273, 1920, 3)
the array 'rCh' shape: (1273, 1920)
the array 'gCh' shape: (1273, 1920)
the array 'bCh' shape: (1273, 1920)

3D图形

绘制带颜色标尺的彩色曲面

 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 numpy as np
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from mpl_toolkits.mplot3d import axes3d

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection = '3d')

x = np.arange(-3, 3, 0.25)
y = np.arange(-3, 3, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(np.power(x, 2) + np.power(y, 2))
z = np.sin(r)

# plot 3d surface
surf = ax.plot_surface(x, y, z, rstride = 1, cstride = 1, cmap = cm.coolwarm, linewidth = 0, antialiased = False)

# customize the z axis
ax.set(zlim = (-1, 1))
ax.zaxis.set_major_locator(LinearLocator(7))
ax.zaxis.set_major_formatter(FormatStrFormatter('%3.2f'))

# add a color bar mapping values to colors
fig.colorbar(surf, shrink = 0.6, aspect = 10, )
plt.show()

在3D空间里分层展示投射到指定平面后的2D柱状图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection = '3d')

colorsList = ['r', 'b', 'y']
yLayersList = [2, 1, 0]

for color, layer in zip(colorsList, yLayersList):
    x = np.arange(10)
    y = np.random.rand(10)
    ax.bar(x, y, zs = layer, zdir = 'y', color = color, alpha = .7)

ax.set(xlabel = 'X', ylabel = 'Y', zlabel = 'Z', yticks = yLayersList)
plt.show()

在3D空间里绘制散点图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
fig = plt.figure()
ax = fig.gca(projection = '3d')

xs = np.random.rand(50)*10
ys = np.random.rand(50)*10+20
zs1 = np.random.rand(50)*10
zs2 = np.sqrt(xs**2+ys**2)

ax.scatter(xs, ys, zs = zs1, zdir = 'z', c = 'cornflowerblue', marker = 'o', s = 40)
ax.scatter(xs, ys, zs = zs2, zdir = 'z', c = 'purple', marker = '^', s = 40)

ax.set(xlabel = 'X', ylabel = 'Y', zlabel = 'Z')
plt.show()

绘制地图

澳大利亚的首都和首府城市的人口数量

 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
47
48
49
50
51
52
53
54
55
56
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

# city population in 2017
locations = {'Sydney':5131326, 'Melbourne':4850740, 'Brisbane':2408223, 'Adelaide':1333927, 
             'Perth':2043138, 'Hobart':226884, 'Darwin':146612, 'Canberra':41030}

# Latitude and Longigude in degrees
names = {'Sydney':(-33.86785,151.20732), 'Melbourne':(-37.8142,144.96332), 'Brisbane':(-27.46794,153.02809),
         'Adelaide':(-34.92866,138.59863), 'Perth':(-31.95224,115.8614), 'Hobart':(-42.87936,147.32941),
         'Darwin':(-12.46113,130.84185), 'Canberra':(-35.28346,149.12807)}

# setup mercator map projection
basemap = Basemap(projection = 'merc', resolution = 'h', area_thresh = 0.1, 
                  llcrnrlon = 112, llcrnrlat = -45, urcrnrlon = 155, urcrnrlat = -8)

# draw several map elements
basemap.drawcoastlines(linewidth = 0.6, linestyle = '-', color = '#b7cfe9', zorder = 3)
basemap.drawrivers(linewidth = 0.8, linestyle = '-', color = '#689CD2', zorder = 2)

basemap.fillcontinents(color = '#BF9E30', lake_color='#689CD2', zorder = 1)
basemap.drawmapboundary(color = 'gray', fill_color = '#689CD2')

basemap.drawmeridians(np.arange(0, 360, 15), color = '#4e8bca', labels = [0, 0, 0, 1], labelstyle = '+/-')
basemap.drawparallels(np.arange(-90, 90, 15), color = '#4e8bca', labels = [1, 1, 0, 0], labelstyle = '+/-')

# convert lon/lat (in degrees) tp x/y map projection coordinates (in meters)
# longitude is transformed into x and latitude in transformed into y
names_values = []
names_keys = list(names.keys())
for i, name in enumerate(names_keys):
    names_values.append(names[name])

lat_x, long_y = list(zip(*names_values))
x, y = basemap(long_y, lat_x)

# draw city markers and add text to markers
size_factor = 80.0
offset_factor = 21000
roration = 30
max_population = max(locations.values())

for city_name, city_x, city_y in zip(names_keys, x, y):
    size = (size_factor/max_population)*locations[city_name]
    x_offset = offset_factor
    y_offset = offset_factor
    basemap.scatter(city_x, city_y, s = size, facecolor = 'w', edgecolor = 'r', linewidths = 2.0, zorder = 10)
    plt.text(city_x+x_offset, city_y+y_offset, city_name)

# setup map title
font = dict(family = 'serif', fontsize = 15, weight = 'bold')
plt.title('Australian Population of Captial City', **font)

plt.show()

当前时间点的昼夜地理区域分布

 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
import datetime

# setup miller projection
basemap = Basemap(projection = 'mill', resolution = 'h', area_thresh = 0.1,
                  llcrnrlon = -180, llcrnrlat = -90, urcrnrlon = 180, urcrnrlat = 90)

# draw coastines
basemap.drawcoastlines(linewidth = 0.6, zorder = 2)

# darw mapboundary
basemap.drawmapboundary(fill_color = 'aqua')
# fill continents with color 'coral', and lake 'aqua'
basemap.fillcontinents(color = 'coral', lake_color = 'aqua', zorder = 1)
# draw meridians and parallels
basemap.drawmeridians(np.arange(-120, 150, 60), linewidth = 0.6, labels = [0, 0, 0, 1])
basemap.drawparallels(np.arange(-60, 80, 30), linewidth = 0.6, labels = [1, 0, 0, 0])

# shade the night areas, and use current in UTC
date = datetime.datetime.utcnow()
basemap.nightshade(date)

# format title with data and time
content  = 'Shade dark regions of the map %s (UTC)'
dtFormat = '%d %b %Y %H:%M:%S'
stringTime = date.strftime(dtFormat)
plt.title(content % stringTime, fontsize = 15)

plt.show()

城市之间相隔距离的可视化呈现

 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
class MapDisVisualization(Basemap):
    
    # get city names
    def getCityNames(self, names):
        namesKeys = list(names.keys())
        return namesKeys

    # define distance between cityA and cityB
    def citiesDistance(self, x, y):
        d = np.power(np.power(x[0]-y[0], 2)+np.power(x[1]-y[1], 2), 0.5)
        distance = round(d, 4)
        return distance

    # compute distance between target city and every other city
    def centerCittyDistance(self, city, names):
        distanceDict = {}
        namesKeys = self.getCityNames(names)
        for i, name in enumerate(namesKeys):
            if name != city:
                distanceDict[name] = self.citiesDistance(names[city], names[name])
        return distanceDict

    # compute line width and line color
    def setcolorandwidth(self, city, names):
        size_factor = 2.0
        namesKeys = self.getCityNames(names)
        distanceDict = self.centerCittyDistance(city, names)
        distanceList = list(distanceDict.values())
        maxDistacne = max(distanceList)
        for i, name in enumerate(namesKeys):
            if name != city:
                self.drawgreatcircle(names[city][1], names[city][0], names[name][1], names[name][0], linewidth = size_factor,
                                    color = mpl.cm.Blues(distanceDict[name]/float(maxDistacne)))
    # visualize city distance on the map
    def showmap(self, city, names):
        self.setcolorandwidth(city, names)
        namesKeys = self.getCityNames(names)
        number = len(namesKeys)
        titleContent = 'a map of visualizing distance between %s and every other city (%d cities)'
        font = dict(family = 'serif', fontsize = 15, weight = 'black')
        plt.title(titleContent % (city, (number - 1)), fontdict = font)
        plt.show()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def main(projection, city):
    # get a Basemap instance
    m = MapDisVisualization(projection = projection, resolution = 'h', area_thresh = 0.1, llcrnrlon = 112, llcrnrlat = -50, urcrnrlon = 180, urcrnrlat = -8)

    # draw several elements on the map
    m.drawcoastlines(linewidth = 0.6, linestyle = '-', zorder = 2)
    m.fillcontinents(alpha = 0.5, zorder = 1)
    m.drawmapboundary(color = 'gray')
    m.drawmeridians(np.arange(100, 180, 15), linewidth = 0.4, labels = [1, 0, 0, 0])

    # Latitude and Longditude in degrees
    names = {'Sydney':(-33.86785, 151.20732), 'Wellington':(-41.28664, 174.77557), 'Brisbane':(-27.46794, 153.02809), 'Adelaide':(-34.92866, 138.59863),
             'Perth':(-31.95224, 115.8614), 'Auckland':(-36.86667, 174.76667), 'Darwin':(-12.46113, 130.84185), 'Canberra':(-35.28346, 149.12807)}
    
    # show the distance between Sydeny and every other city
    m.showmap(city, names)

if __name__ == '__main__':
    # use projection mercator and choose Sydeny
    main('merc', 'Sydney')

图形属性

输入数据可以使用字符串代替变量

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

fig = plt.figure()
ax = fig.gca()

x = np.random.rand(50)*10
y = np.random.rand(50)*10+20
s = np.random.rand(50)*100
c = np.random.rand(50)

data = {'a':x, 'b':y, 'color':c, 'size':s}

# with the 'data' keyword argement
ax.scatter('a', 'b', c = 'color', s = 'size', data = data)

ax.set(xlabel = 'X', ylabel = 'Y')

plt.show()

以PDF文件格式存储画布图形

 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
from matplotlib.backends.backend_pdf import PdfPages

with PdfPages('./Ch13_PdfPages.pdf') as pdf:
    # page one
    plt.figure(figsize = (4, 4))
    x = np.random.rand(20)*100
    y = np.random.rand(20)*100+30
    s = np.random.rand(20)*100
    c = np.random.rand(20)
    data = {'a':x, 'b':y, 'color':c, 'size':s}
    plt.scatter('a', 'b', c = 'color', s = 'size', data = data)
    plt.title('Page1')
    pdf.savefig()  # save the current figure
    plt.close()  # close the current figure

    # page two
    fig = plt.figure(figsize = (8, 6))
    x = np.linspace(0, 2*np.pi, 100)
    y1 = 0.5*np.cos(x)
    y2 = 0.5*np.sin(x)
    plt.plot(y1, y2, color = 'navy', lw = 3)
    plt.axis('equal')
    plt.title('Page2')
    pdf.savefig(fig)  # pass the Figure instance fig to pdf.savefig
    plt.close(fig)  # close the Figure instance fig

    # page three
    fig, ax = plt.subplots(1, 2)
    x = np.linspace(0, 2*np.pi, 100)
    y = np.sin(x)*np.exp(-x)
    ax[0].scatter(x, y, c = 'cornflowerblue', s = 100)
    ax[1].plot(x, y, 'k-o', lw = 2)
    ax[1].set(xlim = (-1, 7))
    fig.suptitle('Page3')
    pdf.savefig(fig)
    plt.close(fig)

调用pyplot的API和面向对象的API设置图形属性

pyplot的API的代码编写方法

1
2
3
4
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 3]
line, = plt.plot(x, y)
plt.setp(line, 'linewidth', 2)
[None]

面向对象API的代码编写方法

1
2
3
4
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 3]
line, = plt.plot(x, y)
line.set_linewidth(2)

用树形图展示文件夹中的文件大小

 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
import squarify
from glob import glob
from matplotlib.image import thumbnail
from os import mkdir
from os.path import basename, dirname, getsize, isdir, join
from sys import argv

script, indir, outdir = argv

sizeList = []
nameList = []

if len(argv) != 3:
    print('On command line, information is not full.')
    raise SystemExit

if not isdir(indir):
    print('Input directory %r is not found.' %indir)
    raise SystemExit

if not isdir(outdir):
    print('Output directory %r is created.' % outdir)
    mkdir(outdir)
else:
    print('Output directory %r has been created' % outdir)

# the image file must be PNG or Pillow-readable
for fname in glob(join(indir, '*.png')):
    indir = dirname(fname)
    filename = basename(fname)
    outfile = join(outdir, filename)
    fig = thumbnail(fname, outfile, scale = 0.5)
    print('Copy %r of %r tpo %r' % (filename, indir, outdir))
    outfilesize = getsize(outfile)
    sizeList.append(outfilesize)
    fn = filename.split('.')
    nameList.append(fn[0])

# treemap
squarify.plot(sizeList, label = nameList, alpha = .7)
plt.hsv()  # set color
plt.axis('off')
plt.show()
1
!python Ch13_image_thumbnail_visualization.py "C:\Users\Boylad\Pictures" "C:\Users\Boylad\Pictures\thumbnail"
Output directory 'C:\\Users\\Boylad\\Pictures\\thumbnail' is created.
Copy 'chenping.png' of 'C:\\Users\\Boylad\\Pictures' tpo 'C:\\Users\\Boylad\\Pictures\\thumbnail'
Copy 'download (1).png' of 'C:\\Users\\Boylad\\Pictures' tpo 'C:\\Users\\Boylad\\Pictures\\thumbnail'
Copy 'phoenix.png' of 'C:\\Users\\Boylad\\Pictures' tpo 'C:\\Users\\Boylad\\Pictures\\thumbnail'
Copy 'school_of_evofish.png' of 'C:\\Users\\Boylad\\Pictures' tpo 'C:\\Users\\Boylad\\Pictures\\thumbnail'
Copy '荣格测试.png' of 'C:\\Users\\Boylad\\Pictures' tpo 'C:\\Users\\Boylad\\Pictures\\thumbnail'
Figure(640x480)

matplotlib 风格集的设置方法

1
2
3
from matplotlib.style.core import available

available
['Solarize_Light2',
 '_classic_test_patch',
 '_mpl-gallery',
 '_mpl-gallery-nogrid',
 'bmh',
 'classic',
 'dark_background',
 'fast',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn',
 'seaborn-bright',
 'seaborn-colorblind',
 'seaborn-dark',
 'seaborn-dark-palette',
 'seaborn-darkgrid',
 'seaborn-deep',
 'seaborn-muted',
 'seaborn-notebook',
 'seaborn-paper',
 'seaborn-pastel',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-whitegrid',
 'tableau-colorblind10']
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from matplotlib.style.core import use, available

use('fivethirtyeight')

# ColorBrewer Diverging: RdYlBu
hexHtml = ['#d73027', '#f46d43', '#fdae61', '#fee090', '#ffffbf', '#e0f3f8', '#abd9e9', '#74add1', '#4575b4']

sample = 1000
fig, ax = plt.subplots()

for i in range(len(hexHtml)):
    x = np.arange(sample)
    y = np.random.normal(0, 0.1, size = sample).cumsum()
    ax.scatter(x, y, label = str(i), linewidths = 0.1, edgecolors = 'grey', facecolor = hexHtml[i])

ax.legend()

plt.show()

 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
use('ggplot')

x = np.linspace(0, 2*np.pi, 100)
y = 1.85*np.sin(x)
y1 = 1.85*np.sin(x)+np.random.randn(100)

fig, ax = plt.subplots(2, 2, sharex = True, sharey = True)

# subplot(2, 2, 1)
ax[0, 0].scatter(x, y1, s = 50, c = 'dodgerblue')
ax[0, 0].set_ylim(-5, 5)
ax[0, 0].set_facecolor('lemonchiffon')

# subplot(2, 2, 2)
ax[0, 1].plot(x, y, lw = 3, color = 'yellowgreen')
ax[0, 1].set_xlim(-1, 7)
ax[0, 1].set_ylim(-5, 5)
ax[0, 1].set_facecolor('lemonchiffon')

# subplot(2, 2, 3)
ax[1, 0].plot(x, y, ls = '--', lw = 3, color = 'k')
ax[1, 0].scatter(x, y1, s = 50, c = 'r')
ax[1, 0].set_ylim(-5, 5)
ax[1, 0].set_facecolor('lemonchiffon')

# subplot(2, 2, 4)
# non-existence

fig.suptitle("'ggplot' style of subplots (2, 2)", fontsize = 18, weight = 'bold', family = 'monospace')

plt.show(0)

matplotlib后端类型的配置方法

1
2
3
import matplotlib as mpl

print(mpl.get_backend())
module://ipykernel.pylab.backend_inline

3种方法改变后端类型

  • 方法1——调用属性字典rcParams
1
2
3
4
5
6
7
8
mpl.rcParams['backend'] = 'Qt5Agg'

x = np.linspace(-2*np.pi, 2*np.pi, 1000)
y = np.exp(-x)*np.sin(2*np.pi*x)

plt.plot(x, y, linewidth = 3.0)

plt.show()

  • 方法2——使用函数use()
1
2
3
4
5
6
mpl.use('qt5agg')

x = np.linspace(-2*np.pi, 2*np.pi, 1000)
y = np.exp(-x)*np.sin(2*np.pi*x)

plt.plot(x, y, color = 'g', linewidth = 3.0)
[<matplotlib.lines.Line2D at 0x290b9466b20>]

  • 方法3——使用配置文件matplotlibrc

matplotlibrc配置文件和脚本放同一路径下!

1
2
3
4
5
6
x = np.linspace(-2*np.pi, 2*np.pi, 1000)
y = np.exp(-x)*np.sin(2*np.pi*x)

plt.plot(x, y, color = 'k', linewidth = 3.0)

plt.show()

Latex

案例展示

 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
47
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from jupyterthemes import jtplot
jtplot.style(theme='grade3', context='paper')

plt.rc('text', usetex=True)
plt.rc('text.latex', preamble=r'\usepackage{amsmath}')
plt.rc("font",**{"family":"sans-serif","sans-serif":["Helvetica"],"size":16})

# sample data
t = np.linspace(0.0, 1.0, 100)
s = np.cos(4*np.pi*t)+2

# plt figure
plt.plot(t, s, ls = '-', lw = 0.5, c = 'b')

# No.1 text
plt.text(0.2,2.8, r"$some\ ranges:(\alpha),[\beta],\{\gamma\},|\Gamma|,\Vert\phi\Vert,\langle\Phi\rangle$")

# No.2 text
# these went wrong in pdf in a previous version
plt.text(0.2, 2.5, r"gamma: $\gamma$", {"color": "r", "fontsize": 20})
plt.text(0.2, 2.3, r"Omega: $\Omega$", {"color": "b", "fontsize": 20})

# No.3 text
plt.text(0.2,2.0,r"$\lim_{i\to\infty}\cos(2\pi)\times\exp\{-i\}=0$")

# No.4 text
plt.text(0.2,1.5,r"$\mathrm{math\ equation}:\frac{n!}{(n-k)!}=\binom{n}{k}$",{"color": "c", "fontsize": 20})

# No.5 text
plt.text(0.2,1.2,r"$\forall\ i,\exists\ \alpha_i\geq\beta_i,\sqrt{\alpha_i-\beta_i}\geq{0}$")

# we can write labels with LaTeX
plt.xlabel(r"\textbf{time(s)}")
plt.ylabel(r"\textit{Velocity(m/s)}")

# and also write title with LaTex
plt.title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!", color="r")

plt.annotate(r"$\cos(4\times\pi\times{t})+2$", xy=(0.87,2.0), xytext=(0.65,2.3),
             color="r", arrowprops={"arrowstyle":"->","color":"r"})

plt.subplots_adjust(top=0.8)
plt.grid(False)         
plt.show()

数学表达式

设置输出字符串的字体效果

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
from jupyterthemes import jtplot
jtplot.style(theme='grade3', context='paper')

# set sample data
x = np.linspace(0,10,10000)
y = np.sin(x)*np.cos(x)
plt.figure(figsize = (8,6))
plt.plot(x,y,ls="-",lw=2,color="c",alpha=0.3)

plt.text(1,0.5,r"\mathrm{Roman}:$\mathrm{Roman}\/(1st)$", fontsize=15)
plt.text(1,0.4,r"\mathit{Italic}:$\mathit{Italic}\/(2nd)$", fontsize=15)
plt.text(1,0.3,r"\mathtt{Typewriter}:$\mathtt{Typewriter}\/(3rd)$", fontsize=15)
plt.text(1,0.2,r"\mathcal{CALLIGRAPHY}:$\mathcal{CALLIGRAPHY}\/(4th)$", fontsize=15)
plt.text(1,0.1,r"\mathbb{blackboard}:$\mathbb{blackboard}\/(5th)$", fontsize=15)
plt.text(1,0.0,r"\mathfrak{Fraktur}:$\mathfrak{Fraktur}\/(6th)$", fontsize=15)
plt.text(1,-0.1,r"\mathsf{sansserif}:$\mathsf{sansserif}\/(7th)$", fontsize=15)
# plt.text(1,-0.2,r"\mathcircled{circled}:$\mathcircled{3333}\/(8th)$", fontsize=15)
plt.text(1,-0.3,r"\mathrm{\mathbb{blackboard}}:$\mathrm{\mathbb{blackboard}}\/(9th)$",fontsize=15)
plt.grid(False)
plt.show()

通过数学公式和数学表达式学习TeX符号的编写规则

$\sin^2\alpha+\cos^2\alpha=1$

1
2
3
4
5
6
7
# set sample data
x = np.linspace(0,10,10000)
y = np.power(np.sin(x),2)+np.power(np.cos(x),2)
plt.plot(x,y,ls="-",lw=2,color="c",alpha=0.3)
plt.text(1,1.01,r"$\sin^2\alpha+\cos^2\alpha=1$",fontsize=20)
plt.grid(False)
plt.show()

$\bar X = \frac{\sum_{i=1}^n x_i}{n}$

1
2
3
4
5
6
7
8
9
# set sample data
x = np.linspace(0,10,10000)
y = np.sin(x)*np.cos(x)
plt.plot(x,y,ls="-",lw=2,color="c",alpha=0.3)
plt.text(1,0.02,r"$\mathrm{\bar x=\frac{\sum_{i=1}^{n}x_i}{n}}$",
fontsize=20)
plt.axhline(y=0,ls=":",lw=2,color="r")
plt.grid(False)
plt.show()

  • 使用Latex渲染
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
plt.rc('text', usetex=True)
plt.rc('text.latex', preamble=r'\usepackage{amsmath}')
plt.rc("font",**{"family":"sans-serif","sans-serif":["Helvetica"],"size":16})

# set sample data
x = np.linspace(0,10,10000)
y = np.sin(x)*np.cos(x)
plt.plot(x,y,ls="-",lw=2,color="c",alpha=0.3)
plt.text(1,0.02,r"$\mathrm{\bar x = \frac{\sum_{i=1}^n x_i}{n}}$",fontsize=20)
plt.axhline(y=0,ls=":",lw=2,color="r")
plt.grid(False)
plt.show()

$\lim_{n\to\infty}(1+\frac{1}{n})^n$

1
2
3
4
5
6
7
8
9
# set sample data
x = np.linspace(1,10,10000)
y = np.power(1+1/x,x)
plt.plot(x,y,ls="-",lw=2,color="c",alpha=0.3)
plt.text(1,2.64,r"$\mathrm{\lim_{n\to\infty}(1+\frac{1}{n})^{n}}$",
fontsize=20)
plt.axhline(y=np.exp(1),ls=":",lw=2,color="r")
plt.grid(False)
plt.show()

$\max_{0\leq x \leq10}xe^{-x^2}$

1
2
3
4
5
6
7
8
# set sample data
x = np.linspace(0,10,10000)
y = x*np.power(np.exp(1),(-x**2))
plt.plot(x,y,ls="-",lw=2,color="r")
plt.text(1,0.39,r"$\mathrm{\max_{0\leq{x}\leq10} xe^{-{x}^2}}$",fontsize=20)
plt.axhline(y=np.max(x*np.power(np.exp(1),(-x**2))),ls=":",lw=2,color="c")
plt.grid(False)
plt.show()

$\log_2 x$

1
2
3
4
5
6
7
8
9
# set sample data
x = np.linspace(0.5,16,100)
y = np.array([np.log2(value) for value in x])
plt.plot(x,y,ls="-",lw=2,color="r")
plt.text(4.0,1.6,r"$\mathrm{\log_{2}{x}}$",fontsize=20)
plt.axhline(y=1,ls=":",lw=2,color="c")
plt.axvline(x=2,ls=":",lw=2,color="c")
plt.grid(False)
plt.show()

矩阵$\begin{pmatrix} \ln{e^2} & 2 \ 1 & \ln e \end{pmatrix}$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# set sample data
x = np.linspace(0.5,16,100)
y = [np.log(value) for value in x]
plt.plot(x,y,ls="-",lw=2,color="r")
plt.text(4.0,2.1,r"$\mathrm{\begin{pmatrix} \ln{e^{2}}&2 \\ 1&\ln{e} \end{pmatrix}}$",fontsize=20)
plt.text(12.0,2.3,r"$\mathrm{y=\ln{x}}$",fontsize=20)
#(e,1)
plt.axhline(y=1,ls=":",lw=1,color="c")
plt.axvline(x=np.exp(1),ls=":",lw=1,color="c")
#(e^2,2)
plt.axhline(y=2,ls=":",lw=1,color="c")
plt.axvline(x=(np.exp(1))*(np.exp(1)),ls=":",lw=1,color="c")
plt.grid(False)
plt.show()

分享

Guankui Liu
作者
Metasphinx
My research interests include statistics, computer vision and ecological modelling.