饼图
# Make a pie chart
# This script is written by Vamei, http://www.cnblogs.com/vamei
# you may freely use it.
import matplotlib.pyplot as plt
# quants: GDP
# labels: country name
labels = []
quants = []
# Read data
for line in file('major_country_gdp'):
info = line.split()
labels.append(info[0])
quants.append(float(info[1]))
# make a square figure
plt.figure(1, figsize=(6,6))
# For China, make the piece explode a bit
def explode(label, target='China'):
if label == target: return 0.1
else: return 0
expl = map(explode,labels)
# Colors used. Recycle if not enough.
colors = ["pink","coral","yellow","orange"]
# Pie Plot
# autopct: format of "percent" string;
plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True)
plt.title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
plt.show()
直方图
import numpy as np
import matplotlib.pyplot as plt
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
子图
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'g')
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
#plt.show()
plt.figure(2)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'g',label='f1')
plt.plot(t2, np.cos(2*np.pi*t2), 'r--',label='f2')
plt.legend(shadow=True)
plt.show()