数据分析笔记

目录

机器学习基础环境安装与使用

库的安装

miniconda3安装教程
https://blog.csdn.net/HowieXue/article/details/118442904

requirements.txt文件
matplotlib==2.2.2
numpy==1.14.2
pandas==0.20.3
tables==3.4.2
jupyter==1.0.0
各版本Anaconda的下载、安装和卸载(适用于Windows/Linux系统)
下载教程
https://blog.csdn.net/QAQIknow/article/details/107681368
anaconda各版本
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
python包安装失败
安装网址
https://www.lfd.uci.edu/~gohlke/pythonlibs/
pip install wheel
pip install --target=包路径 包名
示例
pip install --target=D:\ProgramData\Anaconda3\Lib\site-packages numpy

使用pip 命令安装

pip install -r requirements.txt

matplotlib使用

绘制图像

import matplotlib.pyplot as plt
#1.创建画布
plt.figure(figsize=(20,8),dpi=100)#figsize指定图像长度,dpi指定图像清晰度
#2.绘制图像
x=[1,2,3]
y=[4,5,6]
plt.plot(x,y)
#3.显示图像
plt.show()

image

图像保存

import matplotlib.pyplot as plt
#1.创建画布
plt.figure(figsize=(20,8),dpi=100)#figsize指定图像长度,dpi指定图像清晰度
#2.绘制图像
x=[1,2,3]
y=[4,5,6]
plt.plot(x,y)
#4.保存图片到指定路径
plt.savefig('./测试.png')
#3.显示图像
plt.show()

x,y轴字体设置

simhei字体下载

import matplotlib.pyplot as plt
#1.创建画布
plt.figure(figsize=(20,8),dpi=100)#figsize指定图像长度,dpi指定图像清晰度
#2.绘制图像
x=[1,2,3]
y=[4,5,6]
plt.plot(x,y)
#2.1 添加x,y刻度
y_ticks=range(3)
plt.yticks(y_ticks)
x_ticks_labal=["11点{}分".format(i) for i in range(3)]
# plt.rc("font",family="SimHei") ###增加了这一行
plt.style.use("seaborn")
plt.rcParams['font.sans-serif']='SimHei'
plt.xticks(x,x_ticks_labal)#必须最开始传递的是数字再进行替换
#3.显示图像
plt.show()

image

添加网格显示

import matplotlib.pyplot as plt
#1.创建画布
plt.figure(figsize=(20,8),dpi=100)#figsize指定图像长度,dpi指定图像清晰度
#2.绘制图像
x=[1,2,3]
y=[4,5,6]
plt.plot(x,y)
#2.1 添加x,y刻度
y_ticks=range(3)
plt.yticks(y_ticks)
x_ticks_labal=["11点{}分".format(i) for i in range(3)]
# plt.rc("font",family="SimHei") ###增加了这一行
plt.style.use("seaborn")
plt.rcParams['font.sans-serif']='SimHei'
plt.xticks(x,x_ticks_labal)#必须最开始传递的是数字再进行替换
#添加网格
plt.grid(True,linestyle="--",alpha=1)#线形,透明度
#3.显示图像
plt.show()

image

添加描述

import matplotlib.pyplot as plt
#1.创建画布
plt.figure(figsize=(20,8),dpi=100)#figsize指定图像长度,dpi指定图像清晰度
#2.绘制图像
x=[1,2,3]
y=[4,5,6]
plt.plot(x,y)
#2.1 添加x,y刻度
y_ticks=range(3)
plt.yticks(y_ticks)
x_ticks_labal=["11点{}分".format(i) for i in range(3)]
# plt.rc("font",family="SimHei") ###增加了这一行
plt.style.use("seaborn")
plt.rcParams['font.sans-serif']='SimHei'
plt.xticks(x,x_ticks_labal)#必须最开始传递的是数字再进行替换
#添加网格
plt.grid(True,linestyle="--",alpha=1)#线形,透明度
#添加描述
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("一小时温度变化图",fontsize=30)
#3.显示图像
plt.show()

image

显示图例

import matplotlib.pyplot as plt
#1.创建画布
plt.figure(figsize=(20,8),dpi=100)#figsize指定图像长度,dpi指定图像清晰度
#2.绘制图像
x=[1,2,3]
y1=[4,5,6]
y2=[5,8,6]

plt.plot(x,y1,label="上海")
plt.plot(x,y2,label="北京")
#2.1 添加x,y刻度
y_ticks=range(3)
plt.yticks(y_ticks)
x_ticks_labal=["11点{}分".format(i) for i in range(3)]
# plt.rc("font",family="SimHei") ###增加了这一行
plt.style.use("seaborn")
plt.rcParams['font.sans-serif']='SimHei'
plt.xticks(x,x_ticks_labal)#必须最开始传递的是数字再进行替换
#添加网格
plt.grid(True,linestyle="--",alpha=1)#线形,透明度
#添加描述
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("一小时温度变化图")
#显示图例
plt.legend(loc="best")
#3.显示图像
plt.show()

image

多个坐标系显示图像

import matplotlib.pyplot as plt
# plt.rc("font",family="SimHei") ###增加了这一行
plt.style.use("seaborn")
plt.rcParams['font.sans-serif']='SimHei'
#1.创建画布
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(20,8),dpi=100)
#2.绘制图像
x=[1,2,3]
y1=[4,5,6]
y2=[5,8,6]

axes[0].plot(x,y1,label="上海")
axes[1].plot(x,y2,label="北京")
# #2.1 添加x,y刻度
y_ticks=range(3)
axes[0].set_yticks(y1)
axes[0].set_xticks(x)
x_ticks_labal=["11点{}分".format(i) for i in range(3)]

axes[0].set_xticklabels(x_ticks_labal)#必须最开始传递的是数字再进行替换
# #添加网格
axes[0].grid(True,linestyle="--",color='r',alpha=1)#线形,透明度
# #添加描述
axes[0].set_xlabel("时间")
axes[0].set_ylabel("温度")
axes[0].set_title("一小时温度变化图")
#显示图例
axes[0].legend(loc=0)
#3.显示图像
plt.show()

image

拆线图的应用场景

plot绘制数学图像

import numpy as np
import matplotlib.pyplot as plt
#生成数据
x=np.linspace(-10,10,1000)
y=np.sin(x)
#生成画布
plt.figure(figsize=(20,8),dpi=100)
#绘制
plt.plot(x,y)
#显示
plt.show()

image

其它图

散点图

import matplotlib.pyplot as plt
import random
#1.散点图
# 数据准备
x=[random.randint(1,22) for _ in range(10)]
y=[random.randint(2,30) for _ in range(10)]
#1.创建画布
plt.figure(figsize=(20,8),dpi=100)
#2.图像绘制(散点图)
plt.scatter(x,y)
#3.图像展示
plt.show()

image

柱状图

import matplotlib.pyplot as plt
import matplotlib as mpl
import random
#2.柱状图
#数据准备
str_list_name=['开心','中国','like','美','猫','狗','兔']
#横坐标
x=range(len(str_list_name))
y=[10,20,62,5,36,23,10]
#创建画布
plt.figure(figsize=(20,8),dpi=100)
#绘制
plt.bar(x,y,color=['b','r','g','y','c','m','k'],width=0.5)
#显示中文字体
# mpl.rc("font", family='Microsoft YaHei')
plt.rcParams['font.sans-serif'] = 'SimHei'#SimSun :宋体;KaiTI:楷体;Microsoft YaHei:微软雅黑LiSu:隶书;FangSong:仿宋;Apple LiGothic Medium:苹果丽中黑;
plt.rcParams['axes.unicode_minus']=False#解决坐标轴负数的负号显示问题
#x轴替换
plt.xticks(x,str_list_name,fontsize=15)
#添加网格
plt.grid()
#添加标题
plt.title('柱状图展示')
#显示图像
plt.show()

image

更多

直方图:plt.hist()
饼图:plt.pie()
参考链接

Numpy学习

创建

import numpy as np

score=np.array([[i for i in range(3)] for _ in range(3)])

ndarray的属性

print(score.shape)#数组维度的元组
print(score.ndim)#数组的维度
print(score.size)#数组元素的数量
print(score.itemsize)#一个数组维度的长度(字节)
print(score.dtype)#数组元素的类型

image

基本使用

生成数组

import numpy as np
#生成为1的数组
ones=np.ones([4, 8])
print(ones)
#生成为0的数组
print(np.zeros_like(ones))

image

从现有数组生成

import numpy as np
a=np.array([[1,2,3],[2,3,4]])
a1=np.array(a)#深拷贝
# print(a1)
a2=np.asarray(a)#浅拷贝
# print(a2)
a[0,0]=100
print(a)
print(a1)
print(a2)

image

生成固定范围的数组

import numpy as np
linspace=np.linspace(0,100,10)#生成等间隔的数组
print(linspace)
arange=np.arange(10,50,3)#每隔多少生成数据
print(arange)
logspace=np.logspace(0,2,3)#生成10^x
print(logspace)

image

生成随机数组

import numpy as np
random_rand=np.random.rand(2,3)
print(random_rand)
random_uniform=np.random.uniform(low=1,high=10,size=(2,3))#生成均匀分布的随机数
print(random_uniform)
random_randint=np.random.randint(1,10,size=(2,3))
print(random_randint)

image

生成正态分布

import matplotlib.pyplot as plt
random_normal=np.random.normal(1,75,100000000)
plt.figure(figsize=(20,8),dpi=100)
plt.hist(random_normal,bins=1000)
plt.show()

image

数组索引,切片

import numpy as np
import matplotlib.pyplot as plt
random_normal=np.random.normal(0,1,(3,10))
print(random_normal)
stock_change=random_normal[0:2,0:3]#按照先行后列
print(stock_change)

image
image

形状修改

import numpy as np
import matplotlib.pyplot as plt
random_normal=np.random.normal(0,1,(4,5))
reshape=random_normal.reshape([5,4])#数组的形状被修改为:5,4,
# print(reshape)
reshape1=random_normal.reshape([-1,10])# -1:表示通过待计算
# print(reshape1)

# random_normal.resize([5,4])#对原来的数据进行修改
new_random_normal=random_normal.T
print(new_random_normal)

类型修改

import numpy as np
import matplotlib.pyplot as plt
random_normal=np.random.normal(0,1,(4,5))
astype=random_normal.astype(np.int32)
print(astype)
tostring=astype.tostring()
print(tostring)

image

数据去重

import numpy as np
import matplotlib.pyplot as plt
temp=np.array([[1,2,3,4],[3,4,5,6]])
unique=np.unique(temp)
print(unique)

image

ndarray运算

逻辑运算

import numpy as np
import matplotlib.pyplot as plt
random_normal=np.random.normal(0,1,(8,10))
print(random_normal)
stacke_change=random_normal[0:5,0:5]
print(stacke_change)
print(stacke_change>1)
stacke_change[stacke_change>1]=2
print(stacke_change)

通用判断函数

import numpy as np
import matplotlib.pyplot as plt
random_normal=np.random.normal(0,1,(8,10))
stacke_change=random_normal[0:5,0:5]
stacke_change[stacke_change>1]=2
print(stacke_change)
print(np.all(stacke_change>0))#所有大于0返回True
print(np.any(stacke_change>0))#有一个大于0返回True

image

三元运算符

import numpy as np
import matplotlib.pyplot as plt
random_normal=np.random.normal(0,1,(8,10))
stacke_change=random_normal[0:2,0:2]
stacke_change[stacke_change>1]=2
print(stacke_change)
where=np.where(stacke_change>0,1,0)
print(where)
logical_and=np.where(np.logical_and(stacke_change>0,stacke_change<1),1,0)
print(logical_and)
logical_or=np.where(np.logical_or(stacke_change>0,stacke_change<1),2,3)
print(logical_or)

image

综合运算

import numpy as np
import matplotlib.pyplot as plt
random_normal=np.random.normal(0,1,(8,10))
stacke_change=random_normal[0:2,0:2]
max=stacke_change.max(axis=1)#求最大值,axis 1按行, axis 0按列求值
argmax=stacke_change.argmax()#最大值的下标
print(stacke_change)
print(max)
print(argmax)

image

矩阵

1.矩阵和向量
矩阵:理解---》二维数组
向量:理解---》一维数组
2.加法和标量乘法
加法:对应位置相加
乘法:标量和每个位置的元素相乘
3.矩阵向量(矩阵)乘法
(M行,N列)X (N行,L列)=(M行,L列)

数组间的运算

数组与数的运算

import numpy as np
import matplotlib.pyplot as plt
arr=np.array([1,2,3,4])
print(arr)
new_arr=arr+1
print(new_arr)

image

数组与数组的运算

需要满足广播机制
	维度相同
		shape对应位置为1

矩阵运算

矩阵乘法api

import numpy as np
import matplotlib.pyplot as plt
random_randint_a=np.random.randint(0,10,(3,2))
print(random_randint_a)
random_randint_b=np.random.randint(0,10,(2,2))
print(random_randint_b)
matmul=np.matmul(random_randint_a,random_randint_b)#乘数字报错
dot=np.dot(random_randint_a,10)#乘数字
print(matmul)

image

PyEcharts学习

官网
安装

pip install pyecharts
from pyecharts.charts import Bar
bar=Bar()
bar.add_xaxis(['衬衣','短库','长袖','短袖','棉花','鞋子','裤子'])
bar.add_yaxis('商家',[2,23,45,56,34,90,12])
bar.render()#渲染

快速使用

image

简单配置

from pyecharts.charts import Bar
bar=(
    Bar()
    .add_xaxis(['衬衣','短库','长袖','短袖','棉花','鞋子','裤子'])
    .add_yaxis('商家',[2,23,45,56,34,90,12])
    #配置
    .set_global_opts(title_opts={'text':'主标题','subtext':'副标题'})
)
bar.render()#渲染

image

渲染成图片文件

安装

pip install snapshot-selenium
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
bar=(
    Bar()
    .add_xaxis(['衬衣','短库','长袖','短袖','棉花','鞋子','裤子'])
    .add_yaxis('商家',[2,23,45,56,34,90,12])
)
make_snapshot(snapshot,bar.render(),'bar.png')

使用主题

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from pyecharts.globals import ThemeType
bar=(
    Bar(
        init_opts=opts.InitOpts(theme=ThemeType.DARK)
    )
    .add_xaxis(['衬衣','短库','长袖','短袖','棉花','鞋子','裤子'])
    .add_yaxis('商家',[2,23,45,56,34,90,12])
)
bar.render()

image

全局配置项

初始化配置项

from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType,RenderType
bar=(
    Bar(
        #InitOpts:初始化配置项
        init_opts=opts.InitOpts(
            width='700px',
            height='800px',#图表画布大小,css长度单位
            renderer=RenderType.CANVAS,#渲染风格,可选:canvas,svg
            page_title='网页标题',
            theme=ThemeType.DARK,#主题
            bg_color='red'#背景颜色
        )
    )
    .add_xaxis(Faker.choose())
    .add_yaxis('商家A',Faker.values())
    .add_yaxis('商家B',Faker.values())
)
bar.render()

image

标题配置项

from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Bar()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        # 全局配置项
        .set_global_opts(
        # TitleOpts:标题配置项
        title_opts=opts.TitleOpts(
            title='柱形图',  # 主标题
            title_link='https://www.baidu.com',  # 主标题点击跳转链接
            title_target='blank',  # blank新窗口打开,self 当前窗口打开
            subtitle='副标题',
            subtitle_link='https://www.baidu.com',
            subtitle_target='blank',
            # 位置
            pos_left='20px',
            pos_top='0px',
            padding=10,  # 内边距
            item_gap=4,  # 主标题与副标题的间隙

        ),


    )
)
bar.render()

image

区域缩放项

from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Bar()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        # 全局配置项
        .set_global_opts(

        #DataZoomOpts:区域缩放项
        datazoom_opts=opts.DataZoomOpts(
            is_show=True,  # 是否显行组件
            type_='slider',  # 组件的类型,slider,inside
            is_realtime=True,  # 拖动时是否实时更新图表
            range_start=20,  # 数据窗口的起始位置,百分比
            range_end=80,  # 数据窗口的结束位置,百分比
            orient='horizontal',  # 组件放置位置,默认水平horizontal或垂直vertical
            is_show_data_shadow=False  # 是否锁定选择区域
        ),

    )
)
bar.render()

image

图例配置

from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Bar()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        # 全局配置项
        .set_global_opts(

        #LegendOpts:图例配置项
        legend_opts=opts.LegendOpts(
            type_='plain',#图例类型:plain普通类型,scroll:滚动翻页类型
            is_show=True,#是否显示图例
            pos_left='20%',#图例位置
            orient='vertical',#方向
            selected_mode='single',#选择模式:True 开启图例点击,False 关闭图例点击,single 单选,multiple 多选
            align='right',#图例和图标的位置
            padding=10,#内边距
            item_gap=5,#图例中每项之间的间距
            item_width=30,#项的宽度
            item_height=15,#项的高度
            inactive_color='#ccc',#图列关闭时的颜色
            legend_icon='roundRect',#常见图标:circle,rect,roundRect,triangle,diamond,arrov
        )

    )
)
bar.render()

image

视觉映射配置

from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Bar()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        # 全局配置项
        .set_global_opts(
        #VisualMapOpts:视觉映射配置
            visualmap_opts=opts.VisualMapOpts(
                is_show=True,
                type_='color',#color 或size
                min_=0,#最小值
                max_=150,#最大值
                range_opacity=0.7,#图元和文字透明度
                range_text=['max','min'],#两段的文本
                range_color=['blue','red','pink'],#过渡颜色
                is_piecewise=True,#是否分段
                is_inverse=True,#是否反转

        )

    )
)
bar.render()

image

提示框配置项

from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Bar()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        # 全局配置项
        .set_global_opts(
       #TooltipOpts:提示框配置项
            tooltip_opts=opts.TooltipOpts(
                is_show=True,
                #触发类型:item 数据项,一般用于:散点图,柱形图,饼图
                #       :axis 坐标轴,提示线,主要用于条形图,折线图等
                trigger='item',
                #触发条件:mousemove,click,mousemove|click
                trigger_on='click',
                is_show_content=True,#是否显示提示框浮层
                #标签内容的格式
                    #字符串中的模板变量:
                        #{a}:系列名series_name
                        #{b}:数据名
                        #{c}:值
                formatter='{a}:{b}-{c}',
                border_color='pink',#边框颜色
                border_width=1,#边框宽度
                background_color='blue'#背景颜色


            )
    )
)
bar.render()

image

坐标轴配置项

from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Bar()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        # 全局配置项
        .set_global_opts(
            #AxisOpts:坐标轴配置项
            xaxis_opts=opts.AxisOpts(
                is_show=True,#是否显示X轴
                #坐标轴类型:
                #   value:数值轴,用于连续数据
                #   category:类目轴,适用于离散数据,比如,星期一,星期二等
                #   time:时间轴,适用于连续的时序数据
                type_='category'
            ),
            yaxis_opts=opts.AxisOpts(
                is_show=True,
                #不显示y轴的线
                axisline_opts=opts.AxisLineOpts(is_show=False),
                #不显示y轴的刻度
                axistick_opts=opts.AxisTickOpts(is_show=False)
            )
    )
)
bar.render()

image

系列配置项

图元样式配置项

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Line()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        #全局配置
        .set_global_opts(
            title_opts=opts.TitleOpts(title='折线图'),
            #提示线
            tooltip_opts=opts.TooltipOpts(trigger='axis')
        )
    #系列配置项
    .set_series_opts(
            #ItemStyleOpts:图元样式配置项
            #图的颜色
            # 使用纯色
            # RGB,rgb(120,120,120)
            # RGBA,rgba(120,120,120,0.5)
            # 十六进制:#ccc
            color='blue',
            opacity=0.6,
            border_color='green',
            border_width=2
        )


)
bar.render()

image

线样式配置项

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Line()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        #全局配置
        .set_global_opts(
            title_opts=opts.TitleOpts(title='折线图'),
            #提示线
            tooltip_opts=opts.TooltipOpts(trigger='axis')
        )
    #系列配置项
    .set_series_opts(
         #LineStyleOpts:线样式配置项
            linestyle_opts=opts.LineStyleOpts(
                is_show=True,
                width=2,#线宽
                color='green',#线颜色
                type_='dashed',#直线solid,虚线dashed,点线dotted
            )
        )


)
bar.render()

image

标签配置项

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Line()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        #全局配置
        .set_global_opts(
            title_opts=opts.TitleOpts(title='折线图'),
            #提示线
            tooltip_opts=opts.TooltipOpts(trigger='axis')
        )
    #系列配置项
    .set_series_opts(
       #LabelOpts:标签配置项
            label_opts=opts.LabelOpts(
                is_show=True,
                position='top',#位置
                color='red',#颜色
                font_size=14,#大小
                font_family='Arial',#字体
                font_style='italic',#是否斜体,italic
                font_weight='bold',#是否加粗 bold
                #标签旋转,-90到90
                rotate=-40

            )
        )


)
bar.render()

image

标记点配置项

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Line()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        #全局配置
        .set_global_opts(
            title_opts=opts.TitleOpts(title='折线图'),
            #提示线
            tooltip_opts=opts.TooltipOpts(trigger='axis')
        )
    #系列配置项
    .set_series_opts(
        #MarkPointOpts:标记点配置项
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    #type_:特殊标记类型,min,max,average
                    #symbol:标记点的图形
                    #symbol_size:标记点的大小
                    opts.MarkPointItem(type_='max',symbol='pin',symbol_size=50),
                    opts.MarkPointItem(type_='min')
                ]
            )

        )


)
bar.render()

image

标记线

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType

bar = (
        Line()
        .add_xaxis(Faker.choose())
        .add_yaxis('商家A', Faker.values())
        .add_yaxis('商家B', Faker.values())
        #全局配置
        .set_global_opts(
            title_opts=opts.TitleOpts(title='折线图'),
            #提示线
            tooltip_opts=opts.TooltipOpts(trigger='axis')
        )
    #系列配置项
    .set_series_opts(
       #MarkLineOpts:标记线
            markline_opts=opts.MarkLineOpts(
                data=[
                    opts.MarkLineItem(type_='average')
                ],
                label_opts=opts.LabelOpts(
                    color='red'
                )
            )
        )


)
bar.render()

image

饼图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
pie=(
    Pie()
    .add('',[list(i) for i in zip(Faker.choose(),Faker.values())])
    .set_colors(['red','blue','green','orange','yellow','pink','black'])
    .set_global_opts(
        title_opts=opts.TitleOpts(title='设置颜色'),
        legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical')
    )
	.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)

pie.render()

image

玫瑰图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
pie=(
    Pie()
    .add('',[list(i) for i in zip(Faker.choose(),Faker.values())],
         radius=['30%','75%'],
         center=['25%','50%'],
         rosetype='radius'
         )
    .set_global_opts(
        title_opts=opts.TitleOpts(title='玫瑰图'),
        legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical')
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)

pie.render()

image

柱形图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar=(
    Bar(
        init_opts=opts.InitOpts(
            animation_opts=opts.AnimationOpts(
                animation_delay=1000,#动画延时1秒钟
                animation_easing='elasticOut'#弹性动画
            )
        )
    )
    .add_xaxis(Faker.choose())
    .add_yaxis('商家A',Faker.values())
    .add_yaxis('商家B',Faker.values())
    .set_global_opts(
        title_opts=opts.TitleOpts(title='柱形图'),
        legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical')
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)

bar.render()

image

添加js代码

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
    Bar(
        init_opts=opts.InitOpts(
           bg_color={
               'image':JsCode('img'),
               'repeat':'no-repeat'
           }
        )
    )
    .add_xaxis(Faker.choose())
    .add_yaxis('商家A',Faker.values())
    .add_yaxis('商家B',Faker.values())
    .set_global_opts(
        title_opts=opts.TitleOpts(title='柱形图添加JS代码'),
        legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical')
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
    #添加js代码
    .add_js_funcs(
        """
        var img=new Image();
        img.src='https://ts3.cn.mm.bing.net/th?id=OSK.HEROAswkkmw5w3FXgT4DZB0RybGod_LpTISZPIztKg7SV0Q&w=472&h=280&c=13&rs=2&o=6&oif=webp&dpr=1.3&pid=SANGAM'
        """
    )
)

bar.render()

image

堆叠柱形图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
    Bar(

    )
    .add_xaxis(Faker.choose())
    .add_yaxis('商家A',Faker.values(),stack='abc')
    .add_yaxis('商家B',Faker.values(),stack='abc')
    .set_global_opts(
        title_opts=opts.TitleOpts(title='堆叠柱形图'),
        legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical'),
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
        datazoom_opts=[
            opts.DataZoomOpts(),#x轴缩放
            opts.DataZoomOpts(type_='inside')#鼠标缩放
        ]
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))


)

bar.render()

image

条形图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
    Bar()
    .add_xaxis(Faker.choose())
    .add_yaxis('商家A',Faker.values())
    .add_yaxis('商家B',Faker.values())
    .reversal_axis()#翻转轴,条形图
    .set_global_opts(
        title_opts=opts.TitleOpts(title='条形图'),
    )
    .set_series_opts(
        label_opts=opts.LabelOpts(position='right')
    )



)

bar.render()

image

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
    Bar()
    .add_xaxis(Faker.choose())
    .add_yaxis('商家A',Faker.values(),gap='0%')
    .add_yaxis('商家B',Faker.values(),gap='0%')
    .set_global_opts(
        title_opts=opts.TitleOpts(title='不同系列柱子之间的距离'),
    )
    .set_series_opts(
        label_opts=opts.LabelOpts(position='right')
    )



)

bar.render()

image

直方图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
    Bar()
    .add_xaxis(Faker.choose())
    .add_yaxis('商家A',Faker.values(),category_gap=0)
    .set_global_opts(
        title_opts=opts.TitleOpts(title='单系列柱子之间的距离,直方图'),
    )
)

bar.render()

image

JsCode自定义柱状颜色

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
color_fun="""
    function(params){
        if(params.value >0 && params.value<50){
            return 'red';
            }
        else if (params.value >50 && params.value<100){
            return 'green';
            }
            else{
            return 'blue';
            }
    }
"""
bar=(
    Bar()
    .add_xaxis(Faker.choose())
    .add_yaxis('商家A',Faker.values(),itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_fun)))
    .set_global_opts(
        title_opts=opts.TitleOpts(title='JsCode自定义柱状颜色'),
    )
)

bar.render()

image

象形柱状图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode

bar=(
    PictorialBar()
    .add_xaxis(Faker.choose())
    .add_yaxis('商家A',
               Faker.values(),
               label_opts=opts.LabelOpts(is_show=False),
               symbol_size=10,#符号大小
               is_symbol_clip=True,#符号裁剪
               symbol_repeat='fixed',#重复方式
               symbol=SymbolType.ROUND_RECT,#符号类型

               )
    .reversal_axis()
    .set_global_opts(
        title_opts=opts.TitleOpts(title='象形柱状图'),
        xaxis_opts=opts.AxisOpts(is_show=False),#不显示X轴
        yaxis_opts=opts.AxisOpts(
            axistick_opts=opts.AxisOpts(is_show=False),#不显示y轴刻度
            axisline_opts=opts.AxisOpts(is_show=False)#不显示y轴的线
        )
    )
)

bar.render()

image

雷达图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
v1=[[4300,10000,28000,35000,50000,19000]]
v2=[[5000,14000,28000,31000,50000,19000]]
bar=(
    Radar()
    .add_schema(
        schema=[
            opts.RadarIndicatorItem(name='项目1',max_=6000),
            opts.RadarIndicatorItem(name='项目2',max_=16000),
            opts.RadarIndicatorItem(name='项目3',max_=30000),
            opts.RadarIndicatorItem(name='项目4',max_=38000),
            opts.RadarIndicatorItem(name='项目5',max_=60000),
            opts.RadarIndicatorItem(name='项目6',max_=22000),
        ]
    )
   .set_global_opts(title_opts=opts.TitleOpts(title='雷达图'))
   .add('数据1',v1,color='blue')
   .add('数据2',v2)
   .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
)

bar.render()

image

拆线图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode

line=(
   Line(
       init_opts=opts.InitOpts(width='1000px',height='500px')
   )
   .add_xaxis(Faker.week)
    .add_yaxis('商家A',
               Faker.values(),
               symbol='triangle',#点符号类型:triangle三角形
               symbol_size=20,#点的大小
               linestyle_opts=opts.LineStyleOpts(
                   color='green',width=2,type_='dashed'
               ),#线条样式
               label_opts=opts.LabelOpts(
                   is_show=False
               ),#标签
               itemstyle_opts=opts.ItemStyleOpts(
                    border_color='green',border_width=2,color='yellow'
               ),#点的属性
               markpoint_opts=opts.MarkPointOpts(
                   data=[
                       opts.MarkPointItem(type_='max'),#最大值
                       opts.MarkPointItem(type_='mih'),#最小值
                   ]
               ),#标注点
               markline_opts=opts.MarkLineOpts(
                   data=[
                       opts.MarkLineItem(type_='average'),#平均值
                   ]
               ),#标注线
               )
    .add_yaxis('商家A',Faker.values(),is_smooth=True)#平滑曲线
    .set_global_opts(
       title_opts=opts.TitleOpts(title='拆线图'),
       tooltip_opts=opts.TooltipOpts(trigger='axis'),#提示线
        yaxis_opts=opts.AxisOpts(
            type_='value',
            splitline_opts=opts.SplitAreaOpts(is_show=True),#显示分隔线
        )
   )
)

line.render()

image

面积图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode

line=(
   Line()
   .add_xaxis(Faker.week)
    .add_yaxis('',y_axis=[1,23,34,56,34,78,33],
               areastyle_opts=opts.AreaStyleOpts(opacity=0.5),#面积图
               )
    .set_global_opts(
       title_opts=opts.TitleOpts(
           title='面积图'
       ),
       tooltip_opts=opts.TooltipOpts(trigger='axis'),#提示线
       xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False),#boundary_gap:没有间隙
   )

)

line.render()

image

堆叠面积图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode

line=(
   Line()
   .add_xaxis(Faker.week)
    .add_yaxis('广告',y_axis=[1,23,34,56,34,78,33],
               areastyle_opts=opts.AreaStyleOpts(opacity=0.5),#面积图
               stack='堆叠',
               label_opts=opts.LabelOpts(is_show=False)
               )
.add_yaxis('流量',y_axis=[1,23,34,56,34,78,33],
               areastyle_opts=opts.AreaStyleOpts(opacity=0.5),#面积图
               stack='堆叠',
               label_opts=opts.LabelOpts(is_show=False)
               )
    .set_global_opts(
       title_opts=opts.TitleOpts(
           title='堆叠面积图'
       ),
       tooltip_opts=opts.TooltipOpts(trigger='axis'),#提示线
       xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False),#boundary_gap:没有间隙
   )

)

line.render()

image

散点图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar,Scatter
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
data=[
    [1,3],
    [1,5],
    [4,5],
    [4,5.8],
    [4.9,5.8],
    [8,5.8],
    [8.9,5.8],
    [2,5.8],

]
data.sort(key=lambda x:x[0])
x_data=[i[0] for i in data]
y_data=[y[0] for y in data]
scatter=(
   Scatter(
       init_opts=opts.InitOpts(width='800px',height='400px')
   )
    .add_xaxis(xaxis_data=x_data)
    .add_yaxis('',y_axis=y_data,symbol_size=20,label_opts=opts.LabelOpts(is_show=True))
    .set_global_opts(
       title_opts=opts.TitleOpts(
           title='散点图'
       ),
       xaxis_opts=opts.AxisOpts(
           type_='value',
           splitline_opts=opts.SplitLineOpts(is_show=True)
       ),
        yaxis_opts=opts.AxisOpts(
                   type_='value',
                   splitline_opts=opts.SplitLineOpts(is_show=True)
               )
   )


)

scatter.render()

image

涟漪散点图

from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar,Scatter,EffectScatter
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode

effectscatter=(
   EffectScatter()
   .add_xaxis(Faker.choose())
   .add_yaxis('',Faker.values(),symbol=SymbolType.ARROW)
    .set_global_opts(
       title_opts=opts.TitleOpts(
           title='涟漪散点图'
       )
   )


)

effectscatter.render()

image

热力图

import random
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
value=[[i,j,random.randint(0,50)]for i in range(24) for j in range(7)]
effectscatter=(
   HeatMap()
   .add_xaxis(Faker.clock)#clock:时钟
   .add_yaxis('热力图',Faker.week,value,label_opts=opts.LabelOpts(is_show=True,position='inside'))
    .set_global_opts(
       title_opts=opts.TitleOpts(
           title='热力图'
       )
   )


)

effectscatter.render()

image

日历图

import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
begin=datetime.date(2024,1,1)
end=datetime.date(2024,12,31)
data=[[str(begin+datetime.timedelta(days=i)),random.randint(1000,25000)]
      for i in range((end-begin).days)]
calendar=(
   Calendar()
    .add('',
         data,
         calendar_opts=opts.CalendarOpts(
             range_='2024',
             daylabel_opts=opts.CalendarDayLabelOpts(name_map='cn'),#中文
             monthlabel_opts=opts.CalendarMonthLabelOpts(name_map='cn'),#中文
         ))
    .set_global_opts(
       title_opts=opts.TitleOpts(
           title='日历图'
       ),
       visualmap_opts=opts.VisualMapOpts(
           max_=25000,
           min_=1000,
           orient='horizontal',#水平方向
           is_piecewise=True,#显示方式
           pos_left='100px',
           pos_top='230px'
       )
   )


)

calendar.render()

image

箱型图

import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
v1=[[23,234,4353,23432,654,89,98798],[23,2342,435,2332,6534,89,9898]]
boxplot=Boxplot()
boxplot.add_xaxis(['demo1','demo2'])
boxplot.add_yaxis('A',boxplot.prepare_data(v1))
boxplot.render()

image

词云图

import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
data=[(i,random.randint(1,1000))  for i in Faker.choose() ]
wordcloud=(
    WordCloud()
    .add(
        '热点分析',
        data_pair=data,
        word_size_range=[6,60],#字体大小范围
        textstyle_opts=opts.TextStyleOpts(
            font_family='cursive'#字体
        )
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title='词云图')
    ))
wordcloud.render()

image

漏斗图

import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud,\
Funnel
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
data=[(i,random.randint(1,1000))  for i in Faker.choose() ]
funnel=(
    Funnel()
    .add(
        '商品',
        [list(i) for i in zip(Faker.choose(),Faker.values())],
        gap=2,#间隙
        tooltip_opts=opts.TooltipOpts(
            trigger='item',
            formatter='{a}<br/> {b}:{c}'
        )
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title='漏斗图')
    ))
funnel.render()

image

极坐标图

import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud,\
Funnel,Polar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode

polar=(
    Polar()
    .add_schema(
        radiusaxis_opts=opts.RadiusAxisOpts(
            data=Faker.week,
            type_='category'
        )
    )
    .add('商品A',[1,24,34,45,76,12,90],type_='bar',stack='abc')
    .add('商品B',[1,30,34,45,76,13,90],type_='bar',stack='abc')
    .add('商品C',[1,24,34,56,76,12,90],type_='bar',stack='abc')
    .set_global_opts(
        title_opts=opts.TitleOpts(title='极坐标图+堆叠柱形图')
    ))
polar.render()

image

import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud,\
Funnel,Polar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
data=[(i,random.randint(1,100))for i in range(101)]
polar=(
    Polar()
    .add(
        '极坐标',
        data,
        type_='scatter',#散点图
        label_opts=opts.LabelOpts(is_show=False)
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title='极坐标图')
    ))
polar.render()

image

水球图

import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud,\
Funnel,Polar,Liquid
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode

liquid=(
    Liquid()
    .add('',[0.2,0.7])
    .set_global_opts(
        title_opts=opts.TitleOpts(title='水球图')
    ))
liquid.render()

image

桑基图

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode

nodes = [
    {'name': 'c1'},
    {'name': 'c2'},
    {'name': 'c3'},
    {'name': 'c4'},
    {'name': 'c5'},
    {'name': 'c6'},
    {'name': 'c7'},
]
links=[
    {'source':'c1','target':'c2','value':10},
    {'source':'c2','target':'c3','value':20},
    {'source':'c3','target':'c4','value':30},
    {'source':'c4','target':'c5','value':40},
    {'source':'c6','target':'c7','value':50},
]
sankey = (
    Sankey()
        .add('',
             nodes,#所有节点
             links,#节点之间的链接关系
             linestyle_opt=opts.LineStyleOpts(
                 opacity=0.2,#透明度
                 curve=0.6,#曲线幅度0~1
                 color='red'
             )
             )
        .set_global_opts(
        title_opts=opts.TitleOpts(title='桑基图')
    ))
sankey.render()

image

旭日图

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey,Sunburst
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
data=[
    opts.SunburstItem(
        name='Grandpa',
        children=[
            opts.SunburstItem(
                name='Uncle',
                value=15,
                children=[
                    opts.SunburstItem(name='Jack',value=2),
                    opts.SunburstItem(
                        name='Mary',
                        value=5,
                        children=[opts.SunburstItem(name='Jackson',value=2)]
                    )
                ]
            )
        ],
    ),
    opts.SunburstItem(
        name='Father',
        value=10,
        children=[
            opts.SunburstItem(name='Me',value=5),
            opts.SunburstItem(name='Peter',value=1),
        ]
    ),
    opts.SunburstItem(
        name='Nancy',
        children=[
            opts.SunburstItem(
                name='Nike',
                children=[
                    opts.SunburstItem(name='Betty',value=1),
                    opts.SunburstItem(name='Jenny',value=2)
                ]
            )
        ]
    )
]

sunburst = (
    Sunburst(
        init_opts=opts.InitOpts(
            width='800px',height='400px'
        )
    )
        .add('',data_pair=data)
        .set_global_opts(
        title_opts=opts.TitleOpts(title='旭日图')
    ))
sunburst.render()

image

仪表盘

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey,Sunburst,Gauge
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode


gauge = (
    Gauge()
        .add('指标',data_pair=[('','66')],radius='60%')
        .set_global_opts(
        title_opts=opts.TitleOpts(title='仪表盘')
    ))
gauge.render()

image

树图

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey,Sunburst,Gauge,Tree
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
data=[
    {
        'children':[
            {'name':'叔叔'},
            {'children':[{'name':'小华'}],'name':'伯伯'},
            {'name':'父亲',
             'children':[
                 {'name':'我'},
                 {'name':'姐姐'}
             ]}
        ],
        'name':'祖父母'
    }
]

tree = (
    Tree()
        .add('指标',data)
        .set_global_opts(
        title_opts=opts.TitleOpts(title='树图')
    ))
tree.render()

image

矩形树图

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey,Sunburst,Gauge,Tree,TreeMap
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
data=[
    {'value':40,'name':'伯伯'},
    {
        'value':180,
        'name':'父亲',
        'children':[
            {'value':70,
             'name':'我',
             'children':[
                 {'value':12,'name':'大儿子'},
                 {'value':28,'name':'二儿子'},
                 {'value':18,'name':'三女儿'},
                 {'value':16,'name':'四女儿'},
             ]}
        ]
    }
]

treemap = (
    TreeMap()
        .add('',data)
        .set_global_opts(
        title_opts=opts.TitleOpts(title='矩形树图')
    ))
treemap.render()

image

关系图

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey,Sunburst,Gauge,Tree,TreeMap,Graph
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
nodes=[
    {'name':'node1','symbolSize':10},
    {'name':'node2','symbolSize':20},
    {'name':'node3','symbolSize':30},
    {'name':'node4','symbolSize':40},
    {'name':'node5','symbolSize':50},
    {'name':'node6','symbolSize':60},
    {'name':'node7','symbolSize':10},
]
links=[]
for i in nodes:
    for j in nodes:
        links.append({'source':i.get('name'),'target':j.get('name')})


graph = (
    Graph()
        .add('',
             nodes,
             links,
             repulsion=8000#排斥力:越大分的越开
             )
        .set_global_opts(
        title_opts=opts.TitleOpts(title='关系图')
    ))
graph.render()

image

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey,Sunburst,Gauge,Tree,TreeMap,Graph
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
nodes_data=[
    opts.GraphNode(name='node1',symbol_size=10),
    opts.GraphNode(name='node2',symbol_size=20),
    opts.GraphNode(name='node3',symbol_size=30),
    opts.GraphNode(name='node4',symbol_size=40),
    opts.GraphNode(name='node5',symbol_size=50),
    opts.GraphNode(name='node6',symbol_size=60),
]
link_data=[
    opts.GraphLink(source='node1',target='node2',value=1),
    opts.GraphLink(source='node2',target='node3',value=2),
    opts.GraphLink(source='node3',target='node4',value=3),
    opts.GraphLink(source='node4',target='node5',value=4),
    opts.GraphLink(source='node5',target='node6',value=5),
    opts.GraphLink(source='node6',target='node1',value=6),
]


graph = (
    Graph()
        .add('',
             nodes_data,
             link_data,
             repulsion=3000,#排斥力:越大分的越开
             edge_label=opts.LabelOpts(
                 is_show=True,
                 position='middle',
                 formatter='{b}的数据:{c}'
             )
             )
        .set_global_opts(
        title_opts=opts.TitleOpts(title='关系图2')
    ))
graph.render()

image

K线图

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, Kline
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode

data = [
    # 开盘价,收盘价,最低价,最高价
    [2320.20, 2320.26, 2287, 2363],
    [2320.21, 2320.26, 2287, 2363],
    [2320.22, 2320.27, 2284, 2362],
    [2320.20, 2320.27, 2283, 2363],
    [2320.23, 2320.20, 2282, 2364],
    [2320.24, 2320.22, 2281, 2365],
]

kline = (
    Kline()
        .add_xaxis(['2024/1/{}'.format(i + 1) for i in range(31)])
        .add_yaxis('K线图', data)
        .set_global_opts(
        title_opts=opts.TitleOpts(title='K线图')
    ))
kline.render()

image

地图

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline,Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode

provinces=[p+"市" if p=='上海'else p+'省' for p in Faker.provinces]

map = (
    Map()
     .add(
        '',
        [list(i) for i in zip(provinces,Faker.values())],
        'china'#地图类型
    )
        .set_global_opts(
        title_opts=opts.TitleOpts(title='地图'),
        visualmap_opts=opts.VisualMapOpts(max_=200)
    ))
map.render()

image

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline,Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode


map = (
    Map()
     .add(
        '',
        [list(i) for i in zip(Faker.guangdong_city,Faker.values())],
        '广东'#地图类型
    )
        .set_global_opts(
        title_opts=opts.TitleOpts(title='广东地图'),
        visualmap_opts=opts.VisualMapOpts(max_=200,is_piecewise=True)
    ))
map.render()

image

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline,Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode


map = (
    Map()
     .add(
        '',
        [list(i) for i in zip(Faker.country,Faker.values())],
        'world'#地图类型
    )
        .set_global_opts(
        title_opts=opts.TitleOpts(title='世界地图'),
        visualmap_opts=opts.VisualMapOpts(max_=200,is_piecewise=True)
    ))
map.render()

image

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline,Map,MapGlobe
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
POPULATION=[
    ['china',1322343243],
    ['India',132343243],
    ['Indonesia',1322343243],
    ['Mexico',1022343243],
    ['Japan',122343243],
    ['Ethiopia',1122343243],
            ]
data=[i for _,i in POPULATION]
low,high=min(data),max(data)
mapglobe = (
    MapGlobe()
    .add_schema()
     .add(
        maptype='world',
        series_name='世界人口',
        data_pair=POPULATION,#人口数据
        is_map_symbol_show=False,#地图中各个国家的符号是否显示
        label_opts=opts.LabelOpts(is_show=True)#显示国家名称
    )

        .set_global_opts(
        title_opts=opts.TitleOpts(title='地球'),
        visualmap_opts=opts.VisualMapOpts(max_=high,min_=low,range_text=['Max','Min'],is_piecewise=True)
    ))
mapglobe.render()

地理坐标图

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline, Map, MapGlobe, Geo
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode

geo = (
    Geo()
        .add_schema(maptype='china')
        .add('',
             [('广州', 50), ('北京', 60), ('杭州', 70), ('重庆', 80)],
             type_=ChartType.EFFECT_SCATTER, color='red')
        .add(
        '',
        [('广州', '上海'), ('广州', '北京'), ('广州', '杭州'), ('广州', '重庆')],
        type_=ChartType.LINES,  # 线
        #箭头
        effect_opts=opts.EffectOpts(
            symbol_size=6,
            symbol=SymbolType.ARROW,
            color='blue'
        ),
        linestyle_opts=opts.LineStyleOpts(curve=0.2)

    )
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
        title_opts=opts.TitleOpts(title='地理坐标图'),

    ))
geo.render()

image

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline, Map, MapGlobe, Geo
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode

geo = (
    Geo()
        .add_schema(maptype='china')
        .add('geo',
             [list(i) for i in zip(Faker.provinces,Faker.values())],
             type_=ChartType.HEATMAP#热力图
             )
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
        title_opts=opts.TitleOpts(title='地理坐标图+热力图'),
        visualmap_opts=opts.VisualMapOpts()

    ))
geo.render()

image

import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline, Map, MapGlobe, Geo
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode

geo = (
    Geo()
        .add_schema(maptype='china')
        .add('geo',
             [list(i) for i in zip(Faker.provinces,Faker.values())],
             type_=ChartType.EFFECT_SCATTER#涟漪散点图
             )
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
        title_opts=opts.TitleOpts(title='地理坐标图+涟漪散点图'),
        visualmap_opts=opts.VisualMapOpts()

    ))
geo.render()

image

3D拆线图

import random, datetime,math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline, Map, MapGlobe, Geo,Line3D
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
data=[]
for i in range(0,25000):
    _i=i/1000
    x=(1+0.25*math.cos(75*_i))*math.cos(_i)
    y=(1+0.25*math.cos(75*_i))*math.sin(_i)
    z=_i+2.0*math.sin(75*_i)
    data.append([x,y,z])

line3d = (
    Line3D()

        .add('',
             data,
             xaxis3d_opts=opts.Axis3DOpts(Faker.clock,type_='value'),
             yaxis3d_opts=opts.Axis3DOpts(Faker.week,type_='value'),
             grid3d_opts=opts.Grid3DOpts(
                 width=100,
                 depth=100,
                 rotate_speed=150,
                 is_rotate=True
             )
             )
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
        title_opts=opts.TitleOpts(title='3D拆线图'),
        visualmap_opts=opts.VisualMapOpts(
            min_=0,
            max_=30,
            range_color=Faker.visual_color
        )

    ))
line3d.render()

image

3D柱状图

import random, datetime,math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline, Map, MapGlobe, Geo,Line3D,Bar3D
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
data=[(i,j,random.randint(0,12)) for i in range(6) for j in range(24)]


bar3d = (
    Bar3D()
        .add('',
             [[d[1],d[0],d[2] ]for d in data],
             xaxis3d_opts=opts.Axis3DOpts(Faker.clock,type_='category'),
             yaxis3d_opts=opts.Axis3DOpts(Faker.week,type_='category'),
             zaxis3d_opts=opts.Axis3DOpts(type_='value')
             )
        .set_global_opts(
        title_opts=opts.TitleOpts(title='3D柱状图'),
        visualmap_opts=opts.VisualMapOpts(
            max_=15,
        )

    ))
bar3d.render()

image

3D堆叠柱状图

import random, datetime, math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline, Map, MapGlobe, Geo, Line3D, Bar3D
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode

x_data = y_data = list(range(10))


def generate_data():
    data = []
    for i in range(10):
        for j in range(10):
            value = random.randint(0, 9)
            data.append([i, j, value * 2 + 4])
    return data


bar3d = Bar3D()
for _ in range(10):
    bar3d.add('',
              generate_data(),
              xaxis3d_opts=opts.Axis3DOpts(x_data, type_='value'),
              yaxis3d_opts=opts.Axis3DOpts(y_data, type_='value'),
              zaxis3d_opts=opts.Axis3DOpts(type_='value'),
              shading='lambert'  # 清晰
              )
bar3d.set_global_opts(
    title_opts=opts.TitleOpts(title='3D堆叠柱状图'),
    visualmap_opts=opts.VisualMapOpts(
        max_=15,
    )
)

bar3d.set_series_opts(stack='abc')
bar3d.render()

image

时间轮播图

import random, datetime, math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline, Map, MapGlobe, Geo, Line3D, Bar3D,Timeline
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode

x=Faker.choose()
tl=Timeline()
for i in range(2020,2025):
    bar=(
        Bar()
        .add_xaxis(x)
        .add_yaxis('A',Faker.values())
        .add_yaxis('B',Faker.values())
        .set_global_opts(
            title_opts=opts.TitleOpts(title='时间轮播图')
        )
    )
    tl.add(bar,'{}年'.format(i))
tl.render()

image

并行布局

import random, datetime, math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
    Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
    Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
    Kline, Map, MapGlobe, Geo, Line3D, Bar3D,Timeline,Grid
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
line=(
    Line()
    .add_xaxis(Faker.choose())
    .add_yaxis('A',Faker.values())
    .add_yaxis('B',Faker.values())
    .set_global_opts(
        title_opts=opts.TitleOpts(title='拆线图',pos_left='5%'),
        legend_opts=opts.LegendOpts(pos_left='20%')
    )
)
scatter=(
    Scatter()
    .add_xaxis(Faker.choose())
    .add_yaxis('C',Faker.values())
    .add_yaxis('D',Faker.values())
    .set_global_opts(
        title_opts=opts.TitleOpts(title='散点图',pos_right='40%'),
        legend_opts=opts.LegendOpts(pos_right='20%')
    )
)
grid=(
    Grid()
    .add(line,grid_opts=opts.GridOpts(pos_right='55%'))
    .add(scatter,grid_opts=opts.GridOpts(pos_left='55%'))
)
grid.render()

image

pandas学习

DataFrame的使用

import pandas as pd
import numpy as np
stock_change=np.random.normal(0,1,(10,5))
stock_code=['股票{}'.format(i+1)for i in range(stock_change.shape[0])]
date=pd.date_range(start='20230211',periods=stock_change.shape[1],freq="B")#freq:递进单位,默认1天,“B”默认略过周未
dataframe=pd.DataFrame(stock_change,index=stock_code,columns=date)
print(dataframe)

image

DataFrame的属性

import pandas as pd
import numpy as np
stock_change=np.random.normal(0,1,(3,4))
stock_code=['股票{}'.format(i+1)for i in range(stock_change.shape[0])]
date=pd.date_range(start='20230211',periods=stock_change.shape[1],freq="B")
dataframe=pd.DataFrame(stock_change,index=stock_code,columns=date)
print(dataframe)
print('-'*20)
print(dataframe.shape)#查看几行几列
print('-'*20)
print(dataframe.index)#查看行标题
print('-'*20)
print(dataframe.columns)#查看列标题
print('-'*20)
print(dataframe.values)#查看值
print('-'*20)
print(dataframe.T)#行,列互换
print('-'*20)
print(dataframe.head(2))#查看前几行
print('-'*20)
print(dataframe.tail(1))#查看后几行

image

索引的设置

import pandas as pd
import numpy as np
stock_change=np.random.normal(0,1,(2,4))
stock_code=['股票{}'.format(i+1)for i in range(stock_change.shape[0])]
date=pd.date_range(start='20230211',periods=stock_change.shape[1],freq="B")
dataframe=pd.DataFrame(stock_change,index=stock_code,columns=date)
reset_index=dataframe.reset_index(drop=True)#drop:默认为False,不删除原来索引,如果为True删除原来的索引值
print(reset_index)

image

import pandas as pd
import numpy as np
dataframe=pd.DataFrame({
    'month':[1,2,3],
    'year':[2,3,4],
    'sale':[8,9,0]
})
new_dataframe=dataframe.set_index(keys=['year'])#以某字段设置索引
print(new_dataframe)

image

数据离散化

pd.qcut()---把数据大致分为数量相等的几类
pd.cut()---指定分组间隔
one-hot编码
把数据转换成为0,1统计类型
pd.get_dummies()

合并

pd.concat()
pd.merge()
left --左表
right --右表
on --指定键
how --按照什么方式进行拼接

交叉表与透视表

探索两列数据之间的关系
pd.crosstab()
返回具体数量
对象.pivot_table()
返回占比情况

分组和聚合

对象.groupby()
参数:as_index --是否进行索引
可以对数据进行对此分组,需要里面传递一个列表进行完成

pandas画图

对象.plot()
kind --
line --折线图
bar
barh --条形图旋转
hist
pie
scatter

文件读取与存储

1.csv
	读取--pd.read_csv
		参数:
			usecols --需要哪列
	存储 --对象.to_csv
		参数:
			columns --保存哪列
2.hdf
	读取 --pd.read_hdf()
	写入 --对象.to_hdf()
		注意:保存文件是 ....。h5
3.json
	读取 --pd.read_json()
	写入--对象.to_json()
		参数:
			orient --
			按照什么方式进行读取或者写入
			lines --是否按照行读取和写入

缺失值

判断数据是否为NAN:
np.any(pd.isnull(movie))#里面如果有一个缺失值,就返回True
np.all(pd.notnull(movie))#里面如果有一个缺失值,就返回False
处理方式:
存在缺失值nan,并且是np.nan:
删除存在的缺失值的dropna(axis='rows')
注:不会修改原数据,需要接受返回值
替换缺失值:fillna(value,inplace=True)
value:替换成的值
inplace:True:会修改原数据,False:不替换修改原数据,生成新的对象不是缺失值nan,有默认标记的
对象.replace()
to_replace --替换前的值
value --替换后的值

K-近邻算法简介

Scikit-learn工具

安装

pip install scikit-learn==0.19.1

k近邻算法api初步使用

from sklearn.neighbors import KNeighborsClassifier
#获取数据
x=[[1],[2],[0],[0]]
y=[1,1,0,0]
#机器学习
#1.实例化一个训练模型
estimator=KNeighborsClassifier(n_neighbors=2)#n_neighbors选定参考几个邻居
#2.调用fit方法进行训练
estimator.fit(x,y)
#预测其他值
ret=estimator.predict([[100]])
print(ret)

pytorch学习

pytorch的安装

https://pytorch.org/get_started/locally
带GPU安装步骤
conda install pytorch torchvision cudatoolkit=9.0 -c pytorch
不带GPU安装步骤
conda install pytorch-cpu torchvision-cpu -c pytorch

pytorch中创建张量

import torch
import numpy as np
#使用python中的列表或者序列创建tensor
t1=torch.Tensor([1,2,3])
print(t1)
#使用numpy中的数组创建tensor
array=np.arange(12).reshape(3,4)
t2=torch.Tensor(array)
print(t2)
#使用torch的api创建tensor
print(torch.empty(3,4))#创建3行4列的空的tensor
print(torch.ones([3,4]))#创建3行4列的全为1的tensor
print(torch.zeros([3,4]))#创建3行4列的全为0的tensor
print(torch.rand([3,4]))#创建3行4列的随机值的tensor,随机值的区间是[0,1)
print(torch.randint(low=0,high=10,size=[3,4]))#创建3行4列的随机整数的tensor,随机值的区间是[low,high)
print(torch.randn([3,4]))#创建3行4列的随机数的tensor,随机数的tensor,随机值的分布式均值为0,方差为1

image

张量的方法和属性

import torch
import numpy as np
t1=torch.Tensor([[[1]]])
t2=torch.Tensor([[[1,2]]])
print(t1.item())#获取tensor中的数据(当tensor中只有一个元素可用)
print(t2.numpy())#转为numpy数组
print(t2.size())#获取形状
print(t2.view(2))#类似numpy中的reshape,是一种浅拷贝,仅仅是形状发生改变
print(t2.dim())#获取阶数
print(t2.max())#获取最大值
print(t2.t()) #t2.transpose()转置二维///transpose(1,2) permute(0,2,1)

image

pytorchAPL的使用

import torch
import torch.nn as nn
from torch.optim import SGD
import matplotlib.pyplot as plt
#0.准备数据
x=torch.rand([500,1])
y_true=3 * x +0.8
#1.定义模型
class MyLinear(nn.Module):
    def __init__(self):
        #继承父类的init
        super(MyLinear,self).__init__()
        self.linear=nn.Linear(1,1)
    def forward(self,x):
        out=self.linear(x)
        return out
#2.实例化模型,优化器类实例化,loss实例化
my_linear=MyLinear()
optimizer=SGD(my_linear.parameters(),0.001)
loss_fn=nn.MSELoss()
#3.循环,进行梯度下降,参数的更新
for i in range(2000):
    #得到预测值
    y_predict=my_linear(x)
    loss=loss_fn(y_predict,y_true)
    #梯度置为0
    optimizer.zero_grad()
    #反向传播
    loss.backward()
    #参数的跟新
    optimizer.step()
    if i%50==0:
        params=list(my_linear.parameters())
        print(loss.item(),params[0].item(),params[1].item())
#4.模型评估
my_linear.eval()#设置模型为评估模式,即预测模式
#my_linear.train(mode=True)#表示设置模型为训练模式
predict=my_linear(x)
predict=predict.data.numpy()
plt.scatter(x.data.numpy(),y_true.data.numpy(),c='r')
plt.plot(x.data.numpy(),predict)
plt.show()

image

在GPU上运行

import torch
import torch.nn as nn
from torch.optim import SGD
import matplotlib.pyplot as plt
#定义一个device对象
deivce=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#0.准备数据
x=torch.rand([500,1]).to(deivce)
y_true=3 * x +0.8
y_true=y_true.to(deivce)
#1.定义模型
class MyLinear(nn.Module):
    def __init__(self):
        #继承父类的init
        super(MyLinear,self).__init__()
        self.linear=nn.Linear(1,1)
    def forward(self,x):
        out=self.linear(x)
        return out
#2.实例化模型,优化器类实例化,loss实例化
my_linear=MyLinear().to(deivce)
optimizer=SGD(my_linear.parameters(),0.001)
loss_fn=nn.MSELoss()
#3.循环,进行梯度下降,参数的更新
for i in range(2000):
    #得到预测值
    y_predict=my_linear(x)
    print(y_predict)
    loss=loss_fn(y_predict,y_true)
    #梯度置为0
    optimizer.zero_grad()
    #反向传播
    loss.backward()
    #参数的跟新
    optimizer.step()
    if i%50==0:
        params=list(my_linear.parameters())
        print(loss.item(),params[0].item(),params[1].item())

opencv学习

中文教程

安装

pip install opencv-contrib-python 最新版安装失败
pip install opencv-python==4.3.0.38

初步使用

import cv2
import numpy as np
if __name__ == '__main__':
    flower=cv2.imread('./1.webp')
    # print(flower.shape)
    # print(type(flower))
    # print(flower)
    cv2.imshow('flower',flower)#弹出窗口
    cv2.waitKey()#行待键盘输入,任意输入就可以关闭
    cv2.destroyAllWindows()#销毁内存

尺寸和颜色

import cv2
import numpy as np
if __name__ == '__main__':
    flower=cv2.imread('./1.webp')
    flower=cv2.resize(flower,(305,305))#图片尺寸的改变
    gray=cv2.cvtColor(flower,code=cv2.COLOR_BGR2GRAY)#黑白图片
    cv2.imshow('flower',gray)#弹出窗口
    cv2.waitKey(0)#0无限等待,1000毫秒=1秒这后,自动消失
    cv2.destroyAllWindows()#销毁内存

image

hsv颜色

import cv2
import numpy as np
if __name__ == '__main__':
    flower=cv2.imread('./1.webp')
    hsv=cv2.cvtColor(flower,code=cv2.COLOR_BGR2HSV)
    #定义在HSV颜色空间中蓝色的范围
    lower_blue=np.array([110,50,50])#浅蓝色
    upper_blue=np.array([130,255,255])#深蓝色
    #根据蓝色的范围,标记图片中哪些位置是蓝色
    mask=cv2.inRange(hsv,lower_blue,upper_blue)
    print(mask)
    cv2.imshow('hsv的使用',hsv)#弹出窗口
    cv2.waitKey(0)#0无限等待,1000毫秒=1秒这后,自动消失
    cv2.destroyAllWindows()#销毁内存

image

图片马赛克

import cv2
import numpy as np
if __name__ == '__main__':
    flower=cv2.imread('./1.webp')
    #模糊
    flower2=cv2.resize(flower,(35,23))
    flower3=cv2.resize(flower2,(500,500))
    cv2.imshow('马赛克的使用',flower3)#弹出窗口
    cv2.waitKey(5000)#0无限等待,1000毫秒=1秒这后,自动消失
    cv2.destroyAllWindows()#销毁内存
    #方式一
    # flower2=cv2.resize(flower,(35,23))
    # flower3=np.repeat(flower2,10,axis=0)#repeat重复
    # flower4=np.repeat(flower3,10,axis=1)
    # cv2.imshow('马赛克的使用',flower4)#弹出窗口
    # cv2.waitKey(5000)#0无限等待,1000毫秒=1秒这后,自动消失
    # cv2.destroyAllWindows()#销毁内存

    #方式二
    # flower2=flower[::10,::10]#第10个中取出一个像素
    # cv2.namedWindow('马赛克的使用',flags=cv2.WINDOW_NORMAL)
    # cv2.resizeWindow('马赛克的使用',500,500)
    # cv2.imshow('马赛克的使用',flower2)
    # cv2.waitKey(0)

image
image

人脸马赛克

import cv2
import numpy as np
if __name__ == '__main__':
    belle=cv2.imread('./2.webp')
    #人脸左上角坐标(160,145)右下角坐标(310,300)(宽度,高度)
    face=belle[145:300,160:310]
    face=face[::10,::10]
    face=np.repeat(face,10,axis=0)
    face=np.repeat(face,10,axis=1)
    belle[145:300,160:310]=face[:155,:150]#填充,尺寸一致
    cv2.imshow('美女',belle)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

image

人脸检测

资料下载

import cv2
import numpy as np
if __name__ == '__main__':
    belle=cv2.imread('./2.webp')
    #人脸特征详细说明
    face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    faces=face_detector.detectMultiScale(belle)#坐标x,y,w,h
    print(faces)
    for x,y,w,h in faces:
        cv2.rectangle(belle,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)#矩形
    cv2.imshow('人脸检测',belle)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


image

多张人脸检测

import cv2
import numpy as np
if __name__ == '__main__':
    belles=cv2.imread('./3.jpeg')
    # gray=cv2.cvtColor(belles,code=cv2.COLOR_BGR2GRAY)#减少数据
    #人脸特征详细说明
    face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    faces=face_detector.detectMultiScale(belles,
                                         scaleFactor=1.1,#缩放
                                         minNeighbors=1,#

                                         )#坐标x,y,w,h
    print(faces)
    for x,y,w,h in faces:
        # cv2.rectangle(belles,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)#矩形
        cv2.circle(belles,center=(x+w//2,y+h//2),
                   radius=w//2,
                   color=[0,255,0],
                   thickness=2)#圆
    cv2.imshow('人脸检测',belles)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


image

人脸贴纸画

import cv2
import numpy as np

if __name__ == '__main__':
    belle = cv2.imread('./2.webp')
    gray = cv2.cvtColor(belle, code=cv2.COLOR_BGR2GRAY)
    face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    faces = face_detector.detectMultiScale(gray)
    star = cv2.imread('./4.webp')
    for x, y, w, h in faces:
        # cv2.rectangle(belle,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)
        # belle[y:y+h//4,x+(3*w)//8-10:x+(3*w)//8-10+w//4]=cv2.resize(star,(w//4,h//4))
        star_s = cv2.resize(star, (w // 4, h // 4))
        w1 = w // 4
        h1 = h // 4
        for i in range(h1):
            for j in range(w1):
                if not (star_s[i, j] > 180).all():  # 显示红色
                    belle[i + y, j + x + 3 * w // 8 - 10] = star_s[i, j]
    cv2.imshow('face painting', belle)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

image

轮廓绘制

import cv2
import numpy as np

if __name__ == '__main__':
    belle=cv2.imread('images.jpg')
    hsv=cv2.cvtColor(belle,code=cv2.COLOR_BGR2HSV)
    #跟据颜色的值进行查找
    lower_red=(156,50,50)#浅红色
    upper_red=(180,255,255)#深红色
    mask=cv2.inRange(hsv,lower_red,upper_red)
    #手工绘制
    # h,w,c=belle.shape
    # mask=np.zeros((h,w),dtype=np.uint8)
    # x_data=np.array([124,169,208,285,307,260,175])+50
    # y_data=np.array([205,124,135,173,216,311,309])+10
    # pts=np.c_[x_data,y_data]#横纵坐标合并,点(x,y)
    # cv2.fillPoly(mask,[pts],(255),8,0)
    # res=cv2.bitwise_and(belle,belle,mask=mask)
    cv2.imshow('belle',mask)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

image

轮廓边界

import cv2
import numpy as np

if __name__ == '__main__':
    belle=cv2.imread('2.webp')
    gray=cv2.cvtColor(belle,cv2.COLOR_BGR2GRAY)
    gray2=cv2.GaussianBlur(gray,(5,5),1)
    canny=cv2.Canny(gray2,75,200)
    cv2.imshow('belle',canny)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

image

人脸轮廓替换

import cv2
import numpy as np

if __name__ == '__main__':
    belle=cv2.imread('2.webp')
    dog=cv2.imread('5.jpg')
    face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    belle_gray=cv2.cvtColor(belle,code=cv2.COLOR_BGR2GRAY)
    dog_gray=cv2.cvtColor(dog,code=cv2.COLOR_BGR2GRAY)
    #狗二进制图片,黑白
    threshold,binary=cv2.threshold(dog_gray,50,255,cv2.THRESH_OTSU)
    print(threshold)
    contours,hierarchy=cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    areas=[]
    for contour in contours:
        areas.append(cv2.contourArea(contour))
    areas=np.asarray(areas)
    index=areas.argsort()#从小到大,进行排序
    mask=np.zeros_like(dog_gray,dtype=np.uint8)
    mask=cv2.drawContours(mask,contours,index[-2],(255,255,255),thickness=-1)
    faces=face_detector.detectMultiScale(belle)
    for x,y,w,h in faces:
        mask2=cv2.resize(mask,(w,h))
        dog_gray2=cv2.resize(dog_gray,(w,h))
        for i in range(h):
            for j in range(w):
                if not (mask2[i,j]==255).all():
                    belle[i+y,j+x]=dog_gray2[i,j]
    cv2.imshow('belle',belle)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

image

直方图均匀化处理

pip install scikit-image 图片处理库

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
if __name__ == '__main__':
    moon=data.moon()
    # plt.hist(moon.ravel(),bins=256)
    # plt.show()
    # moon2=cv2.equalizeHist(moon)#直方图均衡化
    # plt.hist(moon2.reshape(-1),bins=256)
    # plt.show()
    hist=cv2.calcHist([moon],[0],None,[256],[0,256])
    print(hist)
    print(hist.shape)
    plt.plot(hist)
    plt.show()
    cv2.imshow('moon',moon)
    # cv2.imshow('moon',moon2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

image
image
image
image
image

视频中人脸识别

import cv2
if __name__ == '__main__':
    video=cv2.VideoCapture('./1.mp4')
    face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    while True:
        retval,image=video.read()#retval 布尔值表明是否获得图片
        image=cv2.resize(image,(640,360))
        if retval==False:#取了最后一张,再读取,就没有了
            print('视频读取完毕')
            break
        gray=cv2.cvtColor(image,code=cv2.COLOR_BGR2GRAY)
        faces=face_detector.detectMultiScale(gray)
        for x,y,w,h in faces:
            cv2.rectangle(image,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)
        cv2.imshow('ttnk',image)
        key=cv2.waitKey(1)
        if key==ord('q'):
            print('用户键盘输入了q,程序退出')
            break
    print(image.shape)
    cv2.destroyAllWindows()
    video.release()#释放内存

image

视频人脸马赛克和属性

import cv2
import numpy as np
if __name__ == '__main__':
    video=cv2.VideoCapture('./1.mp4')
    fps=video.get(propId=cv2.CAP_PROP_FPS)#视频帧率
    width=video.get(propId=cv2.CAP_PROP_FRAME_WIDTH)#视频宽度
    height=video.get(propId=cv2.CAP_PROP_FRAME_HEIGHT)#视频高度
    count=video.get(propId=cv2.CAP_PROP_FRAME_COUNT)#视频帧率
    print(fps,width,height,count)
    face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    while True:
        retval,image=video.read()#retval 布尔值表明是否获得图片
        image=cv2.resize(image,(640,360))
        if retval==False:#取了最后一张,再读取,就没有了
            print('视频读取完毕')
            break
        gray=cv2.cvtColor(image,code=cv2.COLOR_BGR2GRAY)
        faces=face_detector.detectMultiScale(gray)
        for x,y,w,h in faces:
            # cv2.rectangle(image,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)
            face=image[y:y+h,x:x+w]
            face=face[::10,::10]
            face=np.repeat(np.repeat(face,10,axis=0),10,axis=1)
            image[y:y+h,x:x+w]=face[:h,:w]
        cv2.imshow('ttnk',image)
        key=cv2.waitKey(1)
        if key==ord('q'):
            print('用户键盘输入了q,程序退出')
            break

    cv2.destroyAllWindows()
    video.release()#释放内存

image

视频保存

import cv2
import numpy as np
if __name__ == '__main__':
    video=cv2.VideoCapture('./2.mp4')
    face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    writer=cv2.VideoWriter(filename='./a.mp4',
                    fourcc=cv2.VideoWriter.fourcc(*'MP4V'),#视频编码
                    fps=24,#帧率
                    frameSize=(640,360)#图片尺寸
                    )
    while True:
        retval,image=video.read()#retval 布尔值表明是否获得图片
        # image=cv2.resize(image,(640,360))
        if retval==False:#取了最后一张,再读取,就没有了
            print('视频读取完毕')
            break
        gray=cv2.cvtColor(image,code=cv2.COLOR_BGR2GRAY)#黑白,二维
        gray2=np.repeat(gray.reshape(360,640,1),3,axis=1)#蓝绿红
        writer.write(gray2)
        faces=face_detector.detectMultiScale(gray)
        for x,y,w,h in faces:
            # cv2.rectangle(image,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)
            face=image[y:y+h,x:x+w]
            face=face[::10,::10]
            face=np.repeat(np.repeat(face,10,axis=0),10,axis=1)
            image[y:y+h,x:x+w]=face[:h,:w]
        cv2.imshow('ttnk',image)
        key=cv2.waitKey(1)
        if key==ord('q'):
            print('用户键盘输入了q,程序退出')
            break

    cv2.destroyAllWindows()
    video.release()#释放内存
    writer.release()

pydub音频的使用

教程

初步使用

import pydub
if __name__ == '__main__':
    mp3=pydub.AudioSegment.from_mp3('./1.mp3')
    print(mp3)
    m=mp3[:21*1000]
    m.export('./2.mp3')

音视频合成

import subprocess
if __name__ == '__main__':
    #将视频和音频进行合并
    cmd='ffmpeg -i 2.mp4 -i 2.mp3 out.mp4'
    c=subprocess.call(cmd,shell=True)
    print('0成功,反之不成功',c)

摄像头人脸识

import numpy as np
import cv2
if __name__ == '__main__':
    cap=cv2.VideoCapture(0)#打开本机的摄像头
    while True:
        flag,frame=cap.read()#flag是否读取了图片
        if not flag:
            break
        gray=cv2.cvtColor(frame,code=cv2.COLOR_BGR2GRAY)
        cv2.imshow('face',frame)
        key=cv2.waitKey(1000//24)
        if key==ord('q'):
            break
        cv2.destroyAllWindows()
        cap.release()

腐蚀和膨胀

import numpy as np
import cv2
if __name__ == '__main__':
    img=cv2.imread('./images2.jpg',flags=cv2.IMREAD_GRAYSCALE)
    img=(img-255)*255
    kernel=np.ones(shape=[5,5],dtype=np.uint8)
    #腐蚀,由多变少
    img2=cv2.erode(img,kernel=kernel,iterations=1)
    cv2.imshow('erode',img)
    cv2.imshow('erode2',img2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

image

import numpy as np
import cv2
if __name__ == '__main__':
    img=cv2.imread('./images2.jpg',flags=cv2.IMREAD_GRAYSCALE)
    img=(img-255)*255
    kernel=np.ones(shape=[5,5],dtype=np.uint8)
    #腐蚀,由多变少,越是边界上,越容易被腐蚀,去除噪声,图像变小,变细
    img2=cv2.erode(img,kernel=kernel,iterations=1)
    #膨胀,图像变粗,变大
    img3=cv2.dilate(img2,kernel,iterations=1)
    cv2.imshow('raw',img)#原图
    cv2.imshow('erode',img2)#腐蚀
    cv2.imshow('dilate',img3)#膨胀,还原(噪声去掉还原)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

image

形态操作

import numpy as np
import cv2
if __name__ == '__main__':
    img=cv2.imread('./images2.jpg',flags=cv2.IMREAD_GRAYSCALE)
    img=(img-255)*255
    #开运算
    result=cv2.morphologyEx(img,op=cv2.MORPH_OPEN,kernel=np.ones(shape=[5,5],
                                                                 dtype=np.uint8,
                                                                 ),iterations=1)
    #闭运算
    result2=cv2.morphologyEx(img,op=cv2.MORPH_CLOSE,kernel=np.ones(shape=[10,10],dtype=np.uint8),
                             iterations=1)
    #形态学梯度
    result3 = cv2.morphologyEx(img, op=cv2.MORPH_GRADIENT, kernel=np.ones(shape=[5, 5],
                                                                     dtype=np.uint8,
                                                                     ), iterations=1)
    cv2.imshow('raw',img)#原图
    cv2.imshow('morphologyEx',result)
    cv2.imshow('morphologyEx2',result2)
    cv2.imshow('morphologyEx3',result3)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

车牌号识别

资料下载

import numpy as np
import cv2
if __name__ == '__main__':
   car=cv2.imread('./car.png')
   gray=cv2.cvtColor(car,code=cv2.COLOR_BGR2GRAY)
   car_detector=cv2.CascadeClassifier('./haarcascade_russian_plate_number.xml')
   # plates=car_detector.detectMultiScale(gray)
   plates = car_detector.detectMultiScale(gray,
                                          scaleFactor=1.05,  # 缩放
                                          minNeighbors=1,  #

                                          )  # 坐标x,y,w,h
   for x,y,w,h in plates:
       cv2.rectangle(car,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)
   cv2.imshow('plates',car)
   cv2.waitKey(0)
   cv2.destroyAllWindows()

image

车牌号定位

import numpy as np
import cv2
def lpr(filename):
    img=cv2.imread(filename)
    gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#灰度化处理
    GaussianBlur_img=cv2.GaussianBlur(gray_img,(3,3),0)#高斯平滑
    Sobel_img=cv2.Sobel(GaussianBlur_img,-1,1,0,ksize=3)#提取边界
    ret,binary_img=cv2.threshold(Sobel_img,127,255,cv2.THRESH_BINARY)#二值化操作
    #形态学运算
    kernel=np.ones((5,15),np.uint8)
    #先闭运算将车牌数字部分连接,再开运算将不是块状的或是较小的部分去掉
    close_img=cv2.morphologyEx(binary_img,cv2.MORPH_CLOSE,kernel)
    open_img=cv2.morphologyEx(close_img,cv2.MORPH_OPEN,kernel)
    #由于部分图像得到的轮廓边缘不整齐,因此再进行一次膨胀操作
    element=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
    dilation_img=cv2.dilate(open_img,element,iterations=3)
    #获取轮廓
    contours,hierarchy=cv2.findContours(dilation_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    rectangles=[]
    for c in contours:
        x=[]
        y=[]
        for point in c:
            y.append(point[0][0])
            x.append(point[0][1])
        r=[min(y),min(x),max(y),max(x)]
        rectangles.append(r)
    #用颜色识别出车牌区域
    dist_r=[]
    max_mean=0
    for r in rectangles:
        block=img[r[1]:r[3],r[0]:r[2]]
        hsv=cv2.cvtColor(block,cv2.COLOR_BGR2HSV)
        low=np.array([100,43,46])
        up=np.array([124,255,255])
        result=cv2.inRange(hsv,low,up)
        #用计算均值的方式
        mean=np.mean(result)
        if mean > max_mean:
            max_mean=mean
            dist_r=r
    #画出识别结果
    cv2.rectangle(img,(dist_r[0]+3,dist_r[1]),(dist_r[2]-3,dist_r[3]),(0,255,0),2)
    cv2.imshow('lpr',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
if __name__ == '__main__':
    path='./car2.jpg'
    lpr(path)

image

HSV颜色查询

image

posted @ 2024-02-01 11:46  自由的飞翔666  阅读(19)  评论(0编辑  收藏  举报