Skip to content

Commit

Permalink
Add a notebook that shows how to plot shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdpiper committed Jul 28, 2023
1 parent 8a82a8d commit b9d1106
Showing 1 changed file with 168 additions and 0 deletions.
168 changes: 168 additions & 0 deletions lessons/python/7a_shapes.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "db296251-f903-49cb-a88d-41ae7a486826",
"metadata": {},
"source": [
"# Display a `Shape`"
]
},
{
"cell_type": "markdown",
"id": "442ba2bc-7d7d-4353-ad87-4a458f4ae385",
"metadata": {},
"source": [
"This notebook shows how to use Matplotlib to display a shape from the OOP lesson.\n",
"\n",
"Start with imports of Matplotlib and a shape from the OOP lesson; here, the `Rectangle`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "179c186d-95c4-4aa5-8bf5-700d014055cc",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"from shapes.rectangle import Rectangle"
]
},
{
"cell_type": "markdown",
"id": "cf25341c-ea78-4010-95b7-35fc01fa4578",
"metadata": {},
"source": [
"Create an instance of `Rectangle`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e227251-59f0-4036-b5b5-34a435cc66dc",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"rectangle = Rectangle(lower_left=(2.0, 1.0), width=2.0, height=3.0)"
]
},
{
"cell_type": "markdown",
"id": "19a0ebb5-31ba-4349-a1b6-43eba5ebf635",
"metadata": {},
"source": [
"Show the coordinates of the rectangle's vertices."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f1c4892-282c-4034-9fe9-4b3db141938a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"rectangle.x"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "324acca7-5549-4c08-a4ed-041604ae7167",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"rectangle.y"
]
},
{
"cell_type": "markdown",
"id": "bea83f02-c705-48f8-bd7c-86134cfdba79",
"metadata": {},
"source": [
"To close the polygon for plotting,\n",
"make new variables, `x` and `y`,\n",
"that repeat the first coordinate pair."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bf863b2f-ea7e-4d01-96f2-5d0bd43275ef",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"x = [*rectangle.x, rectangle.x[0]]\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14d1d252-d6a2-4e16-85b8-81930de96491",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"y = [*rectangle.y, rectangle.y[0]]\n",
"y"
]
},
{
"cell_type": "markdown",
"id": "5558ef85-1ea6-4036-9749-7b56d46db928",
"metadata": {},
"source": [
"Plot the result."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9bea1a1b-bb8e-4df1-9e8d-460cf6ab23c5",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"plt.figure()\n",
"plt.xlim(0, 5)\n",
"plt.ylim(0, 5)\n",
"plt.plot(x, y)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "CSDMS",
"language": "python",
"name": "csdms"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit b9d1106

Please sign in to comment.