python 画图常用

饼图

# 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()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值