Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Examples used in talks 

4""" 

5 

6 

7def example_networkx(ax=None, **options): 

8 """ 

9 Example using :epkg:`networkx`. 

10 

11 @param ax axis 

12 @param options look at the code 

13 @return ax 

14 """ 

15 import networkx as nx 

16 import matplotlib.pyplot as plt 

17 

18 G = nx.random_geometric_graph(200, 0.125) 

19 # position is stored as node attribute data for random_geometric_graph 

20 pos = nx.get_node_attributes(G, 'pos') 

21 

22 # find node near center (0.5,0.5) 

23 dmin = 1 

24 ncenter = 0 

25 for n in pos: 

26 x, y = pos[n] 

27 d = (x - 0.5) ** 2 + (y - 0.5) ** 2 

28 if d < dmin: 

29 ncenter = n 

30 dmin = d 

31 

32 # color by path length from node near center 

33 p = nx.single_source_shortest_path_length(G, ncenter) 

34 

35 if ax is None: 

36 _, ax = plt.subplots( 

37 nrows=1, ncols=1, figsize=options.get('figsize', (5, 5))) 

38 

39 nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4, ax=ax) 

40 nx.draw_networkx_nodes(G, pos, nodelist=p.keys(), 

41 node_size=80, ax=ax) 

42 

43 ax.set_xlim(-0.05, 1.05) 

44 ax.set_ylim(-0.05, 1.05) 

45 ax.axis('off') 

46 return ax 

47 

48 

49def example_confidence_interval(ax=None, seaborn=False, **options): 

50 """ 

51 Draws pseudo confidence interval for a regression 

52 in a :epkg:`matplotlib` graph. 

53 

54 @param ax axis 

55 @param seaborn uses :epkg:`seaborn` 

56 instead of :epkg:`matplotlib` 

57 @param options look at the code 

58 @return ax 

59 """ 

60 import matplotlib.pyplot as plt 

61 

62 if ax is None: 

63 _, ax = plt.subplots( 

64 nrows=1, ncols=1, figsize=options.get('figsize', (5, 5))) 

65 

66 import scipy 

67 import numpy 

68 nx, nboot = 22, 400 

69 x = scipy.linspace(0.0, 1.0, nx) # pylint: disable=E1101 

70 data = x + numpy.random.normal(loc=0.0, scale=0.1, size=nx) 

71 yp = scipy.polyfit(x, data, 1) # pylint: disable=E1101 

72 y = scipy.polyval(yp, x) # pylint: disable=E1101 

73 

74 if seaborn: 

75 from seaborn import regplot 

76 return regplot(x=x, y=y, ax=ax) 

77 else: 

78 r = data - y 

79 for _ in range(nboot): 

80 pc = scipy.polyfit( # pylint: disable=E1101 

81 x, y + r[scipy.random.randint(0, nx - 1, nx)], 1) # pylint: disable=E1101 

82 ax.plot(x, scipy.polyval(pc, x), 'k-', # pylint: disable=E1101 

83 linewidth=2, alpha=3.0 / float(nboot)) 

84 ax.plot(x, y, 'k-') 

85 ax.plot(x, data, 'ko') 

86 return ax 

87 

88 

89def example_cartopy(ax=None, **options): 

90 """ 

91 Draws a map of France 

92 with :epkg:`cartopy`. 

93 

94 @param ax axis 

95 @param options look at the code 

96 @return ax 

97 """ 

98 import cartopy.crs as ccrs 

99 import cartopy.feature as cfeature 

100 import matplotlib.pyplot as plt 

101 

102 fig = plt.figure(figsize=(7, 7)) 

103 ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree()) 

104 ax.set_extent([-5, 10, 42, 52]) 

105 

106 ax.add_feature(cfeature.OCEAN) 

107 ax.add_feature(cfeature.COASTLINE) 

108 ax.add_feature(cfeature.RIVERS) 

109 ax.add_feature(cfeature.LAKES) 

110 ax.add_feature(cfeature.LAND) 

111 ax.add_feature(cfeature.BORDERS, linestyle=':') 

112 ax.plot([2.35, 2.20], [48.85, 48.71], '.') 

113 ax.text(2.35, 48.85, "Paris") 

114 ax.text(2.20, 48.71, "Saclay", ha="right") 

115 ax.set_title('France') 

116 return ax 

117 

118 

119def example_pydy(ax=None, **options): 

120 """ 

121 Example from the documentation of 

122 :epkg:`pydy`. 

123 

124 @param ax matplotlib axis 

125 @parm options extra options 

126 @return ax 

127 """ 

128 # part 1 

129 

130 from sympy import symbols 

131 import sympy.physics.mechanics as me 

132 

133 mass, stiffness, damping, gravity = symbols('m, k, c, g') 

134 

135 position, speed = me.dynamicsymbols('x v') 

136 positiond = me.dynamicsymbols('x', 1) 

137 force = me.dynamicsymbols('F') 

138 

139 ceiling = me.ReferenceFrame('N') 

140 

141 origin = me.Point('origin') 

142 origin.set_vel(ceiling, 0) 

143 

144 center = origin.locatenew('center', position * ceiling.x) 

145 center.set_vel(ceiling, speed * ceiling.x) 

146 

147 block = me.Particle('block', center, mass) 

148 

149 kinematic_equations = [speed - positiond] 

150 

151 force_magnitude = mass * gravity - stiffness * position - damping * speed + force 

152 forces = [(center, force_magnitude * ceiling.x)] 

153 

154 particles = [block] 

155 

156 kane = me.KanesMethod(ceiling, q_ind=[position], u_ind=[speed], 

157 kd_eqs=kinematic_equations) 

158 kane.kanes_equations(forces, particles) 

159 

160 # part 2 

161 

162 from numpy import linspace, sin 

163 from pydy.system import System 

164 

165 sys = System(kane, 

166 constants={mass: 1.0, stiffness: 1.0, 

167 damping: 0.2, gravity: 9.8}, 

168 specifieds={force: lambda x, t: sin(t)}, 

169 initial_conditions={position: 0.1, speed: -1.0}, 

170 times=linspace(0.0, 10.0, 1000)) 

171 

172 y = sys.integrate() 

173 

174 # part 3 

175 

176 import matplotlib.pyplot as plt 

177 if ax is None: 

178 _, ax = plt.subplots( 

179 nrows=1, ncols=1, figsize=options.get('figsize', (5, 5))) 

180 

181 ax.plot(sys.times, y) 

182 ax.legend((str(position), str(speed))) 

183 return ax