{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# matplotlib exercises"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q1: planetary positions\n",
"\n",
"The distances of the planets from the Sun (technically, their semi-major axes) are:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([0.39, 0.72, 1.00, 1.52, 5.20, 9.54, 19.22, 30.06, 39.48])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These are in units where the Earth-Sun distance is 1 (astronomical units).\n",
"\n",
"The corresponding periods of their orbits (how long they take to go once around the Sun) are, in years"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"P = np.array([0.24, 0.62, 1.00, 1.88, 11.86, 29.46, 84.01, 164.8, 248.09])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, the names of the planets corresponding to these are:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"names = [\"Mercury\", \"Venus\", \"Earth\", \"Mars\", \"Jupiter\", \"Saturn\", \n",
" \"Uranus\", \"Neptune\", \"Pluto\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(technically, pluto isn't a planet anymore, but we still love it :)\n",
"\n",
" * Plot as points, the periods vs. distances for each planet on a log-log plot.\n",
"\n",
" * Write the name of the planet next to the point for that planet on the plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q2: drawing a circle\n",
"\n",
"For an angle $\\theta$ in the range $\\theta \\in [0, 2\\pi]$, the polar equations of a circle of radius $R$ are:\n",
"$$\n",
"x = R\\cos(\\theta)\n",
"$$\n",
"$$ \n",
"y = R\\sin(\\theta)\n",
"$$\n",
"\n",
"We want to draw a circle. \n",
"\n",
" * Create an array to hold the theta values—the more we use, the smoother the circle will be\n",
" * Create `x` and `y` arrays from `theta` for your choice of $R$\n",
" * Plot `y` vs. `x`\n",
" \n",
"Now, look up the matplotlib `fill()` function, and draw a circle filled in with a solid color."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q3: Circles, circles, circles...\n",
"\n",
"Generalize your circle drawing commands to produce a function, \n",
"```\n",
"draw_circle(x0, y0, R, color)\n",
"```\n",
"that draws the circle. Here, `(x0, y0)` is the center of the circle, `R` is the radius, and `color` is the color of the circle. \n",
"\n",
"Now randomly draw 10 circles at different locations, with random radii, and random colors on the same plot."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q4: Climate\n",
"\n",
"Download the data file of global surface air temperature averages from here:\n",
"https://raw.githubusercontent.com/sbu-python-summer/python-tutorial/master/day-4/nasa-giss.txt\n",
"\n",
"(this data comes from: https://data.giss.nasa.gov/gistemp/graphs/)\n",
"\n",
"There are 3 columns here: the year, the temperature change, and a smoothed representation of the temperature change. \n",
"\n",
" * Read in this data using `np.loadtxt()`. \n",
" * Plot as a line the smoothed representation of the temperature changes. \n",
" * Plot as points the temperature change (no smoothing). Color the points blue if they are < 0 and color them red if they are >= 0\n",
" \n",
"You might find the NumPy `where()` function useful."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q5: subplots\n",
"\n",
"matplotlib has a number of ways to create multiple axes in a figure -- look at `plt.subplot()` (http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot)\n",
"\n",
"Create an `x` array using NumPy with a number of points, spanning from $[0, 2\\pi]$. \n",
"\n",
"Create 3 axes vertically, and do the following:\n",
"\n",
"* Define a new numpy array `f` initialized to a function of your choice.\n",
"* Plot f in the top axes\n",
"* Compute a numerical derivative of `f`,\n",
" $$ f' = \\frac{f_{i+1} - f_i}{\\Delta x}$$\n",
" and plot this in the middle axes\n",
"* Do this again, this time on $f'$ to compute the second derivative and plot that in the bottom axes\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q6: frequent words plotting\n",
"\n",
"In this exercise, we will read the file with the transcription of _Star Trek TOS, Shore Leave_ and calculate the amount of time each word was found. We will then plot the 25 most frequent words and label the plot.\n",
"\n",
"### 6.1 Read the file and create the dictionaty {'word':count}\n",
"\n",
" * Open the `shore_leave.txt`\n",
" * Create the dictionary of the form {'word':count}, where `count` shows the amount of times the word was found in the text. Remember to get rid of the punctuation (\".\" and \",\") and to ensure that all words are lowercase"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"f = open(\"shore_leave.txt\", \"r\")\n",
"\n",
"for line in f:\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Plot 25 most frequent words\n",
"\n",
"Plot a labelled bar chart of the most frequent 25 words with their frequencies."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# your code here"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Q7: Mandelbrot set\n",
"\n",
"The mandelbrot set is defined such that $z_{k+1} = z_k^2 + c$\n",
"remains bounded, which is usually taken as $|z_{k+1}| <= 2$\n",
"where $c$ is a complex number and we start with $z_0 = 0$\n",
"\n",
"We want to consider a range of $c$, as complex numbers $c = x + iy$,\n",
"where $-2 < x < 2$ and $-2 < y < 2$.\n",
"\n",
"For each $c$, identify its position on a Cartesian grid as $(x,y)$ and \n",
"assign a value $N$ that is the number of iterations, $k$, required for $|z_{k+1}|$ to become greater than $2$.\n",
"\n",
"The plot of this function is called the Mandelbrot set.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}