Python消费人格雷达图

本人刚学没多久,刚学wxpython就做了,分享出来方便初学者也希望大佬提出建议有借鉴学习,原址找不到了,没有列出

**

Python消费人格雷达图

**

一、分析:
用wxpython创建窗口--------》使用按钮来计数选择题答题情况--------》用matplotlib绘制雷达图-------》pyinstaller打包

二、使用的库:
wxpython、numpy、matplotlib、pyinstaller

三、运行效果:
Python消费人格雷达图_第1张图片

四、代码:
题目放在一个叫timu.txt的文件中,一行一道题(每道题的换行加\n)

import numpy as np
import matplotlib.pyplot as plt
import os
import wx

def CK():
    global read1
    global jishu
    global ab
    global ac
    global ad
    global bc
    global bd
    global cd
    with open('timu.txt', 'r')as f:
        read = f.readlines()
        read1 = []
        for i in read:
            read1.append(i.replace(' ', '\n'))
    jishu = 0
    ab, ac, ad, bc, bd, cd = 0, 0, 0, 0, 0, 0
    class MyFrame(wx.Frame):#创建wx.wx.frame的类
        def __init__(self):
            super().__init__(parent=None, title='消费人格分析答卷', size=(400, 300))
            self.Center()  # 设置屏幕居中
            panel = wx.Panel(parent=self)
            hbox = wx.BoxSizer(wx.HORIZONTAL)

            vbox1 = wx.BoxSizer(wx.VERTICAL)
            bt1 = wx.Button(panel, id=1, label='AB')
            bt2 = wx.Button(panel, id=2, label='AC')
            bt3 = wx.Button(panel, id=3, label='AD')
            vbox1.Add(bt1, 1, wx.EXPAND | wx.BOTTOM, 5)
            vbox1.Add(bt2, 1, wx.EXPAND | wx.BOTTOM, 5)
            vbox1.Add(bt3, 1, wx.EXPAND | wx.BOTTOM, 5)

            vbox2 = wx.BoxSizer(wx.VERTICAL)
            bt4 = wx.Button(panel, id=4, label='BC')
            bt5 = wx.Button(panel, id=5, label='BD')
            bt6 = wx.Button(panel, id=6, label='CD')
            vbox2.Add(bt4, 1, wx.EXPAND | wx.BOTTOM, 5)
            vbox2.Add(bt5, 1, wx.EXPAND | wx.BOTTOM, 5)
            vbox2.Add(bt6, 1, wx.EXPAND | wx.BOTTOM, 5)

            vbox3 = wx.BoxSizer(wx.VERTICAL)
            self.statictext = wx.StaticText(parent=panel,
                                            label='点击任意按钮开始测验\n测验结束后关闭程序生成分析图\n本次测验共十道选择题',
                                            style=wx.EXPAND)
            vbox3.Add(self.statictext, flag=wx.EXPAND)

            hbox.Add(vbox1, 1, flag=wx.ALIGN_LEFT | wx.EXPAND)
            hbox.Add(vbox3, 4, flag=wx.ALIGN_CENTER | wx.EXPAND)
            hbox.Add(vbox2, 1, flag=wx.ALIGN_RIGHT | wx.EXPAND)

            self.Bind(wx.EVT_BUTTON, self.on_click, id=1, id2=6)
            panel.SetSizer(hbox)

        def on_click(self, event):
            global jishu
            global ab
            global ac
            global ad
            global bc
            global bd
            global cd
            event_id = event.GetId()
            try:
                self.statictext.SetLabelText(str(read1[int(jishu)]))
            except:
                x = [ab, ac, ad, bc, bd, cd]
                HuiTu(x)
            if jishu < 11:
                jishu += 1
            if event_id == 1:
                ab += 1
            elif event_id == 2:
                ac += 1
            elif event_id == 3:
                ad += 1
            elif event_id == 4:
                bc += 1
            elif event_id == 5:
                bd += 1
            elif event_id == 6:
                cd += 1

    class App(wx.App):#创建wx.app类创建
        def OnInit(self):#创建 wx.App 的子类并实现 OnInit 函数 -> 创建子类的实例。OnInit 方法中,一般创建至少一个 Frame,并设置为 show。
            frame = MyFrame()
            frame.Show()
            return True#app返回的是个布朗值,返回错误程序停止运行

        def OnExit(self):
            print('Tui chu')
            return 0

    if __name__ == '__main__':#“__main__”等于当前执行文件的名称(包含了后缀.py)
        app = App()
        app.MainLoop()#启动app的mainloop()

def HuiTu(x):
    # ====================绘图=========================
    # 标签
    labels = np.array(['外向型消费者', '内向型消费者', '理智型消费者', '情绪型消费者', '独立型消费者', '顺从型消费者'])
    # 数据个数
    dataLenth = 6
    # 数据
    data = np.array(x)
    angles = np.linspace(0, 2 * np.pi, dataLenth, endpoint=False)
    data = np.concatenate((data, [data[0]])) # 闭合 将数据结合起来
    angles = np.concatenate((angles, [angles[0]]))  # 闭合 将数据结合起来
    fig = plt.figure()
    ax = fig.add_subplot(111, polar=True)  # polar参数!!
    ax.plot(angles, data, 'o--', linewidth=2)  # 画线
    ax.fill(angles, data, facecolor='b', alpha=0.25)  # 填充
    ax.set_thetagrids(angles * 180 / np.pi, labels, fontproperties="SimHei")
    ax.set_title("消费人格分析图",fontproperties="SimHei")
    ax.set_rlim(0, 5)
    ax.grid(True)
    plt.show()
def main():
    global read1
    global jishu
    global ab
    global ac
    global ad
    global bc
    global bd
    global cd
    with open('timu.txt', 'r')as f:
        read = f.readlines()
        read1 = []
        for i in read:
            read1.append(i.replace(' ', '\n'))
    jishu = 0
    x=[]
    ab, ac, ad, bc, bd, cd = 0, 0, 0, 0, 0, 0
    CK()
main()

你可能感兴趣的:(python)