用乌龟或吐温特制作蜘蛛图 [英] Building a spider-chart with turtle or tkinter

查看:51
本文介绍了用乌龟或吐温特制作蜘蛛图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我为我的Minor大学开设了2门有关python编码的入门课程,然后介绍了数据库和有关PHP的CRUD开发的基础课程.python课程相当不错,并涉及了大多数基础知识.我一直在尝试通过编写小型应用程序来完成剩下的语言学课程,以此来教自己.

I recently did 2 beginner courses in python coding at university for my Minor, which were followed by an intro to databases and a basic course about CRUD development in PHP. The python course was decent enough and touched on most of the basics. I've been trying to teach myself by making small applications to do things that I have to do for my remaining courses in linguistics.

现在,我想编写一个小型python应用程序,它需要用户输入才能为该扬声器创建语言配置文件.我以为可以使用python的turtle模块来做到这一点,但是绘制网格后,我发现我不知道如何获取绘制的圆点.

Now I wanted to write a small python application and that takes user input to create a Language profile for that speaker. I thought I could do this using the turtle module of python, but after drawing the grid I found out I do not know how to get the points of the circles that i have drawn.

我编写了以下代码:

'''
 for i in range(6):
        t.circle(20*i,None, 16)
        t.seth(90)
        t.penup()[enter image description here][1]
        t.backward(20)
        t.pendown()
        t.seth(0)
'''

我使用了16个步骤.因为它最终需要看起来像这样的图片:

I used 16 steps. because it needs to look something like this picture in the end :

这里有人可以帮助我了解如何在圆上绘制这些点,以便在用户告诉程序该点他的熟练程度后可以将其画出吗?

Could anyone here help me along on how I can get these points on the circle mapped so that they can be drawn in after the user tells the program what his proficiency is for that point?

请让我知道是否遗漏了任何重要内容.

Please let me know if I left out anything important.

此致

推荐答案

您可以使用 tkinter 构造雷达图或蜘蛛图.

You can construct a radar, or spider chart with tkinter.

以下类获取 tuples('label',score)的列表,分数以百分比表示(区间 [0,100] 中的值).

The following class takes a list of tuples('label', score), with the score expressed as a percentage (value in the interval [0, 100]).

数据点数会根据提供的数据进行调整.
图表的比例可以调整.

The number of data points adjusts to the data provided.
The scale of the chart can be adjusted.

import math
import tkinter as tk


class SpiderChart(tk.Canvas):
    """a canvas that displays datapoints as a SpiderChart
    """
    width=500
    height=500
    def __init__(self, master, datapoints, concentrics=10, scale=200):
        super().__init__(master, width=self.width, height=self.height)
        self.scale = scale
        self.center = self.width // 2, self.height // 2
        self.labels = tuple(d[0] for d in datapoints)
        self.values = tuple(d[1] for d in datapoints)
        self.num_pts = len(self.labels)
        self.concentrics = [n/(concentrics) for n in range(1, concentrics + 1)]
        self.draw()
        
    def position(self, x, y):
        """use +Y pointing up, and origin at center
        """
        cx, cy = self.center
        return x + cx, cy - y
    
    def draw_circle_from_radius_center(self, radius):
        rad = radius * self.scale
        x0, y0 =  self.position(-rad, rad)
        x1, y1 =  self.position(rad, -rad)
        return self.create_oval(x0, y0, x1, y1, dash=(1, 3))
    
    def draw_label(self, idx, label):
        angle = idx * (2 * math.pi) / self.num_pts
        d = self.concentrics[-1] * self.scale
        x, y = d * math.cos(angle), d * math.sin(angle)
        self.create_line(*self.center, *self.position(x, y), dash=(1, 3))
        d *= 1.1 
        x, y = d * math.cos(angle), d * math.sin(angle)
        self.create_text(*self.position(x, y), text=label)
        
    def draw_polygon(self):
        points = []
        for idx, val in enumerate(self.values):
            d = (val / 100) * self.scale
            angle = idx * (2 * math.pi) / self.num_pts
            x, y = d * math.cos(angle), d * math.sin(angle)
            points.append(self.position(x, y))
        self.create_polygon(points, fill='cyan')
        
    def draw(self):
        self.draw_polygon()
        for concentric in self.concentrics:
            self.draw_circle_from_radius_center(concentric)
        for idx, label in enumerate(self.labels):
            self.draw_label(idx, label)
        
            
data = [('stamina', 70), ('python-skill', 100), ('strength', 80), ('break-dance', 66), ('speed', 45), ('health', 72), ('healing', 90), ('energy', 12), ('libido', 100)]

root = tk.Tk()
spider = SpiderChart(root, data)
spider.pack(expand=True, fill=tk.BOTH)

root.mainloop()
        

这篇关于用乌龟或吐温特制作蜘蛛图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆