运行环境:ubuntu 14.04 python 2.7
需要安装numpy 和 matplotlib第三方库
sudo apt-get install python-pip//安装pip
sudo pip install --upgrade pip //更新下pip
sudo pip install numpy//安装numpy
sudo pip install matplotlib//安装matplotlib
自己学习总结下matplot基本用法,可以用于的显示需要的数据
为了可以在python代码里可以使用中文注释,需要使用utf8中文编码
#coding:utf8
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1,2,50)//产生 -1~2 一共50 个数据
y1 = 2*x +1
y2 = x**2
#plt.figure()//相当于定义个窗口windown 在显示多个绘图是需要定义figure 若一个绘图就可以省略,类似于opencv的namedwindow()函数
#plt.plot(x,#行坐标
# y1#列坐标
# )
#plt.figure(num = 3,figsize = (8,5))//num 参数 定义figure 标号 figsize定义 显示绘图的figure的大小
plt.figure()
plt.plot(x,y1,color = 'red',linewidth = 1.0,linestyle = '--')//其中的参数顾名思义color 定义颜色 linewidth定义线宽 linestyle定义线的类型
description | |
---|---|
‘-‘ | solid line style |
‘–‘ | dashed line style |
‘-.’ | dash-dot line style |
‘:’ | dotted line style |
‘.’ | point marker |
‘,’ | pixel marker |
‘o’ | circle marker |
‘v’ | triangle_down marker |
‘^’ | triangle_up marker |
‘<’ | triangle_left marker |
‘>’ | triangle_right marker |
‘1’ | tri_down marker |
‘2’ | tri_up marker |
‘3’ | tri_left marker |
‘4’ | tri_right marker |
‘s’ | square marker |
‘p’ | pentagon marker |
‘*’ | star marker |
‘h’ | hexagon1 marker |
‘H’ | hexagon2 marker |
‘+’ | plus marker |
‘x’ | x marker |
‘D’ | diamond marker |
‘d’ | thin_diamond marker |
‘ | ‘ |
‘_’ | hline marker |
color | |
---|---|
‘b’ | blue |
‘g’ | green |
‘r’ | red |
‘c’ | cyan |
‘m’ | magenta |
‘y’ | yellow |
‘k’ | black |
‘w’ | white |
plt.ylim((-2,3))//定义y轴的显示范围
plt.xlabel('x')//定义x轴显示的label
plt.ylabel('y')//定义y轴显示的label
new_ticks = np.linspace(-1,2,5)//定义x轴显示的范围以及步长
print new_ticks
plt.xticks(new_ticks)
plt.yticks([1,2,3],//定义y轴显示的坐标以及对应的字符表示
[r'$o\ n\ e$',r'$two\ \alpha$','three'])//\空格 转义字符需要前面使用r $ $可以显示字体的样式
##gca = get current axis
ax = plt.gca()//得到当前figure的四个坐标
ax.spines['right'].set_color('none')//把右边的坐标轴设置为空
ax.spines['top'].set_color('none')//把顶部的坐标轴设置为空
ax.xaxis.set_ticks_position('bottom')//x轴设置为底部
ax.yaxis.set_ticks_position('left')//y轴设置为左边
ax.spines['bottom'].set_position(('data',0))//设置x轴的初始位置
ax.spines['left'].set_position(('data',0))//设置y轴的初始位置
plt.plot(x,y2)
plt.show()
程序显示效果如下图