-1

I'm trying to make a code for a pandemic simulation. I have a csv file with data from 3 groups: S (Susceptible) – I (Infectious) – R (Recovered) – . I want to plot all three lists (s, i, r) in the same 2d plot. So that we can see three graphs in the same plot. Where the x axis is days and the y axis is in percentage (eg from 0 to 1). I have problem with making these plots, Here is my code:

import numpy as np
import matplotlib.pyplot as plt
s = []
i = []
r = []
with open("pan.csv","r") as f:
    lis = [line.split(",") for line in f]    
    for n in range(1,121):
        s.append(int(lis[n][0]))
        i.append(float(lis[n][1]))
        r.append(float(lis[n][2]))
      
n_list= [s, i, r]
fig = plt.figure()
ax = fig.add_subplot()

w = [s, i, r]
w_np = np.array(w)

# ax.plot(s, range(1)) here I have the problem

plt.show()

anyone can help me for plotting?

Trying to plot three 2d plots (x from three lists and y between 0-1 in percentage) in same plot.

2
  • Please try and search before you post a question, here is an answer Commented Nov 13, 2023 at 15:58
  • import pandas as pd, df = pd.read_csv('pan.csv'), and ax = df.plot(figsize=(6, 6)) Commented Nov 13, 2023 at 16:54

2 Answers 2

0

If you're already using NumPy, you can do this

import numpy as np                                                                                                    
import matplotlib.pyplot as plt                                                                                       
                                                                                                                      
# optional, see below
from matplotlib.ticker import MaxNLocator                                                                             

                                                                                                                      
s, i, r = np.genfromtxt("pan.csv", delimiter=',', unpack=True)                                                        
                                                                                                                      
days = np.arange(1, len(s)+1, dtype=int)                                                                              
                                                                                                                      
fig = plt.figure()                                                                                                    
ax = fig.add_subplot()                                                                                                
                                                                                                                      
ax.plot(days, s)                                                                                                      
ax.plot(days, i)                                                                                                      
ax.plot(days, r)                                                                                                      
                                                                                                                      
# This is optional: it will enforce axis ticks to be integers
ax.xaxis.set_major_locator(MaxNLocator(integer=True))                                                                 
                                                                                                                      
plt.show()
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you JustLearning and Tech Support Pakistan for answers, much appreciated. I tried to plot the graphs, but the graphs don't show the right value.
Your response is unhelpful. Show the error and/or what went wrong.
I can't plot the graph, I get an error that is from line 362-line 601: ValueError: Some errors were detected ! Line #362 (got 1 columns instead of 4) Line #363 (got 1 columns instead of 4) Line #364 (got 1 columns instead of 4) Line #365 (got 1 columns instead of 4)
0

Normalize the data to percentages and use the plot function for each group.

import numpy as np
import matplotlib.pyplot as plt

s = []
i = []
r = []
with open("pan.csv", "r") as f:
lis = [line.split(",") for line in f]
for n in range(1, 121):
    s.append(int(lis[n][0]))
    i.append(float(lis[n][1]))
    r.append(float(lis[n][2]))

s = np.array(s)
i = np.array(i)
r = np.array(r)

s_percentage = s / (s + i + r)
i_percentage = i / (s + i + r)
r_percentage = r / (s + i + r)

fig, ax = plt.subplots()
ax.plot(s_percentage, label='Susceptible')
ax.plot(i_percentage, label='Infectious')
ax.plot(r_percentage, label='Recovered')

ax.set_xlabel('Days')
ax.set_ylabel('Percentage')
ax.set_title('Pandemic Simulation')
ax.legend()

plt.show()

1 Comment

Thank you JustLearning and Tech Support Pakistan for answers, much appreciated. I tried to plot the graphs, but the graphs don't show the right value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.