matplotlib 在同一个图中从一个CSV文件绘制多个图,而不使用pandas [重复]

yv5phkfx  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(107)

此问题在此处已有答案

How to plot two list in the same graph, with different colors(4个答案)
Import CSV file as a Pandas DataFrame(4个答案)
How to plot multiple pandas columns(3个答案)
8天前关闭。
我正在尝试做一个流行病模拟的代码。我有一个CSV文件,里面有三个组的数据:S(敏感)- I(传染性)- R(已关闭)- .我想绘制所有三个列表(s,i,r)在同一个二维图中。因此,我们可以在同一个图中看到三个图形。其中x轴是天数,y轴是百分比(例如从0到1).我有问题,使这些情节,这里是我的代码:

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()

字符串
有人能帮我策划吗
尝试在同一个图中绘制三个二维图(x来自三个列表,y在0-1之间,百分比)。

u5rb5r59

u5rb5r591#

如果你已经在使用NumPy,你可以这样做。

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()

字符串

gajydyqb

gajydyqb2#

将数据归一化为百分比,并对每组使用绘图函数。

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()

字符串

相关问题