# 绘图: Python matplotlib简介

matplotlib是基于numpy的一套Python工具包。这个包提供了丰富的数据绘图工具，主要用于绘制一些统计图形。你可以找到很多各式各样的例子

### 数据

USA        15094025
China      11299967
India       4457784
Japan       4440376
Germany     3099080
Russia      2383402
Brazil      2293954
UK          2260803
France      2217900
Italy       1846950                                                                                                                                                                                                                                 

### 饼图

# 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   = []
for line in file('../data/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()


### 条形图

"""
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
import numpy as np
# quants: GDP
# labels: country name
labels   = []
quants   = []
for line in file('../data/major_country_gdp'):
info = line.split()
labels.append(info[0])
quants.append(float(info[1]))

width = 0.4
ind = np.linspace(0.5,9.5,10)
# make a square figure
fig = plt.figure(1, figsize=(12,6))
# Bar Plot
ax.bar(ind-width/2,quants,width,color='coral')

# Set the ticks on x-axis
ax.set_xticks(ind)
ax.set_xticklabels(labels)
# labels
ax.set_xlabel('Country')
ax.set_ylabel('GDP (Billion US dollar)')
# title
ax.set_title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
plt.show()

### 总结

#### 技术推动进步，分享促进社区。

posted @ 2012-09-17 22:40  Vamei  阅读(30314)  评论(5编辑  收藏  举报