python常用函数图片_Python matplotlib 常用画图函数

(以下展示均为先图片后代码的形式。)

1d1f3b2fe952

>>> import matplotlib.pyplot as plt

>>> import numpy as np

>>> x = np.linspace(-1.,1.,50)

>>> y = x**2 + 1

>>> plt.plot(x,y)

>>> plt.show()

—————————————————————————————

多窗口(多函数)

1d1f3b2fe952

1d1f3b2fe952

>>> plt.figure('first window')

>>> y1 = np.sqrt(-x**2 + 1)

>>> plt.plot(x,y1)

>>> plt.figure('second window')

>>> y2 = np.sqrt(x**2 + 1)

>>> y3 = x**3 + 1

>>> plt.plot(x,y2)

>>> plt.plot(x,y3)

>>> plt.show()

—————————————————————————————

更多例子

1d1f3b2fe952

>>> plt.plot(x,y1,'g-',x,y2,'b--',x,y3,'r.')

>>> plt.show()

—————————————————————————————

1d1f3b2fe952

>>> plt.figure('graph1')

>>> plt.xlabel('x轴')

>>> plt.ylabel('y轴')

>>> y = 2**x - 3

>>> plt.plot(x,y,'r*')

>>> plt.show()

—————————————————————————————

1d1f3b2fe952

>>> plt.xlim((-1,1))  #设置坐标轴初次显示的范围

(-1,1)

>>> plt.ylim((1,2))

(1,2)

>>> plt.xlabel('i\'m x.')  #设置坐标轴名称

>>> plt.ylabel('i\'m y.')

>>> plt.xticks([-1,-0.5,0,0.5,1])    #设置x的显示步长

([,,,,],)

>>> plt.yticks([1,1.2,2],['bad','normal','good'])

([,,],)

>>> y1 = x + 1.5

>>> y2 = x**2 + 1

>>> plt.plot(x,y1,'go',x,y2,'b--')

>>> plt.show()

—————————————————————————————

1d1f3b2fe952

>>> plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--',label='function1')

>>> plt.plot(x,y2,color='green',linewidth=1.0,linestyle='-',label='function2')

>>> plt.legend(loc='lower right')    # loc可以为‘best’:自动找一个数据少的地方显示

>>> plt.show()

—————————————————————————————

1d1f3b2fe952

>>> x = np.random.randint(1,50,size=(50,))

>>> y1 = np.random.randint(1,50,size=(50,))

>>> y2 = np.random.randint(1,50,size=(50,))

>>> plt.xticks(())

>>> plt.yticks(())

>>> plt.scatter(x,y1,c='red')    #画散点图

>>> plt.scatter(x,y2,c='blue')

>>> plt.show()

—————————————————————————————

### ‘图像更新’

plt.ion()  #打开交互模式

plt.plot(x,y)  #显示图像

plt.pause(0.01)  #暂停功能

plt.clf()  #清除当前的Figure对象

plt.cla()  #清除当前的Axes对象

plt.ioff()  #关闭交互模式

以上功能在for循环中可实现图像(x,y)更新

—————————————————————————————

1d1f3b2fe952

>>> x=np.array(np.arange(1,2,0.01))

>>> y=np.array(np.arange(1,2,0.01))

>>> xx,yy = np.meshgrid(x,y)#将x、y分别往纵向和横向扩展成2d数组

>>> xx

array([[1.  ,  1.01,  1.02,...,  1.97,  1.98,  1.99],

[1.  ,  1.01,  1.02,...,  1.97,  1.98,  1.99],

[1.  ,  1.01,  1.02,...,  1.97,  1.98,  1.99],

...,

[1.  ,  1.01,  1.02,...,  1.97,  1.98,  1.99],

[1.  ,  1.01,  1.02,...,  1.97,  1.98,  1.99],

[1.  ,  1.01,  1.02,...,  1.97,  1.98,  1.99]])

>>> yy

array([[1.  ,  1.  ,  1.  ,...,  1.  ,  1.  ,  1.  ],

[1.01,  1.01,  1.01,...,  1.01,  1.01,  1.01],

[1.02,  1.02,  1.02,...,  1.02,  1.02,  1.02],

...,

[1.97,  1.97,  1.97,...,  1.97,  1.97,  1.97],

[1.98,  1.98,  1.98,...,  1.98,  1.98,  1.98],

[1.99,  1.99,  1.99,...,  1.99,  1.99,  1.99]])

>>> z=xx*yy < 2  #定义第三维的高度:等于1 or 0

>>> plt.contourf(xx,yy,z,alpha=0.4)#创建等高图对象

>>> plt.show()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值