You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"<a href=\"https://colab.research.google.com/github/GEORMC/Nnumerical_Methods_Course/blob/main/python_plotting_with_descriptions.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
11
+
]
12
+
},
13
+
{
14
+
"cell_type": "markdown",
15
+
"id": "b3809745",
16
+
"metadata": {
17
+
"id": "b3809745"
18
+
},
19
+
"source": [
20
+
"# Python Plotting Examples for Finite Element Course\n",
21
+
"\n",
22
+
"This notebook demonstrates how to create different types of plots using Python's `matplotlib` library.\n",
23
+
"We will cover the following:\n",
24
+
"1. Basic Plot\n",
25
+
"2. Adding Multiple Plots\n",
26
+
"3. Subplots\n",
27
+
"4. Customizing Plots\n",
28
+
"5. Saving a Plot\n",
29
+
"6. Finite Element Visualization Example\n",
30
+
"\n",
31
+
"Each section includes code and explanation of the corresponding plot.\n"
32
+
]
33
+
},
34
+
{
35
+
"cell_type": "markdown",
36
+
"id": "bea5d990",
37
+
"metadata": {
38
+
"id": "bea5d990"
39
+
},
40
+
"source": [
41
+
"## 1. Basic Plot Example\n",
42
+
"\n",
43
+
"This is a simple example of how to create a line plot using `matplotlib`.\n",
44
+
"We will plot a sine wave over a range of values.\n"
45
+
]
46
+
},
47
+
{
48
+
"cell_type": "code",
49
+
"execution_count": null,
50
+
"id": "423c9f77",
51
+
"metadata": {
52
+
"id": "423c9f77"
53
+
},
54
+
"outputs": [],
55
+
"source": [
56
+
"import numpy as np\n",
57
+
"import matplotlib.pyplot as plt\n",
58
+
"\n",
59
+
"# Basic Plot Example\n",
60
+
"x = np.linspace(0, 10, 100)\n",
61
+
"y = np.sin(x)\n",
62
+
"\n",
63
+
"plt.plot(x, y)\n",
64
+
"plt.title('Sine Wave')\n",
65
+
"plt.xlabel('X axis')\n",
66
+
"plt.ylabel('Y axis')\n",
67
+
"plt.show()\n"
68
+
]
69
+
},
70
+
{
71
+
"cell_type": "markdown",
72
+
"id": "b9043cba",
73
+
"metadata": {
74
+
"id": "b9043cba"
75
+
},
76
+
"source": [
77
+
"## 2. Adding Multiple Plots\n",
78
+
"\n",
79
+
"Here, we will plot both a sine wave and a cosine wave on the same figure.\n"
80
+
]
81
+
},
82
+
{
83
+
"cell_type": "code",
84
+
"execution_count": null,
85
+
"id": "f00fe565",
86
+
"metadata": {
87
+
"id": "f00fe565"
88
+
},
89
+
"outputs": [],
90
+
"source": [
91
+
"y1 = np.sin(x)\n",
92
+
"y2 = np.cos(x)\n",
93
+
"\n",
94
+
"plt.plot(x, y1, label='Sine')\n",
95
+
"plt.plot(x, y2, label='Cosine')\n",
96
+
"plt.title('Sine and Cosine Waves')\n",
97
+
"plt.xlabel('X axis')\n",
98
+
"plt.ylabel('Y axis')\n",
99
+
"plt.legend()\n",
100
+
"plt.show()\n"
101
+
]
102
+
},
103
+
{
104
+
"cell_type": "markdown",
105
+
"id": "e658c049",
106
+
"metadata": {
107
+
"id": "e658c049"
108
+
},
109
+
"source": [
110
+
"## 3. Subplots\n",
111
+
"\n",
112
+
"In this example, we will create two subplots to display the sine and cosine waves separately.\n"
113
+
]
114
+
},
115
+
{
116
+
"cell_type": "code",
117
+
"execution_count": null,
118
+
"id": "d2a591fd",
119
+
"metadata": {
120
+
"id": "d2a591fd"
121
+
},
122
+
"outputs": [],
123
+
"source": [
124
+
"fig, axs = plt.subplots(2)\n",
125
+
"\n",
126
+
"axs[0].plot(x, y1)\n",
127
+
"axs[0].set_title('Sine Wave')\n",
128
+
"\n",
129
+
"axs[1].plot(x, y2, 'r')\n",
130
+
"axs[1].set_title('Cosine Wave')\n",
131
+
"\n",
132
+
"plt.tight_layout()\n",
133
+
"plt.show()\n"
134
+
]
135
+
},
136
+
{
137
+
"cell_type": "markdown",
138
+
"id": "445f62e1",
139
+
"metadata": {
140
+
"id": "445f62e1"
141
+
},
142
+
"source": [
143
+
"## 4. Customizing Plots\n",
144
+
"\n",
145
+
"You can customize the appearance of your plots by changing colors, markers, and line styles.\n"
146
+
]
147
+
},
148
+
{
149
+
"cell_type": "code",
150
+
"execution_count": null,
151
+
"id": "78aa542e",
152
+
"metadata": {
153
+
"id": "78aa542e"
154
+
},
155
+
"outputs": [],
156
+
"source": [
157
+
"plt.plot(x, y, color='green', linestyle='--', marker='o')\n",
158
+
"plt.title('Customized Sine Wave')\n",
159
+
"plt.xlabel('X axis')\n",
160
+
"plt.ylabel('Y axis')\n",
161
+
"plt.show()\n"
162
+
]
163
+
},
164
+
{
165
+
"cell_type": "markdown",
166
+
"id": "5ced6964",
167
+
"metadata": {
168
+
"id": "5ced6964"
169
+
},
170
+
"source": [
171
+
"## 5. Saving a Plot\n",
172
+
"\n",
173
+
"Instead of showing the plot, you can save it to a file using `plt.savefig()`.\n"
174
+
]
175
+
},
176
+
{
177
+
"cell_type": "code",
178
+
"execution_count": null,
179
+
"id": "2513d3d6",
180
+
"metadata": {
181
+
"id": "2513d3d6"
182
+
},
183
+
"outputs": [],
184
+
"source": [
185
+
"plt.plot(x, y)\n",
186
+
"plt.title('Sine Wave')\n",
187
+
"plt.savefig('sine_wave.png')\n"
188
+
]
189
+
},
190
+
{
191
+
"cell_type": "markdown",
192
+
"id": "d26d08e1",
193
+
"metadata": {
194
+
"id": "d26d08e1"
195
+
},
196
+
"source": [
197
+
"## 6. Finite Element Visualization Example\n",
198
+
"\n",
199
+
"Here is a basic example of visualizing displacements in a finite element model by plotting both the undeformed and deformed shapes of a simple 2D structure.\n"
0 commit comments