numpy数组函数操作 numpy统计函数 Matplotlib图表绘制 Matplotlib绘表函数

import numpy as np
#desize;将原数组形状修改为新形状:如何元素个数不够,则重复遍历,如果个数超了,则舍弃后面的元素
a=np.arange(6).reshape(2,3)
a1=np.resize(a,(3,4))
print(a1)
a2=np.resize(a,(2,2))
print(a2)

#append:
a=np.arange(6).reshape(2,3)
#如果没有指定axis或axis=none,则添加返回一维数组
a1=np.append(a,[[1,2,3]])
print(a1)
#axis=0则按行添加,添加的value的维度要和原数组的维度一致
a2=np.append(a,[[1,2,3]],axis=0)
print(a2)
##axis=1则按列添加,添加的value的维度要和原数组的维度一致
a3=np.append(a,[[1,2,3],[1,2,3]],axis=1)
print(a3)

#insert:在数组指定位置添加元素值
a=np.arange(6).reshape(2,3)
#如果axis=None或者未指定,则按照位置添加元素并返回一维数组
a1=np.insert(a,1,[6])
print(a1)
#如果axis=0:按行在指定位置插入,如果插入的value维度为1,则自动广播
a2=np.insert(a,1,[6],axis=0)
print(a2)
#如果axis=1:按行在指定位置插入,如果插入的value维度为1,则自动广播
a3=np.insert(a,1,[6],axis=1)
print(a3)
# #如果插入的value形状和原数组的形状不一致,则抛异常
# a4=np.insert(a,1,[6,7,8],axis=1)
# print(a4)

#delete:删除指定位置的元素
#一维数组:
a=np.arange(6).reshape(2,3)
print(a)
a1=np.delete(a,1)
print(a1)
#二维数组
a=np.arange(6).reshape(2,3)
print(a)
#如果axis=None或者未指定,则按照一维数组的位置删除元素并返回一个一维数组
a1=np.delete(a,1,axis=None)
print(a1)
#axis=0,按行删除指定元素
a2=np.delete(a,1,axis=0)
print(a2)
#axis=1,按列删除指定元素
a3=np.delete(a,[1,2],axis=1)
print(a3)
#argwhere:默认返回非0元素的坐标
a=np.arange(6).reshape(2,3)
print(np.argwhere(a))
#也可以使用布尔索引过滤
print(np.argwhere(a>2))
#argmax:查找数组中最大值对应的下标
a=np.arange(6)
print(np.argmax(a))

#where:查找非0元素的坐标,返回的时行坐标的列坐标元组
a=np.arange(6).reshape(2,3)
print(np.where(a))

#unique:数组元素去重
a=np.array([1,2,3,4,5,6])
#默认去值
print(np.unique(a))
#return_index为True,返回新数组元素在原数组中的位置列表
e,idx=np.unique(a,return_index=True)
print(e,idx)
#return_inverse为True,返回原数组元素在新数组中的位置
el,inv=np.unique(a,return_inverse=True)
print(el,inv)
#return_counts为True,返回新数组中元素在新数组中出现的次数
el,count=np.unique(a,return_counts=True)
print(el,count)
# axis=None,转换为一维数组去重
# axis=日,按行去重
# axis=1,按列去重
a= np.array([[1,2],[2,3],[1,2]])
print(np.unique(a,axis=0))
print(np.unique(a,axis=1))

import numpy as np
#amin和amax:统计数组中的最小值和最大值
a=np.array([[1,23,4,5,6],[1,2,333,4,5]])
#axis=0,按照垂直方向查找最小值和最大值
print(np.amin(a,axis=0))
print(np.amax(a,axis=0))
#axis=0,按照水平方向查找最小值和最大值
print(np.amin(a,axis=1))
print(np.amax(a,axis=1))
#如果axis=None或不指定,则按照一维数组查找最小值和最大值
print(np.min(a))
print(np.max(a))

'''
ptp:最大值-最小值
'''
a=np.array([[1,2,3],[4,5,6]])
#如果axis=None或不指定,转换为一维数组,查找最大值和最小值,然后最大值-最小值
print(np.ptp(a))
#如果axis=0,按垂直方向查找最大值和最小值,然后最大值-最小值
print(np.ptp(a,axis=0))
#如果axis=1,按水平方向查找最大值和最小值,然后最大值-最小值
print(np.ptp(a,axis=1))

#median:中位数,如果数组长度为奇数,则返回中间的元素值,如果长度为偶数,则返回之间两个数的平均值
#axis=None或不指定,则将二维数组转换为一维数组,然后求中位数
#axis=1,按水平方向求中位数
#axis=0,按垂直方向求中位数
a=np.array([[1,2,3],[4,5,6]])
print(np.median(a))
print(np.median(a,axis=0))
print(np.median(a,axis=1))

#mean:求平均值(元素之和除以元素个数)
a=np.array([1,2,3,4,5,6])
print(np.mean(a))
#二维数组
#axis=None,转化为一维数组计算平均值
a=np.arange(1,7).reshape(2,3)
print(np.mean(a))
# #axis=0,按水平方向求平均值
print(np.mean(a,axis=0))
# #axis=1,按垂直方向求平均值
print(np.mean(a,axis=1))

#average:求加权平均值,数组元素乘以对应权重之和除以所有权重之和
a = np.array([1, 2, 3,4,5])
w = np.array([0.1,0.2,0.3,0.2,0.2])
res = np.average(a, weights=w)
print(res)

#var:方差
a = np.array([1, 2, 3,4,5])
#var默认使用总体方差
#ddof=1表示使用样本方差
print(np.var(a))
print(np.var(a,ddof=1))
#std:标准方差,就是方差开平方
a = np.array([1, 2, 3,4,5])
print(np.std(a))
print(np.std(a,ddof=1))
from matplotlib import pyplot as plt
import numpy as np

#局部设置中文乱码解决方式:
# plt.rcParams['font.sans-serif']=['SimHei']
# plt.rcParams['axes.unicode_minus']=False
import matplotlib
print(matplotlib.matplotlib_fname())
def test01():
    x = np.linspace(-6,6, 50)
    # y = x ** 2
    y = np.sin(x)
#plot:绘制曲线函数
#foramt_string:修改线条颜色,样式
    plt.plot(x,y,'r--')
    plt.show()

def test02():
    x=np.linspace(0,10, 100)
    y=np.cos(x)
    y2 = np.sin(x)
#创建画布
    fig = plt.figure(figsize = (12,8))
#在画布里创建绘图区域
#参数列表:[left,bottom,width,height]
#参数取值范围[0,1]
    ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
#在绘图区域里绘制图像
    ax.plot(x,y,label = 'cos')
    ax.plot(x,y2,label = 'sin你在')
    ax.legend(loc = 'best')
    plt.show()


def test03():
    x = np.linspace(0,10, 100)
    y1=np.sin(x)
    y2=np.cos(x)
    y3=np.tan(x)

    fig = plt.figure(figsize = (12,8))
#创建第一个子绘图区域,将画布分成1行3列,index是1
    ax1 = fig.add_subplot(1,3,1)    #等同于ax1 = fig.add_axes(131)这样的写法
    ax1.plot(x,y1,label = 'sin')
    ax1.legend()


    ax2 = fig.add_subplot(1,3,2)
    ax2.plot(x,y2,label = 'cos')
    ax2.legend()


    ax3 = fig.add_subplot(1,3,3)
    ax3.plot(x,y3,label = 'tan')
    ax3.legend()
    plt.show()



def test04():
    x = np.linspace(0,10, 100)
    y1=np.sin(x)
    y2=np.cos(x)
    y3=np.tan(x)

    fig,ax = plt.subplots(2,3, figsize = (12,8))
    ax[0,0].plot(x,y1,label = 'sin')
    ax[0,0].legend()
    ax[0,1].plot(x,y2,label = 'cos')
    ax[0,1].legend()
    ax[0,2].plot(x,y3,label = 'tan')
    ax[0,2].legend()
    plt.show()


if __name__ == '__main__':
    # test01()
    # test02()
    # test03()
    test04()
from matplotlib import pyplot as plt
import numpy as np
#柱状图
def test01():
    x = ['A','B','C','D']
    y = [20,30,10,50]
#bar():绘制柱状图
# x:x轴数据 y:y轴数据
    plt.bar(x,y,width=0.5,align='edge',color='blue')
    plt.show()

#堆叠柱状图
def test02():
    x = ['A', 'B', 'C', 'D']
    y1 = [20, 30, 10, 50]
    y2 = [30, 10, 20, 40]

    plt.bar(x, y1, width=0.5, align='edge', color='green')
    #bottom:设置柱状图的底部所在的位置,默认为0
    plt.bar(x, y2, width=0.5, align='edge', color='blue', bottom=y1)

    plt.show()
#分组柱状图
def test03():
    categories = ['A', 'B', 'C', 'D']
    x=np.arange(len(categories))
    y1 = [20, 30, 10, 50]
    y2 = [30, 10, 20, 40]

    width=0.35
    plt.bar(x - width / y1,width=width)
    plt.bar(x + width / y2, width=width)
    plt.show()



if __name__ == '__main__':
    # test01()
    # test02()
    test03()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值