Plt_tk

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

# coding:utf-8
""" 从视频读取帧保存为图片"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
def line():
    # plt.show(block=False)
    # plt.pause(1)
    # plt.close("all")

    x=np.linspace(-1,1,50)  #定义 x 范围
    y1=2*x+1  #定义 y 范围
    y2=x**2
    plt.figure()  #定义 图像 窗口
    plt.plot(x,y1)  #画出  曲线
    plt.plot(x,y2)
	#自关闭
    plt.show(block=False)
    plt.pause(5)#暂定3秒
    plt.close("all")

def line_quxian():
    plt.close("all")
    x = np.linspace(-3, 3, 50)
    y = 2 * x + 1
    plt.figure(num=1, figsize=(8, 5))
    plt.plot(x, y)

    # 移动坐标轴
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data', 0))
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data', 0))

    # 标注信息
    x0 = 1
    y0 = 2 * x0 + 1
    plt.scatter(x0, y0, color='b')
    plt.plot([x0, x0], [y0, 0], 'k--', lw=2.5)  # 连接两点,k表示黑色,lw=line weight 线粗细

    plt.annotate(r'$2x0+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30), textcoords='offset points',
                 fontsize=16,
                 arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=.2'))
    # xycoords='data'  基于数据值--选位置,xytext=(+30,-30),对标注位置描述,textcoords='offset points',xy偏差值,arrowprops---图中箭头类型
    plt.text(-3.7, 3, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$', fontdict={
    
    'size': 16, 'color': 'r'})

    plt.show(block=False)
    plt.pause(5)
    plt.close("all")

def Scatter():
    n = 1024
    X = np.random.normal(0, 1, n)
    Y = np.random.normal(0, 1, n)
    T = np.arctan2(Y, X)  # arctan2  返回给定X、Y值反正切值
    # scatter画散点图,size=75,颜色T,透明度50%,用xticks函数---隐藏x坐标轴
    plt.scatter(X, Y, s=75, c=T, alpha=0.5)
    plt.xlim(-1.5, 1.5)
    plt.xticks(())  # 忽略xticks
    plt.ylim(-1.5, 1.5)
    plt.yticks(())  # 忽略yticks
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
def bar():
    n = 12
    X = np.arange(n)
    Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1, n)
    Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1, n)
    plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
    plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')

    # 标记值
    for x, y in zip(X, Y1):  # zip表示可以传递两个值
        plt.text(x + 0.4, y + 0.5, '%.2f' % y, ha='center', va='bottom')  # ha表示横向对齐,bottom表示向下对齐
    for x, y in zip(X, Y2):
        plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top')
    plt.xlim(-0.5, n)
    plt.xticks(())
    plt.ylim(-1.25, 1.25)
    plt.yticks(())

    plt.show(block=False)
    plt.pause(5)
    plt.close("all")

def  Contours():
    n = 256
    x = np.linspace(-3, 3, n)
    y = np.linspace(-3, 3, n)
    X, Y = np.meshgrid(x, y)  # 从坐标向量返回坐标矩阵

    # 函数用来计算高度值,利用contour函数把颜色加进去,位置参数依次为x,y,f(x,y),透明度为0.75,并将f(x,y)的值对应到camp之中
    def f(x, y):
        return (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)

    plt.contourf(X, Y, f(X, Y), 8, alpha=0.75, cmap=plt.cm.hot)  # 8表示等高线分成多少份,alpha表示透明度,cmap表示color map

    C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=0.5)
    plt.clabel(C, inline=True, fontsize=10)
    plt.xticks(())  # 隐藏坐标轴
    plt.yticks(())

    # plt.show()
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
def Image():
    import matplotlib.pyplot as plt
    # 利用matplotlib打印出图像
    a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
                  0.365348418405, 0.439599930621, 0.525083754405,
                  0.423733120134, 0.525083754405, 0.651536351379]).reshape(3, 3)
    plt.imshow(a, interpolation='nearest', cmap='bone', origin='lower')  # origin='lower'代表就是选择原点位置
    plt.colorbar(shrink=.92)  # shrink参数是将图片长度变为原来的92%
    plt.xticks(())
    plt.yticks(())
    plt.show()
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
def D3():
    from mpl_toolkits.mplot3d import Axes3D   #需要导入模块Axes3D
    fig=plt.figure() #定义图像窗口
    ax=Axes3D(fig)  #在窗口上添加3D坐标轴
    #将x和y值编织成栅格
    X=np.arange(-4,4,0.25)
    Y=np.arange(-4,4,0.25)
    X,Y=np.meshgrid(X,Y)
    R=np.sqrt(X**2+Y**2)
    Z=np.sin(R)  #高度值

    #将colormap ranbow填充颜色,之后将三维图像投影到XY平面做等高线图,其中rstride和cstride表示row和column的宽度
    ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow')) #rstride表示图像中分割线的跨图

    #添加XY平面等高线 投影到Z平面
    ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap=plt.get_cmap('rainbow')) #把图像进行投影的图形 offset表示比0坐标轴低两个位置
    ax.set_zlim(-2,2)
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
    
def Subplot_jun():
    plt.figure()
    plt.subplot(2, 2, 1)  # 表示将整个图像分割成2行2列,当前位置为1
    plt.plot([0, 1], [0, 1])  # 横坐标变化为[0,1] 竖坐标变化为[0,2]

    plt.subplot(2, 2, 2)
    plt.plot([0, 1], [0, 2])

    plt.subplot(2, 2, 3)
    plt.plot([0, 1], [0, 3])

    plt.subplot(2, 2, 4)
    plt.plot([0, 1], [0, 4])
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
def Subplot_bujun():
    plt.figure()
    plt.subplot(2, 1, 1)  # 表示将整个图像分割成2行1列,当前位置为1
    plt.plot([0, 1], [0, 1])  # 横坐标变化为[0,1] 竖坐标变化为[0,2]

    plt.subplot(2, 3, 4)
    plt.plot([0, 1], [0, 2])

    plt.subplot(2, 3, 5)
    plt.plot([0, 1], [0, 3])

    plt.subplot(2, 3, 6)
    plt.plot([0, 1], [0, 4])
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
def Subplot_fenge():
    import matplotlib.gridspec as gridspec  # 引入新模块
    plt.figure()
    # 使用plt.subplot2grid创建一个小图,(3,3)表示将整个图像分割成三行三列,(0,0)表示从第0行0列开始作图,
    # colspan=3表示列的跨度为3.colspan和rowspan缺省时默认跨度为1
    ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
    ax1.plot([1, 2], [1, 2])
    ax1.set_title('ax1_title')  # 设置图的标题

    # 将图像分割成3行3列,从第1行0列开始作图,列的跨度为2
    ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)

    # 将图像分割成3行3列,从第1行2列开始作图,行的跨度为2
    ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)

    # 将图像分割成3行3列,从第2行0列开始作图,行与列的跨度默认为1
    ax4 = plt.subplot2grid((3, 3), (2, 0))
    ax4.scatter([1, 2], [2, 2])
    ax4.set_xlabel('ax4_x')
    ax4.set_ylabel('ax4_y')

    ax5 = plt.subplot2grid((3, 3), (2, 1))
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
    '''
    plt.figure()
    gs=gridspec.GridSpec(3,3)  #将图像分割成三行三列
    ax6=plt.subplot(gs[0,:]) #gs[0:1]表示图占第0行和所有列
    ax7=plt.subplot(gs[1,:2]) #gs[1,:2]表示图占第1行和前两列
    ax8=plt.subplot(gs[1:,2]) #gs[1,:]表示图占后两行的最后一列
    ax9=plt.subplot(gs[-1,0]) 
    ax10=plt.subplot(gs[-1,-2]) #gs[-1,-2]表示这个图占倒数第一行和倒数第2列
    plt.show()

    '''
    '''
    #建立一个2行2列的图像窗口,sharex=True表示共享x轴坐标,sharey=True表示共享y轴坐标,
    #((ax11,ax12),(ax13,ax14))表示从左到右一次存放ax11,ax12,ax13,ax14
    f,((ax11,ax12),(ax13,ax14))=plt.subplots(2,2,sharex=True,sharey=True)
    ax11.scatter([1,2],[1,2]) #坐标范围x为[1,2],y为[1,2]
    plt.tight_layout() #表示紧凑显示图像
    plt.show()

    '''
def huazhonghua():
    fig = plt.figure()
    # 创建数据
    x = [1, 2, 3, 4, 5, 6, 7]
    y = [1, 3, 4, 2, 5, 8, 6]

    # 绘制大图:设大图大小为10,大图被包含在由(1,1)开始,宽8高8的坐标系中
    left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
    ax1 = fig.add_axes([left, bottom, width, height])  # main axes
    ax1.plot(x, y, 'r')  # 绘制大图,颜色为red
    ax1.set_xlabel('x')  # 横坐标名称为x
    ax1.set_ylabel('y')
    ax1.set_title('title')  # 图名称为title

    # 绘制小图,注意坐标系位置和大小的改变
    ax2 = fig.add_axes([0.2, 0.6, 0.25, 0.25])
    ax2.plot(y, x, 'b')  # 颜色为bule
    ax2.set_xlabel('x')
    ax2.set_title('title inside 1')

    # 绘制第二个小图
    plt.axes([0.6, 0.2, 0.25, 0.25])
    plt.plot(y[::-1], x, 'g')  # 将y进行逆序
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('title inside 2')
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
def ci_zuobiao():
    x = np.arange(0, 10, 0.1)
    y1 = 0.5 * x ** 2
    y2 = -1 * y1
    fig, ax1 = plt.subplots()

    ax2 = ax1.twinx()  # 镜像显示
    ax1.plot(x, y1, 'g-')
    ax2.plot(x, y2, 'b-')

    ax1.set_xlabel('x data')
    ax1.set_ylabel('Y1 data', color='g')  # 第一个y坐标轴
    ax2.set_ylabel('Y2 data', color='b')  # 第二个y坐标轴
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
def donghua():
    from matplotlib import pyplot as plt
    from matplotlib import animation
    import numpy as np
    fig, ax = plt.subplots()
    # 数据是一个0~2π正弦曲线
    x = np.arange(0, 2 * np.pi, 0.01)
    line, = ax.plot(x, np.sin(x))

    # 2、构造自定义动画函数animate,用来更新每一帧上各个x对应y坐标值,参数表示第i帧
    def animate(i):
        line.set_ydata(np.sin(x + i / 10.0))
        return line,

    # 3、构造开始帧函数init
    def init():
        line.set_ydata(np.sin(x))
        return line,

    # 调用FuncAnimation函数生成动画。参数说明:
    # fig 动画绘制的figure
    # func 自定义动画函数,即传入刚定义函数animate
    # frames 动画长度,一次循环包含帧数
    # init_func 自定义开始帧,即传入刚定义函数init
    # interval 更新频率,以ms计
    # blit 选择更新所有点,或仅更新产生变化点。应选择True,mac用户请选择False,否则无法显示

    ani = animation.FuncAnimation(fig=fig,
                                  func=animate,
                                  frames=100,
                                  init_func=init,
                                  interval=20,
                                  blit=False)
    plt.show(block=False)
    plt.pause(5)
    plt.close("all")
    # 效果曲线向左移动
    # 可将动画以mp4格式保存下来,首先要保证安装ffmpeg 或者mencoder
    # ani.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])


if __name__=='__main__':
    # line()
    from tkinter import *
    root = Tk()
    root.geometry('800x600')
    root.wm_title('识别')

    menubar_1 = Menu(root)#定义主菜单1

    filemenu_1 = Menu(menubar_1, tearoff=False)# 主菜单--加入下拉窗口filemenu_1
    filemenu_1.add_command(label="line", command=line)
    filemenu_1.add_command(label="line_quxian", command=line_quxian)
    filemenu_1.add_command(label="Scatter", command=Scatter)
    filemenu_1.add_command(label="bar", command=bar)
    filemenu_1.add_command(label="Contours", command=Contours)
    filemenu_1.add_command(label="Image", command=Image)
    filemenu_1.add_command(label="D3", command=D3)
    filemenu_1.add_command(label="Subplot_jun", command=Subplot_jun)
    filemenu_1.add_command(label="Subplot_bujun", command=Subplot_bujun)
    filemenu_1.add_command(label="Subplot_fenge", command=Subplot_fenge)
    filemenu_1.add_command(label="huazhonghua", command=huazhonghua)
    filemenu_1.add_command(label="ci_zuobiao", command=ci_zuobiao)



    filemenu_1.add_command(label="donghua", command=donghua)

    menubar_1.add_cascade(label="file", menu=filemenu_1)  # 下拉菜单filemenu_1绑定到主菜单
    #
    root.config(menu=menubar_1)#主菜单绑定到主页面
    root.mainloop()

猜你喜欢

转载自blog.csdn.net/ydk001001/article/details/121810424
plt