Prev: Customizing the plot | Next: Multiple plots
- Bar chart
- Pie chart
- Histogram
- Box-and-Whisker plot
- Scatter plot
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# preparation
N = 5
barloc = np.arange(N) # the bar locations
width = 0.25 # the width of the bars
plt.xticks(barloc+width, ('SP1', 'SP2', 'SP3', 'SP4', 'SP5'))
plt.yticks(np.arange(0, 100, 10))
# frequencies on island-1
turt = np.array([10, 30, 140, 5, 25]) # measurements of turtle species on island
turtfreq = turt*100/sum(turt) # frequencies of turtle species on island (bar height)
# frequencies on another island-2
turt2 = np.array([25, 15, 70, 45, 18]) # measurements of turtle species on island-2
turtfreq2 = turt2*100/sum(turt2) # frequencies of turtle species on island-2 (bar height)
# plotting
b1 = plt.bar(barloc, turtfreq, width, color='c', yerr=2)
b2 = plt.bar(barloc+width, turtfreq2, width, color='r', yerr=1.5)
# legend
plt.ylabel('% Frequencies')
plt.title('Turtle Species Frequency Distribution')
plt.legend((b1[0], b2[0]), ('Island-1', 'Island-2'))
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# plotting
turt = np.array([10, 30, 140, 5, 25])
turtfreq = turt/sum(turt)
p1 = plt.pie(turtfreq, explode=[0,0,0.25,0,0], labels=['SP1','SP2','SP3','SP4','SP5'],
colors=('w', 'g', 'y', 'b', 'm'), autopct='%.2f', shadow=True)
# legend
plt.title('Turtle Species Frequency Distribution')
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
%matplotlib inline
# setting up an ad-hoc distribution for temprature values
n = 100 # number of measurements
mn = 21.5 # the mean of the temperature distribution
std = 2.5 # the standard deviation of the temperature distribution
temp = std * np.random.randn(n) + mn # 'temp' is a numpy array with n values
n, bins, patches = plt.hist(temp, color='cyan', alpha=0.5)
# n: the value in each bin
# bins: the intervals in which the histogram is divided; default = 10
# alpha: opacity of the plot
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
%matplotlib inline
# setting up an ad-hoc distribution for temprature values
# normally the 'temp' array would be filled in with data read from external file
n = 100 # number of measurements
mn = 21.5 # the mean of the temperature distribution
std = 2.5 # the standard deviation of the temperature distribution
temp = std * np.random.randn(n) + mn # 'temp' is now a numpy array with n values
tbins = 20
n, bins, patches = plt.hist(temp, tbins, normed=True, color='green', alpha=0.5)
# tbins: we set a specific number of bins
# normed=True: now bin heights are normalized (area = integral of the histogram equals 1).
# Labels
plt.xlabel('Temperature')
plt.ylabel('Probability')
plt.title('Temperature measurement')
# Normal distro
y = mlab.normpdf(bins, mn, std)
plt.ylim(0,0.20)
plt.plot(bins, y, 'r--')
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# np.random.seed(937)
# data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
# labels = list('ABCD')
# fs = 10 # fontsize
n = 100 # number of measurements
mn = 21.5 # the mean of the temperature distribution
std = 2.5 # the standard deviation of the temperature distribution
temp = std * np.random.randn(n) + mn # 'temp' is now a numpy array with n values
n = 100
mn = 24.5
std = 2.0
temp2 = std * np.random.randn(n) + mn
#temps = np.array([temp, temp2])
list2 = list(temp2)
list1 = list(temp)
temps = [list1,list2]
di = plt.boxplot(temps)
plt.ylim(15,30)
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
N=50
b1 = 0.2
b2 = -0.2
x = 30*np.random.sample(N)
y1 = b1*x+np.random.randn(N)
y2 = b2*x+np.random.randn(N)
plt.scatter(x, y1, color='blue', alpha=0.5)
plt.scatter(x, y2, color='red', alpha=0.5)
. Free learning material
. See full copyright and disclaimer notice