3. Example Codes

The following are some examples showing what this module is capable of.

3.1. Annotating a sine wave

In this example, since the axes are not “equal”, the auto scale should be on.

Sine wave example
  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
# -*- coding: utf-8 -*- 

'''
Author : 高斯羽 博士 (Dr. GAO, Siyu)

Version : 1.0.0

Last Modified : 2019-04-22

This script is an example to demonstration how to use the "curlyBrace" module to plot curly brackets using matplotlib.

This example demonstrate annotating two sine waves with the auto scale on and off.
'''

import matplotlib.pyplot as plt
import numpy as np
from curlyBrace import curlyBrace
import os

# save fig control
bool_savefig = False

# figure size and dpi
dbl_width   = 800.0
dbl_height  = 800.0
dbl_dpi     = 100.0

# line width and colour for the sine wave
lw = 2
color='royalblue'

# plot the sine wave
theta = np.linspace(0.0, 2.0 * np.pi, 101)

x = theta
y = np.sin(theta) * 8.0

fig, axes = plt.subplots(2, 1 , figsize=(dbl_width / dbl_dpi, dbl_width / dbl_dpi), dpi=dbl_dpi)

# fontdict for axis title
font = {'family': 'Arial',
        'color':  'k',
        'weight': 'bold',
        'style': 'normal',
        'size': 20,
        }

# aixs title string
str_title = [None, None]

str_title[0] = 'Example: Different axes scales, auto scale on'
str_title[1] = 'Example: Different axes scales, auto scale off'

axes[0].set_title(str_title[0], fontdict=font)
axes[0].plot(x, y, lw=lw, color=color)
axes[0].grid(linestyle='--')
axes[0].set_xlim(xmin=0.0, xmax=2.0*np.pi)

axes[1].set_title(str_title[1], fontdict=font)
axes[1].plot(x, y, lw=lw, color=color)
axes[1].grid(linestyle='--')
axes[1].set_xlim(xmin=0.0, xmax=2.0*np.pi)

# curly bracket 1 start point and end point
p1 = [0.0, 0.0]
p2 = [np.pi, 0.0]

# curly bracket 2 start point and end point
p3 = [np.pi, 0.0]
p4 = [2.0 * np.pi, 0.0]

# fontdict for curly bracket 1 text
font = {'family': 'serif',
        'color':  'k',
        'weight': 'bold',
        'style': 'italic',
        'size': 20,
        }

# curly brack text
str_text = '$\pi$'

# coefficient for curly bracket 1
k_r1 = 0.05

# coefficient for curly bracket 2
k_r2 = 0.1

# clockwise, need to swap the start point and end point
curlyBrace(fig, axes[0], p2, p1, k_r1, bool_auto=True, str_text=str_text, color='r', lw=2, int_line_num=1, fontdict=font)

# anti-clockwise, no need to swap the start point and end point
curlyBrace(fig, axes[0], p3, p4, k_r2, bool_auto=True, str_text=str_text, color='darkorange', lw=3, int_line_num=1, fontdict=font)

# clockwise, need to swap the start point and end point
curlyBrace(fig, axes[1], p2, p1, k_r1, bool_auto=False, str_text=str_text, color='m', lw=2, int_line_num=1, fontdict=font)

# anti-clockwise, no need to swap the start point and end point
curlyBrace(fig, axes[1], p3, p4, k_r2, bool_auto=False, str_text=str_text, color='darkgreen', lw=3, int_line_num=1, fontdict=font)

# save fig
if bool_savefig:

    str_filename = os.path.basename(__file__)[:-3] + '.png'

    str_filename = os.path.join(os.getcwd(), str_filename)

    fig.savefig(str_filename, bbox_inches='tight', dpi=300)

else:

    pass

plt.show()

3.2. Annotating two circles

The annotation text follows an “aniti-clockwise” convention for its location. To flip it, swap the start point and the end point. Also, since the axes are “equal” in this example, auto scale should be off.

Cirle example
  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
# -*- coding: utf-8 -*- 

'''
Author : 高斯羽 博士 (Dr. GAO, Siyu)

Version : 1.0.0

Last Modified : 2019-04-22

This script is an example to demonstration how to use the "curlyBrace" module to 
plot curly brackets using matplotlib.

This example demonstrate annotating two pairs of concentrate circles with the auto scale off.
'''

import matplotlib.pyplot as plt
import numpy as np
from curlyBrace import curlyBrace
import os

# fig save control
bool_savefig = False

# figure size and dpi
dbl_width   = 1000.0
dbl_height  = 1080.0
dbl_dpi     = 100.0

# line width and colour for the circles
lw = 2
color='royalblue'

# plot the two concentrate circles
theta = np.linspace(0, 2.0 * np.pi, 101)

# the radius for the circles
r1 = 5.0
r2 = 15.0

# the points of the circles
x1 = r1 * np.cos(theta)
y1 = r1 * np.sin(theta)

x2 = r2 * np.cos(theta)
y2 = r2 * np.sin(theta)

fig, axes = plt.subplots(2, 1, figsize=(dbl_width / dbl_dpi, dbl_height / dbl_dpi), dpi=dbl_dpi)

# fontdict for axis titles
font = {'family': 'Arial',
        'color':  'k',
        'weight': 'bold',
        'style': 'normal',
        'size': 12,
        }

str_title1 = 'Example: Anti-Clockwise, equal aspect, auto scale off'
str_title2 = 'Example: Clockwise, equal aspect, auto scale off'

axes[0].plot(x1, y1, lw=lw, color=color)
axes[0].plot(x2, y2, lw=lw, color=color)
axes[0].set_aspect('equal', 'box')
axes[0].grid(color='lightgray', linestyle='--')
axes[0].set_title(str_title1, fontdict=font)

axes[1].plot(x1, y1, lw=lw, color=color)
axes[1].plot(x2, y2, lw=lw, color=color)
axes[1].set_aspect('equal', 'box')
axes[1].grid(color='lightgray', linestyle='--')
axes[1].set_title(str_title2, fontdict=font)

# bracket coefficient
k_r = 0.1
lw = 3

# points for the brackets
phi = np.linspace(0, 2.0 * np.pi, 13)

x1 = r1 * np.cos(phi)
y1 = r1 * np.sin(phi)

x2 = r2 * np.cos(phi)
y2 = r2 * np.sin(phi)

# fontdict for the brackets
font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'style': 'normal',
        'size': 9,
        }

for i in range(0, len(x1)):

    p1 = [x1[i], y1[i]]
    p2 = [x2[i], y2[i]]

    str_text = 'Circle\nanti-clockwise'

    curlyBrace(fig, axes[0], p1, p2, k_r, bool_auto=False, str_text=str_text, color='r', lw=lw, int_line_num=2, fontdict=font)

for i in range(0, len(x1)):

    p1 = [x2[i], y2[i]]
    p2 = [x1[i], y1[i]]

    str_text = 'Circle\nclockwise'

    curlyBrace(fig, axes[1], p1, p2, k_r, bool_auto=False, str_text=str_text, color='darkorange', lw=lw, int_line_num=2, fontdict=font)

if bool_savefig:

    str_filename = os.path.basename(__file__)[:-3] + '.png'

    str_filename = os.path.join(os.getcwd(), str_filename)

    fig.savefig(str_filename, bbox_inches='tight', dpi=300)

else:

    pass

plt.show()

3.3. Annotating two ellipses

Similar to the circle example, but this time demonstrates not “equal” axes and “equal” axes. You should be able to that see the summits of the brackets in the second subplot are kinda warpped.

Ellipse example
  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
# -*- coding: utf-8 -*- 

'''
Author : 高斯羽 博士 (Dr. GAO, Siyu)

Version : 1.0.0

Last Modified : 2019-04-22

This script is an example to demonstration how to use the "curlyBrace" module to 
plot curly brackets using matplotlib.

This example demonstrate annotating two pairs of ellipses.

The parametric equation of ellipse can be found:
https://stackoverflow.com/questions/10952060/plot-ellipse-with-matplotlib-pyplot-python
'''

import matplotlib.pyplot as plt
import numpy as np
from curlyBrace import curlyBrace
import os

# fig save control
bool_savefig = False

# figure size and dpi
dbl_width   = 1000.0
dbl_height  = 800.0
dbl_dpi     = 100.0

# line width and colour for the ellipses
lw = 2
color='royalblue'

u = 0.0  # x-position of the centre
v = 0.0  # y-position of the centre
a1 = 2.0  # radius on the x-axis for ellipse 1
b1 = 1.5  # radius on the y-axis for ellipse 1
a2 = 7.0  # radius on the x-axis for ellipse 2
b2 = 5.5  # radius on the y-axis for ellipse 2

theta = np.linspace(0, 2.0 * np.pi, 101)

x1 = u + a1 * np.cos(theta)
y1 = v + b1 * np.sin(theta)
x2 = u + a2 * np.cos(theta)
y2 = v + b2 * np.sin(theta)

fig, axes = plt.subplots(2, 1, figsize=(dbl_width / dbl_dpi, dbl_width / dbl_dpi), dpi=dbl_dpi)

# fontdict for axis titles
font = {'family': 'Arial',
        'color':  'k',
        'weight': 'bold',
        'style': 'normal',
        'size': 12,
        }

str_title1 = 'Example: Axes aspect equal, auto scale off'
str_title2 = 'Example: Axes aspect equal, auto scale on'

axes[0].plot(x1, y1, lw=lw, color=color)
axes[0].plot(x2, y2, lw=lw, color=color)
axes[1].plot(x1, y1, lw=lw, color=color)
axes[1].plot(x2, y2, lw=lw, color=color)

axes[0].set_aspect('equal', 'box')
axes[0].grid(color='lightgray', linestyle='--')
axes[0].set_title(str_title1, fontdict=font)
axes[1].set_aspect('equal', 'box')
axes[1].grid(color='lightgray', linestyle='--')
axes[1].set_title(str_title2, fontdict=font)

# bracket coefficient
k_r = 0.1

# points for the brackets
phi = np.linspace(0, 2.0 * np.pi, 13)

x1 = u + a1 * np.cos(phi)
y1 = v + b1 * np.sin(phi)
x2 = u + a2 * np.cos(phi)
y2 = v + b2 * np.sin(phi)

# fontdict for the brackets
font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'style': 'normal',
        'size': 8,
        }

# bracket line width and colour
lw = 3
color1 = 'r'
color2 = 'darkorange'

for i in range(0, len(x1)):

    p1 = [x1[i], y1[i]]
    p2 = [x2[i], y2[i]]

    str_text = 'Epllise\nanti-clockwise'

    curlyBrace(fig, axes[0], p1, p2, k_r, bool_auto=False, str_text=str_text, color=color1, lw=lw, int_line_num=3, fontdict=font)

for i in range(0, len(x1)):

    p1 = [x2[i], y2[i]]
    p2 = [x1[i], y1[i]]

    str_text = 'Epllise\nclockwise'

    curlyBrace(fig, axes[1], p1, p2, k_r, bool_auto=True, str_text=str_text, color=color2, lw=lw, int_line_num=3, fontdict=font)

if bool_savefig:

    str_filename = os.path.basename(__file__)[:-3] + '.png'

    str_filename = os.path.join(os.getcwd(), str_filename)

    fig.savefig(str_filename, bbox_inches='tight', dpi=300)

else:

    pass

plt.show()

3.4. Annotating two astroids

Similar to the ellipse example, but this time demonstrates what would happen when the axes are “equal” but auto scale is on.

Astroid example
  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
# -*- coding: utf-8 -*- 

'''
Author : 高斯羽 博士 (Dr. GAO, Siyu)

Version : 1.0.0

Last Modified : 2019-04-22

This script is an example to demonstration how to use the "curlyBrace" module to 
plot curly brackets using matplotlib.

This example demonstrate annotating two pairs of astroids.

The parametric equation of astroid can be found:
http://jwilson.coe.uga.edu/EMAT6680Fa2014/Gieseking/Exploration%2010/Parametric%20Equations.html
'''

import matplotlib.pyplot as plt
import numpy as np
from curlyBrace import curlyBrace
import os

# fig save control
bool_savefig = False

# figure size and dpi
dbl_width   = 1000.0
dbl_height  = 800.0
dbl_dpi     = 100.0

# line width and colour for the astroids
lw = 2
color='royalblue'

r1 = 5.0    # radius 1
r2 = 20.0   # radius 2

theta = np.linspace(0, 2.0 * np.pi, 101)

# calculate the points of the two astroids
x1 = r1 * (np.sin(theta) ** 3)
y1 = r1 * (np.cos(theta) ** 3)
x2 = r2 * (np.sin(theta) ** 3)
y2 = r2 * (np.cos(theta) ** 3)

# fontdict for axis titles
font = {'family': 'Arial',
        'color':  'k',
        'weight': 'bold',
        'style': 'normal',
        'size': 12,
        }

str_title1 = 'Example: Not "equal" axes aspect, auto scale on'
str_title2 = 'Example: "Equal" axes aspect, auto scale off'

fig, axes = plt.subplots(2, 1, figsize=(dbl_width / dbl_dpi, dbl_width / dbl_dpi), dpi=dbl_dpi)

axes[0].plot(x1, y1, lw=lw, color=color)
axes[0].plot(x2, y2, lw=lw, color=color)
# axes[0].set_aspect('equal', 'box')
axes[0].grid(color='lightgray', linestyle='--')
axes[0].set_title(str_title1, fontdict=font)

axes[1].plot(x1, y1, lw=lw, color=color)
axes[1].plot(x2, y2, lw=lw, color=color)
axes[1].set_aspect('equal', 'box')
axes[1].grid(color='lightgray', linestyle='--')
axes[1].set_title(str_title2, fontdict=font)

# bracket coefficient
k_r = 0.1

# points for the brackets
phi = np.linspace(0, 2.0 * np.pi, 9)

x1 = r1 * (np.sin(phi) ** 3)
y1 = r1 * (np.cos(phi) ** 3)
x2 = r2 * (np.sin(phi) ** 3)
y2 = r2 * (np.cos(phi) ** 3)

# fontdict for the brackets
font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'style': 'normal',
        'size': 6,
        }

# bracket line width and colour
lw = 2
color1 = 'r'
color2 = 'darkorange'

for i in range(0, len(x1)):

    p1 = [x1[i], y1[i]]
    p2 = [x2[i], y2[i]]

    str_text = 'Astroid\nanti-clockwise'

    curlyBrace(fig, axes[0], p1, p2, k_r, bool_auto=True, str_text=str_text, color=color1, lw=lw, int_line_num=2, fontdict=font)

for i in range(0, len(x1)):

    p1 = [x2[i], y2[i]]
    p2 = [x1[i], y1[i]]

    str_text = 'Astroid\nclockwise'

    curlyBrace(fig, axes[1], p1, p2, k_r, bool_auto=False, str_text=str_text, color=color2, lw=lw, int_line_num=2, fontdict=font)

if bool_savefig:

    str_filename = os.path.basename(__file__)[:-3] + '.png'

    str_filename = os.path.join(os.getcwd(), str_filename)

    fig.savefig(str_filename, bbox_inches='tight', dpi=300)

else:

    pass

plt.show()

3.5. Annotating linear axes with log axes

This example shows that this module also works with log scale. The starting point and the end point are the same in the two subplots.

P.S. The really difficult thing to work around is the “symlog” scale. Not because it could have negative log, but because part of it is log while part of it is linear. This breaks the transformation. I hope you don’t need to use curly brackets on symlog. If you have a good solution, please let me know.

Log example
 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
# -*- coding: utf-8 -*- 

'''
Author : 高斯羽 博士 (Dr. GAO, Siyu)

Version : 1.0.0

Last Modified : 2019-04-22

This script is an example to demonstration how to use the "curlyBrace" module to 
plot curly brackets using matplotlib.

This example demonstrate the function works on log scale as well.

But the result on "symlog" is not satisfactory.
'''

import numpy as np
import matplotlib.pyplot as plt
from curlyBrace import curlyBrace
import os

# fig save control
bool_savefig = False

# figure size and dpi
dbl_width   = 800.0
dbl_height  = 800.0
dbl_dpi     = 100.0
lw = 3

# make data
x = np.linspace(0.1, 100, 200)
y = 2.0 * x

# points
p1 = [20, 10.69]
p2 = [80, 151.6]

# p3 = [20.0, 27.58]
# p4 = [87.8, 115.95]

# fontdict for axis title
font = {'family': 'Arial',
        'color':  'k',
        'weight': 'bold',
        'style': 'normal',
        'size': 14
        }

fig, axes = plt.subplots(2, 1, figsize=(dbl_width / dbl_dpi, dbl_width / dbl_dpi), dpi=dbl_dpi)

# linear
axes[0].plot(x,y, lw=lw)
axes[0].set_title('Linear Scale', fontdict=font)
axes[0].grid(True)

curlyBrace(fig, axes[0], p2, p1, k_r=0.05, str_text='Linear', int_line_num=2, color='r', lw=2)

# log
axes[1].plot(x,y, lw=lw)
axes[1].set_title('Log Scale', fontdict=font)
axes[1].set_yscale('log')
axes[1].grid(True)

curlyBrace(fig, axes[1], p2, p1, k_r=0.05, str_text='Log', int_line_num=2, color='r', lw=2)

fig.suptitle('Example: Log scale', fontweight='bold', fontsize=20)

if bool_savefig:

    str_filename = os.path.basename(__file__)[:-3] + '.png'

    str_filename = os.path.join(os.getcwd(), str_filename)

    fig.savefig(str_filename, bbox_inches='tight', dpi=300)

else:

    pass

plt.show()