在不使用 pandas 的情况下,在同一图中绘制一个 csv 文件中的多个图形 [duplicate]

Plot multiple graph from one csv file in same plot without using pandas [duplicate]

提问人:Hebe 提问时间:11/13/2023 最后编辑:Hebe 更新时间:11/14/2023 访问量:71

问:

我正在尝试为大流行模拟制作代码。我有一个 csv 文件,其中包含来自 3 组的数据: S(易感)– 我(传染性)—— R(已恢复)– . 我想在同一个 2d 图中绘制所有三个列表(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()

有人可以帮我策划吗?

尝试在同一图中绘制三个 2d 图(来自三个列表的 x,百分比介于 0-1 之间的 y)。

python matplotlib 嵌套列表

评论

0赞 GregoirePelegrin 11/13/2023
请在发布问题之前尝试搜索,这是一个答案
0赞 Trenton McKinney 11/14/2023
import pandas as pddf = pd.read_csv('pan.csv')ax = df.plot(figsize=(6, 6))

答:

0赞 JustLearning 11/14/2023 #1

如果您已经在使用 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()

评论

0赞 Hebe 11/14/2023
感谢 JustLearning 和巴基斯坦技术支持的回答,非常感谢。我试图绘制图形,但图形没有显示正确的值。
0赞 JustLearning 11/14/2023
你的回答是无济于事的。显示错误和/或出了什么问题。
0赞 Hebe 11/14/2023
我无法绘制图形,我收到来自第 362 行第 601 行的错误:ValueError:检测到一些错误!行 #362(得到 1 列而不是 4 列) 行 #363(得到 1 列而不是 4 列) 行 #364(得到 1 列而不是 4 列) 行 #365(得到 1 列而不是 4 列)
0赞 Tech Support Pakistan 11/14/2023 #2

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

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

评论

0赞 Hebe 11/14/2023
感谢 JustLearning 和巴基斯坦技术支持的回答,非常感谢。我试图绘制图形,但图形没有显示正确的值。