Python工具matplotlib以及数组知识补充

numpy:数组元素操作

import numpy as np

# resize:将原数组形状修改为新的形状
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)
a3 = np.append(a, [[1, 2, 3], [1, 2, 3]], axis=1)
print(a3)
# insert:在数组指定位置添加元素值
a = np.arange(6).reshape(2, 3)
# 如果不指定axis或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形状与原数组形状不一致,则会报错
# ValueError: could not broadcast input array from shape (3,1) into shape (2,1)
"""a4 = np.insert(a, 1, [6, 7, 8], axis=1)
print(a4)"""
# delete:删除指定位置的元素
# 一维数组:
a = np.arange(6)
a1 = np.delete(a, 1)
print(a1)
# 二维数组:
a = np.arange(6).reshape(2, 3)
# 如果不指定axis或axis为None
# 则按照一维数组的位置删除元素后返回一维数组
a1 = np.delete(a, [1, 3], 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(1, 7)
print(np.argmax(a))
# where:查找非0元素的坐标,返回的是行坐标和列坐标的元组
a = np.arange(6).reshape(2, 3)
print(np.where(a))
# unique:数组元素去重
a = np.array([1, 2, 2, 3, 4, 4, 5])
# 默认去重
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或axis为None,转换为一维数组去重
# axis=0,按行去重
# axis=1,按列去重
a = np.array([[1, 2], [2, 3], [1, 2]])
print(np.unique(a, axis=0))
print(np.unique(a, axis=1))

numpy:统计函数

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=1,按照行方向查找最小值和最大值
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=0,按照列方向求中位数
# axis=1,按照行方向求中位数
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])
print(np.mean(a))
# 二维数组
a = np.arange(1, 7).reshape(2, 3)
# 如果axis为None或不指定,则按照一维数组求平均值
print(np.mean(a))
# axis=0,按照列方向求平均值
# axis=1,按照行方向求平均值
print(np.mean(a, axis=0))
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])
std_dev = np.std(a)
print(std_dev)

matplotlib:绘图函数

from matplotlib import pyplot as plt
import numpy as np

# 全局设置解决中文乱码
# import matplotlib
#
# print(matplotlib.matplotlib_fname())

# 局部设置解决中文乱码
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False

def test01():
    x = np.linspace(-6, 6, 50)
    y = np.tanh(x)
    y2 = np.sin(x)
    # plot:绘制曲线函数
    # format_string:修改线条的颜色、样式
    plt.plot(x, y, "r--")
    plt.plot(x, y2, "b--")
    # show:显示图形函数
    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])
    # 在绘图区域绘制图形
    # label若是不进行处理,则输入中文会出现乱码
    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.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()
    ax[1, 0].plot(x, y1, label="tan函数")
    ax[1, 0].legend()
    plt.show()

if __name__ == '__main__':
    test01()
    # test02()
    # test03()
    # test04()

matplotlib:图表绘制

from matplotlib import pyplot as plt
import numpy as np

# 柱状图
def bar():
    x = ["A", "B", "C", "D"]
    y = [20, 30, 10, 50]
    # bar():绘制柱状图
    # x:x轴数据,y:y轴数据
    plt.bar(x, y, width=0.5, align="center", color="green")
    plt.show()

def barV1():
    x = ["A", "B", "C", "D"]
    y1 = [20, 30, 10, 50]
    y2 = [30, 10, 20, 40]
    # bottom:将该柱状图堆叠到另外一个柱状图之上
    plt.bar(x, y1, width=0.5, align="center", color="green")
    plt.bar(x, y2, width=0.5, align="center", color="blue", bottom=y1)
    plt.show()

def barV2():
    categoriese = ["A", "B", "C", "D"]
    x = np.array(len(categoriese))
    y1 = [20, 30, 10, 50]
    y2 = [30, 10, 20, 40]
    width = 0.5
    plt.bar(x - width / 2, y1, width=width)
    plt.bar(x + width / 2, y2, width=width)
    plt.show()

if __name__ == '__main__':
    # bar()
    # barV1()
    barV2()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值