Ubuntu安装Matplotlib

分类: python |
1.安装命令 sudo
apt-get install python-numpy
sudo apt-get
install python-matplotlib
2.画柱状图
import
matplotlib.pyplot as plt def bar_chart_generator(): l=[1,2,3,4,5]
h=[20,14,38,27,9] w=[0.1,0.2,0.3,0.4,0.5] b=[1,2,3,4,5]
fig=plt.figure() ax=fig.add_subplot(111) rects=ax.bar(l,h,w,b)
plt.show() bar_chart_generator()
2.写个例子:
import
matplotlib.pyplot as plt
import numpy
as np
#To draw y
=x^2(-3<=x<=3)
x =
np.arange(-3,3.5,0.5)
y = [ele**2
for ele in x]
z = [ele *2
for ele in x]
fig =
plt.figure(1)
ax =
fig.add_subplot(211)
line1 =
ax.plot(x,y,'ro-')
ax =
fig.add_subplot(212)
line2 =
ax.plot(x,z,'g-') plt.show()
效果:
http://s11/mw690/001UTbqlzy6FZzG2nSG8a&690
3.再写个例子
# -*- coding: utf-8 -*-
import numpy as np
import pylab as pl
from StringIO import StringIO
data_str = """
2012-04-01_02 68
2012-04-01_05 70
2012-04-01_08 69
2012-04-01_11 71
2012-04-01_14 72
2012-04-01_20 70
2012-04-02_02 71
2012-04-02_05 70
2012-04-02_08 69
2012-04-02_11 71
2012-04-02_14 69
2012-04-02_20 71
2012-04-03_02 74
2012-04-03_05 73
2012-04-03_08 77
2012-04-03_11 70
2012-04-03_14 71
2012-04-03_20 70
2012-04-04_02 70
2012-04-04_05 72
2012-04-04_08 72
2012-04-04_11 69
2012-04-04_14 71
2012-04-04_20 69
2012-04-05_02 75
"""
data = np.loadtxt(StringIO(data_str),
dtype=np.dtype([("t", "S13"),("v", float)]))
datestr = np.char.replace(data["t"], "_", " ")
t = pl.datestr2num(datestr)
v = data["v"]
pl.plot_date(t, v, fmt="-o")
pl.subplots_adjust(bottom=0.3)
ax = pl.gca()
ax.fmt_xdata = pl.DateFormatter('%Y-%m-%d %H:%M:%S')
pl.xticks(rotation=90)
pl.xticks(t, datestr) # 如果以数据点为刻度,则注释掉这一行
ax.xaxis.set_major_formatter(pl.DateFormatter('%Y-%m-%d %H'))
pl.grid()
pl.show()
s=StringIO.StrngIO([buf])此实例类似于open方法,不同的是它并不会在硬盘中生成文件,而只寄存在缓冲区;可选参数buf是一个str或unicode类型。它将会与其他后续写入的数据存放在一起。
效果: