学习代码:matplotlib

import random
import sys
from matplotlib import pyplot as plt
from PyQt5.QtWidgets import QApplication, QWidget,  QVBoxLayout,QPushButton, QGroupBox, QHBoxLayout, QMenu

class MyWin(QWidget):
    def __init__(self):
        super().__init__()
        self.setUI()

    def setUI(self):
        self.setWindowTitle('统计图matplotlib')
        mainlay = QHBoxLayout()

        # 图形设置图框
        setChartGroup = QGroupBox('图形设置')
        setChartLay = QVBoxLayout()
        setChartGroup.setLayout(setChartLay)
        mainlay.addWidget(setChartGroup)

        # 显示折线图按钮
        lineChartButton = QPushButton("显示拆线图")
        lineChartButton.clicked.connect(self.show_lineChart)
        setChartLay.addWidget(lineChartButton)

        # 绘图设置按钮
        setMatplotlibButton = QPushButton("设置尺寸和保存路径")
        setMatplotlibButton.clicked.connect(self.setSize_Save_Ticks)
        setChartLay.addWidget(setMatplotlibButton)

        # 设置刻度按钮
        ticksButton = QPushButton('设置刻度')
        ticksButton.clicked.connect(self.setChineseFontTicks)
        setChartLay.addWidget(ticksButton)

        # 设置图形信息
        chartInfoButton = QPushButton('设置图形信息')
        chartInfoButton.clicked.connect(self.setChartInfo)
        setChartLay.addWidget(chartInfoButton)

        # 多折线显示
        morelinechartButton = QPushButton('多折线显示')
        morelinechartButton.clicked.connect(self.morelineChart)
        setChartLay.addWidget(morelinechartButton)

        # 更多设置
        moreSetButton = QPushButton('更多设置')
        moreSetButton.clicked.connect(self.moreSet)
        setChartLay.addWidget(moreSetButton)

        # 统计分析图分组框
        statisticalAnalysisChartGroup = QGroupBox('统计分析图表')
        statisticalAnalysisLay = QVBoxLayout()
        statisticalAnalysisChartGroup.setLayout(statisticalAnalysisLay)
        mainlay.addWidget(statisticalAnalysisChartGroup)
        # 散点菜单
        scatterChartButton = QPushButton('散点图')
        scatterChartButton.clicked.connect(self.scatterMenu)
        statisticalAnalysisLay.addWidget(scatterChartButton)

        # 条形图菜单
        barChartButton=QPushButton('条形图')
        barChartButton.clicked.connect(self.setBarChartMenu)
        statisticalAnalysisLay.addWidget(barChartButton)

        # 直方图按钮
        histChartButton=QPushButton('直方图')
        histChartButton.clicked.connect(self.setHistMenu)
        statisticalAnalysisLay.addWidget(histChartButton)

        # 进阶设置按钮
        otherSetButton=QPushButton("进阶设置")
        otherSetButton.clicked.connect(self.otherSetMenu)
        statisticalAnalysisLay.addWidget(otherSetButton)


        self.setLayout(mainlay)

    # 显示拆线图
    def show_lineChart(self):
        x = range(2, 26, 2)
        y = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
        plt.plot(x, y)
        plt.show()

    # 设置尺寸和保存路径
    def setSize_Save_Ticks(self):
        # 1.设置图片尺寸
        plt.figure(figsize=(12, 8), dpi=80)
        x = range(2, 26, 2)
        y = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
        plt.plot(x, y)
        # 设置数字刻度
        plt.yticks(y)
        '''
        设置纯数字刻度的语法
        plt.xticks(x)
        plt.yticks(x)
        刻度值的参数x或y可以设置为任意可以遍历的数字序列
        '''
        # 保存图片
        plt.savefig('lineChart.png')
        plt.show()

    # 设置刻度及中文显示
    def setChineseFontTicks(self):
        plt.figure(figsize=(20, 8), dpi=80)
        # 设置字体中文显示,该方法比如老师教的方法简单
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        '''
        语法:plt.rcParams['font.sans-serif']='中文字体的英语名称'
        常见中中文字体的英语名称:
            黑体: SimHei
            宋体:SimSun
            新宋体: NSimSun
            仿宋:FangSong
            楷体;KaiTi
            仿宋_GB2312; FangSong_GB2312
            楷体_GB2312: KaiTi_GB2312
            微软雅黑体: Microsoft YaHei
            隶书: LiSu
            幼圆: YouYuan
            华文细黑:STXihei
            华文楷体:STKaiti
            华文宋体:STSong
            华文中宋:STZhongsong
            华文仿宋:STFangsong
            方正舒体:FZShuTi
            方正姚体:FZYaoti
            华文彩云:STCaiyun
            华文琥珀:STHupo
            华文隶书:STLiti
            华文行楷:STXingkai
            华文新魏:STXinwei
        '''
        x = range(0, 121)
        y = [random.randint(20, 35) for i in range(121)]
        plt.plot(x, y)

        xNum = range(0, 121, 10)
        xText = ["你好{}".format(i) for i in xNum]
        teststr = list('ABCDEFGHIJKLMN')

        # 设置坐标轴显示带有文字的刻度
        plt.xticks(xNum, xText, rotation=45)
        '''
        xNum,表示要传入的坐标轴数字
        xText,表示在想应的xNum中数字位置要显示的字符串,即要显示的文本
            不定要需要传入带有刻度文字的字符串,也可以传入纯字符串,比如
            teststr中的ABCDEFGHIJKLM,将xText换成变量teststr,即可
            以正常显示,且记,xNum和 xText元素个数一定要一致,否则报错。
        rotation=45,表示刻度旋转45度。
        '''
        plt.show()

    # 设置图形信息
    def setChartInfo(self):
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        x = range(2, 26, 2)
        y = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
        plt.plot(x, y)
        # 设置坐标轴名称
        plt.xlabel('时间')
        plt.ylabel('温度')
        # 设置图表名称
        plt.title('温度变化规律')
        plt.show()

    # 显示多条折线
    def morelineChart(self):
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        x = [1, 2, 3, 4, 5, 6, 7, 8]
        y1 = [6, 2, 8, 9, 12, 17, 7, 6]
        y2 = [4, 9, 1, 3, 5, 11, 8, 10]
        plt.plot(x, y1)
        plt.plot(x, y2)
        plt.show()

    # 更多设置
    def moreSet(self):
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        x = [1, 2, 3, 4, 5, 6, 7, 8]
        y1 = [6, 2, 8, 9, 12, 17, 7, 6]
        y2 = [4, 9, 1, 3, 5, 11, 8, 10]

        plt.plot(x, y1, label="李四")
        # 其他设置:线条图例,线条颜色,线条线型,线条透明度,线条宽度,线条透明
        plt.plot(x, y2, label='张三', color='r', linestyle='-.', linewidth=3, alpha=0.6)
        '''
        label:设置图例名称
        color:设置线条颜色
            颜色参数值的含义:
                'r':红色
                'g':绿色
                'b':蓝色
                'w':白色
                'c':青色
                'm':洋红
                'y':黄色
                'k':黑色
                '16进制颜色标识符',如#FFC0CB表示粉红

        liestyle:设置线条的线型
            线型参数值含义:
                '-':实线
                '--':虚线
                '-.':点划线
                ':':点虚线,虚线
                '空格':无线

        alpa:设置线条的透明度,参数为数值


        '''
        # 设置显示网格线
        plt.grid(alpha=0.2)
        '''
        alpha表示透明度,数值在0~1之间
        '''
        # 显示图例位置
        plt.legend(loc=2)
        '''
        默认显示位置好像是右上角,即不传递参数时显示位置为右上角
        best:0

        upper right:1
        upper left:2

        lower right:3
        lower left:4

        right:5

        center right:6
        center left:7

        lower center:8
        upper center:9
        center:10
        '''
        plt.show()

    # 散点图菜单
    def scatterMenu(self):
        scatterMenu = QMenu()
        scatterChartAction = scatterMenu.addAction('绘制散点图')
        scatterChartAction.triggered.connect(self.scatterChart)

        setScatterChartAction = scatterMenu.addAction('设置散点图')
        setScatterChartAction.triggered.connect(self.setScatterChart)

        setScatterLeftRightChartAction = scatterMenu.addAction('左右两个散点图')
        setScatterLeftRightChartAction.triggered.connect(self.LeftRightScatter)
        scatterMenu.exec()

    # 绘制散点图
    def scatterChart(self):
        temperaturelist = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15,
                           15, 19, 21, 22, 22, 22, 23]
        xaxis = range(1, 32)
        plt.scatter(xaxis, temperaturelist)
        plt.show()

    # 设置散点图
    def setScatterChart(self):
        # 设置尺寸
        plt.figure(figsize=(20,12),dpi=80)
        # 设置字体
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        temperaturelist = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15,
                           15, 19, 21, 22, 22, 22, 23]
        xaxis = range(1, 32)
        plt.scatter(xaxis, temperaturelist)
        # 设置坐标轴
        xnum=[5,10,15,20,25,30]
        xtext=list('3月{}日'.format(i) for i in xnum)
        ynum=[0,3,6,9,12,15,18,21,24,27]
        ytext=list('{}°C'.format(i) for i in ynum)
        plt.xticks(xnum,xtext)
        plt.yticks(ynum,ytext)
        # 设置标题和坐标轴名称
        plt.title('2018年3月份温度变化')
        plt.xlabel('日期')
        plt.ylabel('温度(°C)')
        plt.show()
    # 左右两个散点图
    def LeftRightScatter(self):
        plt.figure(figsize=(20,6),dpi=80)
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        Mar_temperaturelist = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15,
                           15, 19, 21, 22, 22, 22, 23]
        Oct_temperaturelist = [26.26,28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11,
             13, 12, 13, 6]
        Mar_date=range(1,32)
        Oct_date=range(61,91)
        plt.scatter(Mar_date,Mar_temperaturelist)
        plt.scatter(Oct_date,Oct_temperaturelist)
        Mar_Num=[5,10,15,20,15,30]
        Oct_Num=[65,70,75,80,85,90]
        dateNum=Mar_Num+Oct_Num
        dateText=list('3月{}日'.format(i) for i in Mar_Num)+list('10月{}日'.format(i-60) for i in Oct_Num)
        plt.xticks(dateNum,dateText)
        plt.show()
    # 设置条形图菜单
    def setBarChartMenu(self):
        setBarMenu = QMenu()
        barChartAction = setBarMenu.addAction('绘制条形图')
        barChartAction.triggered.connect(self.barChart)

        HbarChartAction=setBarMenu.addAction('横向条形图')
        HbarChartAction.triggered.connect(self.setHBarChart)

        moreBarInChartAction=setBarMenu.addAction('一窗多条')
        moreBarInChartAction.triggered.connect(self.moreBarInChart)


        setBarMenu.exec()
    # 绘制普通条形图
    def barChart(self):
        plt.figure(figsize=(20,8),dpi=80)
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        filmlist=['战狼2','速度与激情8','功夫瑜伽','西游伏魔篇','变形金刚5:最后的骑士','摔跤吧!爸爸','加勒比海盗5:死无对证','金刚:骷髅岛',
           '极限特工:终极回归','生化危机:终章','乘风破浪','神偷奶爸3','智取威虎山','大闹天竺','金刚狼3:殊死一战','蜘蛛侠:英雄归来',
           '悟空传','银河护卫队2','情圣','新木乃伊']
        sellmoney = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99,
             6.88, 6.86, 6.58, 6.23]
        plt.bar(filmlist,sellmoney,width=0.6,color='r')

        '''
        width:表示条形图的宽宽
        color:表示条形图的颜色
        可以采用类似方法绘制横向条形图
        plt.barh(filmlist,sellmoney)
        '''
        plt.xticks(rotation=90)
        # 设置图形的边距
        plt.subplots_adjust(left=0.5,bottom=0.25)
        '''
        6个参数:
        left,bottom,right,top,这四个参数的值好像在0~1之间,这个数值的含义好像是边距占整个绘图界面的尺寸比例
        wspace,hspace这个参数的作用以后再研究
        '''
        plt.show()
    # 绘制横向条形图
    def setHBarChart(self):
        plt.figure(figsize=(20, 8), dpi=80)
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        filmlist = ['战狼2', '速度与激情8', '功夫瑜伽', '西游伏魔篇', '变形金刚5:最后的骑士', '摔跤吧!爸爸',
                    '加勒比海盗5:死无对证', '金刚:骷髅岛',
                    '极限特工:终极回归', '生化危机:终章', '乘风破浪', '神偷奶爸3', '智取威虎山', '大闹天竺',
                    '金刚狼3:殊死一战', '蜘蛛侠:英雄归来',
                    '悟空传', '银河护卫队2', '情圣', '新木乃伊']
        sellmoney = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32,
                     6.99,
                     6.88, 6.86, 6.58, 6.23]
        plt.barh(range(len(filmlist)),sellmoney)
        plt.yticks(range(len(filmlist)),filmlist)
        '''
        水平向条形图可以采用类似的方法传递参数绘制
        plt.bar(range(len(filmlist)),sellmoney)
        plt.xticks(range(len(filmlist)),filmlist)
        '''
        plt.ylim()
        plt.show()
    # 一窗多条,此用法需要加强练习和琢磨
    def moreBarInChart(self):
        plt.figure(figsize=(20,10),dpi=80)
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        filmlist=['猩球崛起3:终极之战','敦刻尔克','蜘蛛侠:英雄归来','战狼2']
        Sep14=[15746,312,4497,319]
        Sep15=[12357, 156, 2045, 168]
        Sep16=[2358, 399, 2358, 362]

        # 设置x轴刻度
        barWidth=0.2
        x14=range(len(filmlist))
        x15=list(i+barWidth for i in x14)
        x16=list(i+barWidth*2 for i in x14)

        plt.bar(x14,Sep14,width=barWidth,label='9月14日')
        plt.bar(x15,Sep15,width=barWidth,label='9月15日')
        plt.bar(x16,Sep16,width=barWidth,label='9月15日')

        plt.xticks(x15, filmlist)  # 只需要在中间一个条形图标名称即可
        plt.legend()
        plt.show()
    # 设置直方图菜单
    def setHistMenu(self):
        setHistMenu=QMenu()
        HistChartAction=setHistMenu.addAction('直方图')
        HistChartAction.triggered.connect(self.histChart)
        HistChartOtherTeatherAction=setHistMenu.addAction('直方图(其他老师)')
        HistChartOtherTeatherAction.triggered.connect(self.histChart_otherTeather)
        setHistMenu.exec()
    def histChart(self):
        # 处理qq识别后的数据str1
        str1='131,98,125,131,124,139,131,117,128,108,135,138,131,102,107,114, 119,128,121,142,127, 130,124,101,110,116, 117,110, 128,128,115,99,136,126,134,95,138,117, 111,78,132,124,113,150,101,117,86,95,144,105,126,130,126,130,126,116,123,106,112,138,123,86,101,99,136,123,117,119,105,137,123,128,125,104,109,134,125,127,105, 120,107, 129, 116, 108,132,103,136,118, 102, 120, 114,105,115, 132,145, 119, 121,112,139, 125, 138,109,132,134,156, 106,117, 127,144,139,139, 119,140,83,110,102,123,107,143,115,136,118,139,123,112, 118,125,109,119,133,112,114,122,109,106,123,116,131,127,115,118,112,135,115,146,137,116,103,144,83,123,111,110, 111,100,154,136,100,118,119,133,134,106,129,126, 110, 111,109,141,120,117,106,149,122,122,110, 118, 127, 121,114,125, 126,114, 140, 103,130, 141,117,106,114,121,114,133,137,92,121, 112, 146,97,137,105,98,117, 112,81,97,139,113,134,106,144, 110,137, 137, 111, 104, 117, 100, 111,101,110,105,129,137, 112,120,113,133,112,83,94,146,133,101,131,116,111,84,137,115,122,106,144,109,123,116,111,111,133,150'
        str2=str1.replace(' ','')
        str3=str2.replace(',',',')
        list1=str3.split(sep=',')
        filmTime=list(eval(i) for i in list1)
        # 设置组数,组距
        '''
        组数:将数据分组,当数据在100以内时,按数据多少,常分为5~12组
        组距:每个小数两个终点的距离
        级数=极差/组距
        '''
        hist_width=3
        hist_num=int((max(filmTime)-min(filmTime))/hist_width)
        # 设置图形尺寸文字
        plt.figure(figsize=(20,8),dpi=80)
        # 绘制直方图
        plt.hist(filmTime,hist_num)
        # 设置刻度
        plt.xticks(range(min(filmTime),max(filmTime)+hist_width,hist_width+5))
        plt.grid()
        plt.show()
    def histChart_otherTeather(self):
        a = [88, 122, 97, 92, 129, 101, 133, 78, 109, 115, 136, 122, 92, 149, 92, 112, 145, 115, 118, 90, 130, 97, 147,
             89, 103, 112, 86, 130, 98, 114, 84, 98, 99, 85, 133, 111, 127, 139, 131, 133, 150, 86, 92, 103, 97, 85, 91,
             88, 147, 141, 104, 96, 84, 80, 101, 146, 142, 88, 105, 103, 148, 146, 113, 106, 109, 104, 83, 130, 131, 97,
             109, 139, 124, 98, 107, 99, 94, 127, 144, 140, 104, 132, 83, 127, 82, 103, 125, 86, 121, 120, 98, 140, 102,
             142, 120, 117, 126, 128, 118, 103, 130, 102, 116, 83, 120, 138, 95, 123, 147, 125, 139, 83, 137, 143, 144,
             117, 94, 90, 127, 143, 142, 98, 98, 132, 85, 80, 150, 121, 87, 148, 87, 150, 137, 114, 106, 81, 114, 138,
             101, 131, 138, 129, 100, 106, 114, 97, 83, 130, 89, 88, 98, 87, 102, 149, 89, 135, 116, 96, 130, 133, 135,
             146, 137, 129, 137, 107, 117, 82, 132, 94, 142, 99, 117, 103, 124, 112, 124, 115, 99, 143, 116, 91, 109,
             150, 93, 134, 150, 137, 111, 149, 140, 111, 129, 106, 143, 120, 92, 145, 106, 108, 114, 149, 112, 100, 101,
             141, 97, 89, 85, 118, 147, 82, 108, 102, 145, 93, 130, 110, 110, 101, 115, 137, 126, 105, 132, 123, 146,
             92, 113, 129, 112, 81, 104, 129, 109, 146, 124, 115, 116, 139, 144, 147, 132, 84, 110, 127, 119, 116, 150,
             122]

        plt.figure(figsize=(20,8),dpi=80)
        list1 = list(77 + 5 * i for i in range(16))
        plt.hist(a,list1)
        '''
        参数density设置y刻度显示频率
        '''
        xnum=range(min(a),max(a)+1,3)
        plt.xticks(list1)
        plt.grid()
        plt.show()

    # 进阶设置菜单
    def otherSetMenu(self):
        otherSetMenu=QMenu()
        # 设置坐标
        coordinatesAction=otherSetMenu.addAction('设置坐标')
        coordinatesAction.triggered.connect(self.setCoordinates)
        # 条形图的color参数可以传入颜色列表,这样相邻条形颜色可以不同
        BarColorListAction=otherSetMenu.addAction('条形图的颜色参数可以传入列表')

        otherSetMenu.exec()
    # 设置坐标
    def setCoordinates(self):
        plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
        x = [1, 2, 3, 4, 5, 6, 7, 8]
        y= [6, 2, 8, 9, 12, 17, 7, 6]
        list1 = [(x[i], y[i]) for i in range(len(x))]
        plt.plot(x, y)
        # 设置坐标文本

        print(list1)
        for i in range(len(x)):
            plt.text(x=x[i],y=y[i],s=list1[i])
        '''
        关于plt.text()参数的使用方法:见微信文件输入助手,有网络将该内容整理至此处
        该方法的使用需要好好研究
        
        '''
        plt.show()











app = QApplication(sys.argv)
w = MyWin()
w.show()
app.exec()

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值