inspired by Exploratory Computing with Python
In [1]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
In [ ]:
plt.plot([1, 2, 1, 2]); # ; to supress text-output
In [ ]:
plt.plot(np.array([1, 2, 1, 2]));
In [ ]:
x = np.linspace(-4, 10, 29)
print(x)
In [ ]:
a = 1
b = 1
c = -6
x = np.linspace(-4, 4, 100)
y = a * x ** 2 + b * x + c
plt.plot(x, y);
In [ ]:
plt.plot([2, 4, 6], [2, 4, 3], '-', linewidth=6, color='red');
In [ ]:
a5 = np.arange(5)
plt.plot(a5,a5**3,'ro');
In [ ]:
# find out more about supported parameters like 'color', 'linewidth'...
plt.plot?
Plot $y=(x+2)(x-3)(x+1)$ for $x$ going from $-5$ to $5$ using a dashed green line.
On the same figure, plot a blue circle for every point where $y$ equals zero. Set the size of the circles to 8.
Label the axes as 'x-axis' and 'y-axis'. Add the title 'y=(x+2)(x-3)(x+1)'
Check out the official matplotlib documentation and the output of plt.plot? for help.
In [15]:
# TODO
You are provided with 3 data files containing the mean montly temperature of Basel for 1900, 1950, 2000.
Year 1900 is already loaded, load also that one for 1950 and 2000.
Plot the temperature for each year against the number of the month (starting with 1 for January) all in a single graph. Add a legend by using the function plt.legend(['line1','line2']) (of course with more descriptive names).
Use plt.legend? and the matplotlib documentation to get help if you are stuck.
In [16]:
!ls ../../data/homog_mo*
In [17]:
!head -n 5 ../../data/homog_mo_BAS-1900.csv
In [18]:
basel_temperatures_1900 = np.genfromtxt('../../data/homog_mo_BAS-1900.csv', delimiter=',', skip_header=1) # Month -> mean Temperature
basel_temperatures_1900[:3]
Out[18]:
In [23]:
# TODO Plot the temperature for each year against the number of the month ...
Load the average monthly temperature 1900, 1950, 2000 for Basel. Create one plot with 3 graphs above each other using the subplot command (use plt.subplot?). On the top graph, plot the temperature for 1900, next 1950, next 2000.
Label the ticks on the horizontal axis as 'jan', 'feb', 'mar', etc., rather than 0,1,2,etc. Use plt.xticks? to find out how. Add legends, axes labels.
In [24]:
# TODO
4.1 now with using the subplots command (use plt.subplots?, also check plt.axes?). On the top graph, plot the temperature for 1900, next 1950, next 2000.
Label the ticks on the horizontal axis as 'jan', 'feb', 'mar', etc., rather than 0,1,2,etc. Use plt.xticks? to find out how. Add legends, axes labels.
In [25]:
# TODO
At the 2012 London Olympics, the top ten countries with gold medals were
['USA', 'CHN', 'GBR', 'RUS', 'KOR', 'GER', 'FRA', 'ITA', 'HUN', 'AUS', 'OTHER'].
They received
[46, 38, 29, 24, 13, 11, 11, 8, 8, 7, 107] gold medals.
Make a pie chart (type plt.pie? or go to the pie charts in the matplotlib gallery) of the top 10 gold medal winners plus the others at the London Olympics.
Take a look in the Matplotlib Gallery for further samples.
In [27]:
# TODO