python_校園熱力圖、三角函數(shù)曲線連接、二階貝塞斯曲線連接、matplotlib

import matplotlib.pyplot as plt
import random
import math
import numpy as np

# 定義‘地方’類
class Place():

    def __init__(self, name, ID, center_point_x, center_point_y, width, lengh, category, students_position_x, students_position_y):
        self.name = name
        self.ID = ID
        self.center_point_x = center_point_x
        self.center_point_y = center_point_y
        self.width = width
        self.lengh = lengh
        self.category = category
        self.students_position_x = students_position_x
        self.students_position_y = students_position_y
    
    # 在類中生成學(xué)生及其位置
    def generate_students(self):
        base_num = 5
        students_num = base_num * self.width * self.lengh / 1.0918
        students_num = int(students_num)
        # print(students_num)
        x_min = self.center_point_x - self.width/2
        x_max = self.center_point_x + self.width/2
        y_min = self.center_point_y - self.lengh/2
        y_max = self.center_point_y + self.lengh/2
        
        for i in range(0, students_num):
            # x = self.center_point_x - (self.width/2.0) + (self.width * random.randint(0, students_num)/students_num)
            # y = self.center_point_y - (self.lengh/2.0) + (self.lengh * random.randint(0, students_num)/students_num)
            i
            x = random.uniform(x_min, x_max)
            y = random.uniform(y_min, y_max)
            self.students_position_x.append(x)
            self.students_position_y.append(y)

# place1 = Place(1, 1, 29.49, 12.63, 1.68, 4.78, 'classroom', [], [])
# place1.generate_students()
# print(place1.students_position_x)

# 根據(jù)位置點(diǎn)生成與固定點(diǎn)的二階貝塞爾曲線,x1,y1為固定點(diǎn),x3,y3為采樣點(diǎn),控制點(diǎn)為其中位上距離為b的點(diǎn)
def two_degree_bc(x1, y1, x3, y3, dots_num=100): #bezier curve
    global xt, yt, x_dots12, x_dots23, y_dots12, y_dots23
    a = math.atan((y1-y3)/(x1-x3))
    b = 5
    x2 = (x1+x3)/2 - b*math.sin(a)
    y2 = (y1+y3)/2 - b*math.cos(a)
    xt = []
    yt = []
    x_dots12 = np.linspace(x1, x2, dots_num)
    y_dots12 = np.linspace(y1, y2, dots_num)
    x_dots23 = np.linspace(x2, x3, dots_num)
    y_dots23 = np.linspace(y2, y3, dots_num)
    for i in range(dots_num):
        x = x_dots12[i] + (x_dots23[i]-x_dots12[i])*i / (dots_num-1)
        y = y_dots12[i] + (y_dots23[i]-y_dots12[i])*i / (dots_num-1)
        xt.append(x)
        yt.append(y)

# 根據(jù)位置點(diǎn)生成三角函數(shù)曲線進(jìn)行連接
def cal_san(x1,y1,x2,y2):
    global x, y
    x =[]
    y = []
    for num in range(0,100):
        a = y1 + (y2-y1)*num/100
        pi = math.pi
        b = 0.5*(x1-x2)*math.sin(pi/(y2-y1)*(a-0.5*(y1+y2))-pi) + (x1+x2)/2
        y.append(a)
        x.append(b)

# 確定每個(gè)位置的坐標(biāo),大小,類別
place1 = Place(1, 1, 4.69, 12.39, 3.46, 2.43, 'office', [], [])
place2 = Place(2, 2, 10.86, 12.33, 4.94, 1.89, 'sports', [], [])
place3 = Place(3, 3, 14.59, 15.34, 2.82, 1.13, 'sports', [], [])
place4 = Place(4, 4, 16.55, 13.41, 4.0, 1.13, 'classroom', [], [])
place5 = Place(5, 5, 18.84, 11.46, 7.76, 0.6, 'classroom', [], [])
place6 = Place(6, 6, 20.76, 13.38, 1.98, 1.98, 'office', [], [])
place7 = Place(7, 7, 23.65, 14.76, 1.8, 3.1, 'office', [], [])
place8 = Place(8, 8, 22.67, 14.82, 3.4, 3.07, 'office', [], [])
place9 = Place(9, 9, 26.9, 11.28, 2.36, 1.48, 'office', [], [])
place10 = Place(10, 10, 25.94, 18.11, 5.72, 2.89, 'office', [], [])
place11 = Place(11, 11, 29.28, 20, 1.73, 1.62, 'office', [], [])
place12 = Place(12, 12, 31.48, 20, 1.31, 1.59, 'canteen', [], [])
place13 = Place(13, 13, 30.94, 22.1, 2.65, 2.05, 'dorm', [], [])
place14 = Place(14, 14, 30.33, 17.29, 3.7, 3.7, 'office', [], [])
place15 = Place(15, 15, 29.58, 12.36, 1.62, 4.87, 'classroom', [], [])
place16 = Place(16, 16, 30.3, 6.95, 1.59, 1.52, 'office', [], [])
place17 = Place(17, 17, 28.53, 5.14, 1.13, 1.83, 'office', [], [])
place18 = Place(18, 18, 33.46, 4.14, 1.41, 2.29, 'office', [], [])
place19 = Place(19, 19, 35.93, 8.87, 1.94, 1.76, 'library', [], [])
place20 = Place(20, 20, 36.23, 12.9, 4.83, 4.23, 'sports', [], [])
place21 = Place(21, 21, 32.14, 13.98, 0.85, 1.59, 'office', [], [])
place22 = Place(22, 22, 35.45, 25.14, 4.48, 1.59, 'office', [], [])
place23 = Place(23, 23, 39.78, 12.81, 1.73, 4.16, 'office', [], [])
place24 = Place(24, 24, 37.77, 8.45, 0.95, 3.7, 'office', [], [])
place25 = Place(25, 25, 39.06, 3.88, 1.02, 1.94, 'office', [], [])
place26 = Place(26, 26, 39.27, 7.52, 0.67, 2.22, 'classroom', [], [])
place27 = Place(27, 27, 40.35, 9.26, 1.13, 1.062, 'canteen', [], [])
place28 = Place(28, 28, 42.22, 10.31, 1.2, 2.05, 'dorm', [], [])

place_list = [place1, place2, place3, place4, place5, place6, place7, place8, place9, place10, place11, place12, place13, place14, place15, place16, place17, place18, place19, place20, place21, place22, place23, place24, place25, place26, place27, place28]



img = plt.imread("bg.png")
fig,ax = plt.subplots()
ax.imshow(img,extent=[0, 47, 0, 28])


# 根據(jù)每個(gè)地點(diǎn),生成可視化,先制作散點(diǎn)圖,然后連接貝塞爾曲線
student_x = []
student_y = []
for place in place_list:
    place.generate_students()
    # for student_X in place.students_position_x :
    #     student_x.append(student_X)
    # for student_Y in place.students_position_y :
    #     student_y.append(student_Y)
    
    # 形狀大小設(shè)置
    # s = []
    # for i in range(0, len(place.students_position_x)):
    #     s.append(random.randint(1,100))
    s = 50
    if place.category == 'classroom':
        plt.scatter(place.students_position_x, place.students_position_y, c= '#8519b3',  s= s, edgecolors='none', alpha= 0.5,marker= 'h' )
        for i in range(0, len(place.students_position_x)):
            # two_degree_bc(0, -60 , place.students_position_x[i], place.students_position_y[i])
            # plt.plot(xt,yt,color='blue', alpha = 0.1)
            cal_san(0,0,place.students_position_x[i], place.students_position_y[i])
            plt.plot(x, y, color='#583590', alpha = 0.1)

    if place.category == 'sports':
        plt.scatter(place.students_position_x, place.students_position_y, c= '#8519b3',  s= s, edgecolors='face', alpha= 0.5 ,marker='h')
        for i in range(0, len(place.students_position_x)):
            # two_degree_bc(10, -60 , place.students_position_x[i], place.students_position_y[i])
            # plt.plot(xt,yt,color='purple', alpha = 0.1)
            cal_san(10,0,place.students_position_x[i], place.students_position_y[i])
            plt.plot(x, y, color='#583590', alpha = 0.1)

    if place.category == 'office':
        plt.scatter(place.students_position_x, place.students_position_y, c= '#8519b3',  s= s, edgecolors='face', alpha= 0.5 ,marker='h')
        for i in range(0, len(place.students_position_x)):
            # two_degree_bc(20, -60 , place.students_position_x[i], place.students_position_y[i])
            # plt.plot(xt,yt,color='pink', alpha = 0.1)
            cal_san(20,0,place.students_position_x[i], place.students_position_y[i])
            plt.plot(x, y, color='#583590', alpha = 0.1)

    if place.category == 'canteen':
        plt.scatter(place.students_position_x, place.students_position_y, c= '#8519b3',  s= s, edgecolors='face', alpha= 0.5 ,marker = 'h')
        for i in range(0, len(place.students_position_x)):
            # two_degree_bc(30, -60 , place.students_position_x[i], place.students_position_y[i])
            # plt.plot(xt,yt,color='green', alpha = 0.1)
            cal_san(30,0,place.students_position_x[i], place.students_position_y[i])
            plt.plot(x, y, color='#583590', alpha = 0.1)

    if place.category == 'dorm':
        plt.scatter(place.students_position_x, place.students_position_y, c= '#8519b3',  s= s, edgecolors='face', alpha= 0.5 , marker ='h')
        for i in range(0, len(place.students_position_x)):
            # two_degree_bc(40, -60 , place.students_position_x[i], place.students_position_y[i])
            # plt.plot(xt,yt,color='red', alpha = 0.1)
            cal_san(40,0,place.students_position_x[i], place.students_position_y[i])
            plt.plot(x, y, color='#583590', alpha = 0.1)



# plt.scatter(student_x, student_y, cmap=plt.cm.get_cmap('Blues'),  s= s, edgecolors=None, alpha= 0.5)
    
plt.axis('off')

plt.show()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容