Skip to content

Commit

Permalink
Created using Colab
Browse files Browse the repository at this point in the history
  • Loading branch information
GEORMC committed Oct 3, 2024
1 parent 1ea06f6 commit 6ef74dd
Showing 1 changed file with 232 additions and 0 deletions.
232 changes: 232 additions & 0 deletions python_plotting_with_descriptions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<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>"
]
},
{
"cell_type": "markdown",
"id": "b3809745",
"metadata": {
"id": "b3809745"
},
"source": [
"# Python Plotting Examples for Finite Element Course\n",
"\n",
"This notebook demonstrates how to create different types of plots using Python's `matplotlib` library.\n",
"We will cover the following:\n",
"1. Basic Plot\n",
"2. Adding Multiple Plots\n",
"3. Subplots\n",
"4. Customizing Plots\n",
"5. Saving a Plot\n",
"6. Finite Element Visualization Example\n",
"\n",
"Each section includes code and explanation of the corresponding plot.\n"
]
},
{
"cell_type": "markdown",
"id": "bea5d990",
"metadata": {
"id": "bea5d990"
},
"source": [
"## 1. Basic Plot Example\n",
"\n",
"This is a simple example of how to create a line plot using `matplotlib`.\n",
"We will plot a sine wave over a range of values.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "423c9f77",
"metadata": {
"id": "423c9f77"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Basic Plot Example\n",
"x = np.linspace(0, 10, 100)\n",
"y = np.sin(x)\n",
"\n",
"plt.plot(x, y)\n",
"plt.title('Sine Wave')\n",
"plt.xlabel('X axis')\n",
"plt.ylabel('Y axis')\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"id": "b9043cba",
"metadata": {
"id": "b9043cba"
},
"source": [
"## 2. Adding Multiple Plots\n",
"\n",
"Here, we will plot both a sine wave and a cosine wave on the same figure.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f00fe565",
"metadata": {
"id": "f00fe565"
},
"outputs": [],
"source": [
"y1 = np.sin(x)\n",
"y2 = np.cos(x)\n",
"\n",
"plt.plot(x, y1, label='Sine')\n",
"plt.plot(x, y2, label='Cosine')\n",
"plt.title('Sine and Cosine Waves')\n",
"plt.xlabel('X axis')\n",
"plt.ylabel('Y axis')\n",
"plt.legend()\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"id": "e658c049",
"metadata": {
"id": "e658c049"
},
"source": [
"## 3. Subplots\n",
"\n",
"In this example, we will create two subplots to display the sine and cosine waves separately.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2a591fd",
"metadata": {
"id": "d2a591fd"
},
"outputs": [],
"source": [
"fig, axs = plt.subplots(2)\n",
"\n",
"axs[0].plot(x, y1)\n",
"axs[0].set_title('Sine Wave')\n",
"\n",
"axs[1].plot(x, y2, 'r')\n",
"axs[1].set_title('Cosine Wave')\n",
"\n",
"plt.tight_layout()\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"id": "445f62e1",
"metadata": {
"id": "445f62e1"
},
"source": [
"## 4. Customizing Plots\n",
"\n",
"You can customize the appearance of your plots by changing colors, markers, and line styles.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78aa542e",
"metadata": {
"id": "78aa542e"
},
"outputs": [],
"source": [
"plt.plot(x, y, color='green', linestyle='--', marker='o')\n",
"plt.title('Customized Sine Wave')\n",
"plt.xlabel('X axis')\n",
"plt.ylabel('Y axis')\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"id": "5ced6964",
"metadata": {
"id": "5ced6964"
},
"source": [
"## 5. Saving a Plot\n",
"\n",
"Instead of showing the plot, you can save it to a file using `plt.savefig()`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2513d3d6",
"metadata": {
"id": "2513d3d6"
},
"outputs": [],
"source": [
"plt.plot(x, y)\n",
"plt.title('Sine Wave')\n",
"plt.savefig('sine_wave.png')\n"
]
},
{
"cell_type": "markdown",
"id": "d26d08e1",
"metadata": {
"id": "d26d08e1"
},
"source": [
"## 6. Finite Element Visualization Example\n",
"\n",
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3af2977b",
"metadata": {
"id": "3af2977b"
},
"outputs": [],
"source": [
"nodes = np.array([[0, 0], [1, 0], [1, 1], [0, 1]])\n",
"displacements = np.array([[0, 0], [0.1, 0], [0.1, 0.1], [0, 0.1]])\n",
"\n",
"plt.plot(nodes[:, 0], nodes[:, 1], 'bo-', label='Undeformed')\n",
"plt.plot(nodes[:, 0] + displacements[:, 0], nodes[:, 1] + displacements[:, 1], 'ro-', label='Deformed')\n",
"plt.title('Finite Element Displacement Plot')\n",
"plt.xlabel('X Coordinate')\n",
"plt.ylabel('Y Coordinate')\n",
"plt.legend()\n",
"plt.show()\n"
]
}
],
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 6ef74dd

Please sign in to comment.