python22.sci2u.dk Active sci2u
Loading...
Login
Prefer latest booklet Prefer latest booklet Login
Link to booklet is copied to clipboard!
Saved to booklet!
Removed from booklet!
Started capturing to booklet.
Stopped capturing to booklet.
Preferring latest booklet.
No longer prefers latest booklet.
Entered edit mode.
Exited edit mode.
Edit mode

Click on the booklet title or a chapter title to edit (max. 30 characters)


How To's

How to get python How to plot points and lines How to use loops How to draw general shapes
4.1 Introduction
4.2 Reversing a NumPy array
4.3 Glue together NumPy arrays
4.4 Coloring between half-circles
How to draw curves How to use functions How to manipulate Polygons How to create animations How to do mathematical analysis How to fit How to go from SymPy to NumPy How to solve a single differential equation How to do coupled and 2nd order ODEs How to combine vectors and ODEs How to do ODE events Add Chapter..

How to draw general shapes

4.1 Introduction

This how-to will demonstrate how to draw curves and polygons of any shape in Python.
As an example, consider a half-circle. The points on a circle of radius can be parametrized in terms of an angle, :
where fulfills . In order to plot a half-circle, it suffices to draw a large number of small linear segments between points on the half-circle. To do so, we need to generate a range of angles, which is conveniently done with the linspace function in NumPy. linspace takes three arguments, a starting value, an ending value, and the number of evenly distributed values from the starting to the ending value. Here we ask for 10 values from 0 to :
theta = np.linspace(0,np.pi,10)
theta
array([0.        , 0.34906585, 0.6981317 , 1.04719755, 1.3962634 ,
       1.74532925, 2.0943951 , 2.44346095, 2.7925268 , 3.14159265])
A half-circle with may now be plotted with this piece of code:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

plt.rc('font', size=18)
fig, ax = plt.subplots()

theta = np.linspace(0,np.pi,10)
r = 2
x = r * np.cos(theta)
y = r * np.sin(theta)
ax.plot(x,y,linewidth=3,color='c')

ax.grid()
ax.set_aspect('equal')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
but it looks a bit raggy:

Figure 1

Introducing more values for remedies this, as seen here for the half-circle with radius :
theta = np.linspace(0,np.pi,100)

r = 1
x = r * np.cos(theta)
y = r * np.sin(theta)
ax.plot(x,y,linewidth=3,color='k')

ax.legend(['$r=1$','$r=2$'])
fig

Figure 2



4.2 Reversing a NumPy array

With two half-circles drawn, a neat application would be to color the region between them. This can be done with Matplotlibs fill function, where we provide the -points as one runs around the perimeter of the desired polygon. One problem with this is that if we provide points on the outer half-circle in anti-clockwise order, then we have to provide the points on the inner half-circle in clockwise order. Thus the -values must be supplied in opposite order. That can be done in several ways. A cryptic one:
theta[::-1]
array([3.14159265, 2.7925268 , 2.44346095, 2.0943951 , 1.74532925,
       1.3962634 , 1.04719755, 0.6981317 , 0.34906585, 0.        ])
and a more intuitive one, where the first two arguments of linspace are interchanged:
theta = np.linspace(np.pi,0,10)
theta
array([3.14159265, 2.7925268 , 2.44346095, 2.0943951 , 1.74532925,
       1.3962634 , 1.04719755, 0.6981317 , 0.34906585, 0.        ])
As you become more and more fluent in Python, you will probably prefer the former way (since it does not involve recalculating the values in the list), but while you are learning Python, the latter way is certainly fine.


4.3 Glue together NumPy arrays

Another thing we need to be able to do is to "glue" together the of - and -coordinates from the two half-circles. Consider two simple NumPy arrays:
a1 = np.linspace(0,2,3)
a2 = np.linspace(10,12,3)

a1,a2
(array([0., 1., 2.]), array([10., 11., 12.]))
If you try to glue together the NumPy arrays with the +-operator you get the elementwise sum:
a3 = a1 + a2
a3
array([10., 12., 14.])
which is very useful in many contexts. But to get a NumPy array of all elements within the two original NumPy arrays, you have to resort to the NumPy concatenate function:
a4 = np.concatenate((a1,a2))
a4
(don't miss the double set of parentheses), which gives:
array([ 0.,  1.,  2., 10., 11., 12.])


4.4 Coloring between half-circles

We now have all Python skills required to form a polygon that fills the space between two half-circles:
fig, ax = plt.subplots()

r = 2
theta = np.linspace(0,np.pi,100)
x1 = r * np.cos(theta)
y1 = r * np.sin(theta)

r = 1
theta = np.linspace(np.pi,0,100)
x2 = r * np.cos(theta)
y2 = r * np.sin(theta)

x = np.concatenate((x1,x2))
y = np.concatenate((y1,y2))

ax.fill(x,y,linewidth=3,facecolor='pink',edgecolor='k')

ax.grid()
ax.set_aspect('equal')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')

Figure 3



Sci2u Assignment: 820
Delete "How To's"?
Once deleted this booklet is gone forever!
Block is found in multiple booklets!

Choose which booklet to go to:

© 2019-2022 Uniblender ApS