我没有在Spyder 3.8中打印我的情节,我看不到关于这些地块为什么不会在地图窗格中打印的任何问题。我有未置的直列绘图

发布于 2025-01-22 00:44:42 字数 3013 浏览 1 评论 0原文

from random import random, randint
from matplotlib import pyplot as plt


class human:
    def __init__(self, status):
        self.status = status
        self.time_infected = 0   #time ill at start is zero
        self.building = list()
        
class place:
    def __init__(self, place_id):
        self.place_id = place_id
        self.num_infected = 0 #number ill at start
        
def sim(n_places, n_uninfected, n_infected, iterations):
    city = [place(i) for i in range(n_places)]
    people = [human('healthy') for i in range(n_uninfected)] + [human('infected') for i in range(n_infected)]
    
    uninfected_history = [n_uninfected]
    infected_history = [n_infected]
    recovered_history = [0]
    deceased_history = [0]
    days = [day for day in range(iterations)]
    
    for day in days:
        uninfected = 0
        infected = 0 
        recovered = 0 
        deceased = 0
        
        for human in people:
            human.place = city[randint(0, len(city)-1)]  #sending each person to one building during the day
            
        for human in people:
            if human.status == 'uninfected':
                uninfected += 1
            elif human.status == 'infected':
                infected += 1
                human.place.num_ill += 1
            elif human.status == 'recovered':
                recovered += 1
            else:
                deceased += 1
                
                
            for human in people:
                if human.status == 'infected' and human.time_infected < 15:
                    human.time_infected += 1
                elif human.status == 'infected' and human.time_infected == 15:
                    if randint(0, 9) == 4:
                        human.status = 'deceased'
                    else:
                        human.status = 'recovered'
                if human.status == 'uninfected':
                    infection_chance = 0.001*human.place.num_infected
                    if random() < infection_chance:
                        human.status = 'infected'
                        
            for place in city:
                place.num_infected = 0
                        
            uninfected_history.append(uninfected)
            infected_history.append(infected)
            recovered_history.append(recovered)
            deceased_history.append(deceased)
                        
            
        fig, axs = plt.subplots(4, sharex=True, sharey=False)
        fig.suptitle('SIMPLE COVID19 SPREAD')
        axs[0].plot(days, uninfected_history[:-1])
        axs[0].set_title('Uninfected')
        axs[1].plot(days, infected_history[:-1], 'tab:yellow')
        axs[1].set_title('Infected')
        axs[2].plot(days, recovered_history[:-1], 'tab:green')
        axs[2].set_title('Recovered')
        axs[3].plot(days, deceased_history[:-1], 'tab:red')
        axs[3].set_title('Deceased')
        
        plt.show()
        
        
    sim(150, 49950, 50, 365)
from random import random, randint
from matplotlib import pyplot as plt


class human:
    def __init__(self, status):
        self.status = status
        self.time_infected = 0   #time ill at start is zero
        self.building = list()
        
class place:
    def __init__(self, place_id):
        self.place_id = place_id
        self.num_infected = 0 #number ill at start
        
def sim(n_places, n_uninfected, n_infected, iterations):
    city = [place(i) for i in range(n_places)]
    people = [human('healthy') for i in range(n_uninfected)] + [human('infected') for i in range(n_infected)]
    
    uninfected_history = [n_uninfected]
    infected_history = [n_infected]
    recovered_history = [0]
    deceased_history = [0]
    days = [day for day in range(iterations)]
    
    for day in days:
        uninfected = 0
        infected = 0 
        recovered = 0 
        deceased = 0
        
        for human in people:
            human.place = city[randint(0, len(city)-1)]  #sending each person to one building during the day
            
        for human in people:
            if human.status == 'uninfected':
                uninfected += 1
            elif human.status == 'infected':
                infected += 1
                human.place.num_ill += 1
            elif human.status == 'recovered':
                recovered += 1
            else:
                deceased += 1
                
                
            for human in people:
                if human.status == 'infected' and human.time_infected < 15:
                    human.time_infected += 1
                elif human.status == 'infected' and human.time_infected == 15:
                    if randint(0, 9) == 4:
                        human.status = 'deceased'
                    else:
                        human.status = 'recovered'
                if human.status == 'uninfected':
                    infection_chance = 0.001*human.place.num_infected
                    if random() < infection_chance:
                        human.status = 'infected'
                        
            for place in city:
                place.num_infected = 0
                        
            uninfected_history.append(uninfected)
            infected_history.append(infected)
            recovered_history.append(recovered)
            deceased_history.append(deceased)
                        
            
        fig, axs = plt.subplots(4, sharex=True, sharey=False)
        fig.suptitle('SIMPLE COVID19 SPREAD')
        axs[0].plot(days, uninfected_history[:-1])
        axs[0].set_title('Uninfected')
        axs[1].plot(days, infected_history[:-1], 'tab:yellow')
        axs[1].set_title('Infected')
        axs[2].plot(days, recovered_history[:-1], 'tab:green')
        axs[2].set_title('Recovered')
        axs[3].plot(days, deceased_history[:-1], 'tab:red')
        axs[3].set_title('Deceased')
        
        plt.show()
        
        
    sim(150, 49950, 50, 365)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文