# Python Challenge 1 - Can computers be used to create art?

## Python Challenge 1 - Can a computer create art?

This is of course a challenging question, as art has diﬀerent meanings in diﬀerent contexts. Whatever your thoughts on that question, there is no doubt that computers can be used to create shapes and ﬁgures that can change a mood or feeling when viewed. This ﬁrst challenge looks at creating pictures using the numpy and matplotlib packages, which will be used throughout this series.

One particular way of creating interesting geometric shapes is by using parametric equations. These are a particular way of describing a curve using a ‘parameter’, t. For example, one way of plotting a circle is by using the equations

x = cos(t)
y = sin(t)

Values of x and y are found at a range of values of t, and then a computer plots lines between the pairs of points that are found.

In this challenge you create the pictures shown below by plotting parametric equations. As well as the circle, we will also plot a Butterﬂy Curve (left) and Fermat Spiral (right).  ## Target:

Write one page of Python that will:

1. Plot a circle, using the parametric equation above.
2. Find the parametric equation for Fermat’s Spiral (online), and therefore plot the spiral.
3. Do the same for the Butterﬂy Curve.

## Extensions:

• Specify and plot 5 circles with diﬀerent sizes and diﬀerent colours.
• For the Butterﬂy Curve, change the script so that the colour of each line segment changes with the x-coordinate.
• For Fermat’s spiral, plot the curve so that the colour gets lighter with the radius of each line segment.

#### Packages

All challenges will use numpy and matplotlib, and these are the only two packages required to make the ﬁgures shown above. Once your code is working, the colormap and ListCollection modules may be useful for speeding up code or changing the colour of ﬁgures.

## Python Challenge 1 - concertina

### Hints for Python Challenge 1

• If you are having diﬃculties plotting a circle, try plotting a simple function ﬁrst and build up to more complex functions. For example, maybe try plotting the straight line y = 2x + 1 ﬁrst, or the quadratic y = x2 − 1.

• When you are plotting the circle, the parameter t needs to be chosen. Usually this is chosen as an array. Such a array can be set up using the numpy.linspace command.

• The equation describing Fermat’s Spiral is given by

y = ±t1/2 sin(t)
x = ±t1/2 cos(t)

(where ± means that one part of the spiral uses + and one uses −). Can you work out how to plot the curve using just one plot command?

• The equation describing the Butterﬂy Curve includes the exponential function. This function is coded within numpy as numpy.exp. Also, the number π can be obtained using numpy.pi.

#### Notes

In the figures plotted above, a number of Colormaps have been used to vary the colour of the line segments as a function of position. Different maps have different purposes - some are smoothly graded from light to dark, while others only appear as discrete colours.