matplot 基本用法

运行环境: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.xlim((-1,2))//定义x轴的显示范围
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()

程序显示效果如下图




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值