matplotlib学习笔记 (1)
标签(空格分隔): matplotlib,python画图,pyqt用户界面
matplotlib画图
基础画图
import matplotlib.pyplot as plt
x=range(1,5)
plt.plot(x,[x1*3 for x1 in x])
plt.show()
or
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(1,5)
plt.plot(x,x*3)
plt.show()
这里 x=range(1,5)得到的结果是: x=[1,2,3,4] 是一个list 如果直接用 x*3得到的结果是: [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] 而 x=np.arange(1,5)得到的结果是: x=array([1, 2, 3, 4]) 是numpy里面的数组这种数据结构 所以可以直接用 x*3得到的结果是: array([ 3, 6, 9, 12]) list和转化np.array可以相互转化:
a=[1,2,3]
b=np.array(a)
c=list(b)
Grid,axes,labels,title,legends and save files
添加grid
plt.grid(True)
设置axis
plt.axis([0,4,0,8])
or
plt.axis(xmin=xx0,ymax=yy0)
or
plt.xlim([x0,x1])
plt.ylim([y0,y1])
添加labels
plt.xlabel(‘this is the x label’) plt.ylabel(‘this is the y label’)
添加title
plt.title('this is the title')
添加legend
plt.plot(x,x*3,label='mylegend')
plt.legend()
可以用plt.legend(loc=’best’)指定legend位置
保存图片文件
plt.savefig('figname.png')
一个综合的例子
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(1,5)
plt.plot(x,x*2,label='Normal')
plt.grid(True)
plt.title('my figure')
plt.xlabel('x label')
plt.legend(loc=(1,0))
plt.legend(loc='best')
plt.show()
一些高级特性
设置color
plt.plot(x,x*2,'y')
支持的颜色
color abbreviationcolor name
bblueccyanggreenkblackmmagentarredwwhiteyyellow
设置linestyle
plt.plot(y, '--', y+1, '-.', y+2, ':')
style abbreviationstyle
-solid line–dashed line-.dash-dot line:dotted line
设置数据点
import matplotlib.pyplot as plt
import numpy as np
y = np.arange(1, 3, 0.3)
plt.plot(y, 'cx--', y+1, 'mo:', y+2, 'kp-.');
plt.show()
其中x,o,p均是设置数据点形状的参数 参数很多,具体可查找,不再列举
设置x,y轴刻度
import matplotlib.pyplot as plt
x = [5, 3, 7, 2, 4, 1]
plt.plot(x);
plt.xticks(range(len(x)), ['a', 'b', 'c', 'd', 'e', 'f']);
plt.yticks(range(1, 8, 2));
locs,labels=plt.xticks()
plt.show()
运行的结果如图所示:
绘制统计图
histogram charts 直方图
import matplotlib.pyplot as plt
import numpy as np
y = np.random.randn(1000)
plt.hist(y);
plt.show()
运行的结果如下图所示: 通常俩说hist()函数用10 bins,但是我们可以设置hist()的参数。
plt.hist(y,25)
设置bins的数量为25
Error bar 误差图
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 4, 0.2)
y = np.exp(-x)
e1 = 0.1 * np.abs(np.random.randn(len(y)))
plt.errorbar(x, y, yerr=e1, fmt='.-');
plt.show()
Bar charts
import matplotlib.pyplot as plt
plt.bar([1, 2, 3], [3, 2, 5]);
plt.show()
其中[1,2,3]分别表示bar的左边界坐标,[3,2,5]分别表示bar的高度 运行的结果如下图: bar默认的宽度是0.8,当然我们也可以自己设置bar的宽度
import matplotlib.pyplot as plt
import numpy as np
dict={'A': 40, 'B': 70, 'C': 30, 'D': 85}
for i, key in enumerate(dict): plt.bar(i, dict[key]);
plt.xticks(np.arange(len(dict))+0.4, dict.keys());
plt.yticks(dict.values());
plt.show()
运行的结果如下:
Pie charts
import matplotlib.pyplot as plt
plt.figure(figsize=(8,8));
x = [45, 35, 20]
labels = ['Cats', 'Dogs', 'Fishes']
plt.pie(x, labels=labels);
plt.show()
运行的结果是:
import matplotlib.pyplot as plt
plt.figure(figsize=(7,7));
x = [4, 9, 21, 55, 30, 18]
labels = ['Swiss', 'Austria', 'Spain', 'Italy', 'France', 'Benelux']
explode = [0.2, 0.1, 0, 0, 0.1, 0]
plt.pie(x, labels=labels, explode=explode, autopct='%1.1f%%');
plt.show()
这个代码运行的结果是:
Scatter plots
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
y = np.random.randn(1000)
size = 50*np.random.randn(1000)
colors = np.random.rand(1000)
plt.scatter(x, y, s=size, c=colors);
plt.show()
运行的结果如下图所示:
下一节将学习matplotlib的高级功能