Matplotlib库的基本操作

基本

对于基本的使用来说,明确当前的figure即可
直接使用plt.plot进行画线,就会对当前figure进行修改
plt.title
plt.setp
plt.xlabel
plt.ylabel
plt.title
plt.text
plt.grid
同理,都是对当前的“图”进行操作

还可以使用plt.subplot分隔子图,使用之后当前plt操作的作用域就是当前子图(很自然)
对于基本使用,上面的方法思想够用了


但要明确一个重要思想是“axes” ,即轴域的概念
可使用gca(get current axes)查看当前轴域
这就是传说中的作用的figure的区域
对于没有subplot的figure,默认是一个轴域,且坐标位置也是默认的
而subplot实际上是axes的特例(一个特殊的轴域)
所以,你可以设置任何位置和大小的axes
并分别对其进行操作
这样思路会更完备(面向对象的思想)

关于axes对象的操作之后补上

下面是一些基本的操作

代码示例

#基础知识
from matplotlib import pyplot as plt  
import numpy as np
from pylab import *


plt.figure()
plt.plot([1,2,3,4,4,3,2,1])#以元素下标为横坐标  形成的点连线
plt.ylabel("some numbers")

plt.plot([1,2,3],[3,2,-1],'r')#提供横纵坐标,且为红色

plt.plot([1,2,3],[3,2,-1],'r*')#提供横纵坐标,指定了点的形状 就不连线了


plt.figure()#开启新的图像
t=np.arange(0,5,0.2)#使用np中的方法
plt.plot(t,t,'r-',t,t**2,'bs',t,t**3,'g^')
#s为方块  ^为三角形  红、蓝、绿


plt.figure()
x=np.arange(0,100,1)
y=np.sqrt(x)
#显然,下面有两条线
line1,line2=plt.plot(x,y,"r",x,np.cos(y),"r",linewidth=5.0)
#linewidth是线的宽度 所有如果用点表示 则该参数无效
#有了线这个对象(Line2D),就可以设置进一步的格式
line1.set_antialiased(False)
plt.setp(line2,color='b')#又被修改为蓝色


plt.figure()
def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t)

t1=np.arange(0,10,0.1)
t2=np.arange(0,10,0.1)

plt.subplot(221)#取当前图的子图  #小于10的编号,不用逗号  2*2 第一块
plt.plot(t1,f(t1),'b',t2,f(t2),'*',linewidth=0.8)
#一个使用点  一个使用线

plt.subplot(223)#取当前图的子图   第三个 先数行 
plt.plot(t1,t1**2.02,'b',t2**1.20,'g')
plt.title('test subplot')



plt.figure()
# 直方图
'''
def hist(x, bins=None, range=None, density=None, weights=None, cumulative=False,
         bottom=None, histtype='bar', align='mid', orientation='vertical',
         rwidth=None, log=False, color=None, label=None, stacked=False,
         normed=None, hold=None, data=None, **kwargs):
'''
#正态分布
mu, sigma = 100, 15
#x是取值
x = mu + sigma * np.random.randn(1000000)#  ~N(0,1)
#根据x的范围  将其分成bins份   统计每一份出现的个数 画出hist图
n, bins, patches=plt.hist(x, 100, color='g', alpha=0.8)#alpha是透明度  normed使得纵坐标是概率
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.grid(True)
print(n)#纵坐标的值
print(bins)#每一个bin的左边界(开始处)
binbin=[]

for i in range(len(bins)-1):
    binbin.append((bins[i]+bins[i+1])/2)
print(binbin)#横坐标的新值  取相邻两个的中点

#print(list(patches))  rectangles对象  可迭代  具体不知
plt.plot(binbin,n,'r--')


savefig("test")
show()#展示内存中的所有图像

最后一个figure

img

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值