如何使用 python 获取多个 csv 文件的折线图?
how to get the line graph for the multiple csv files using python?
enter image description here假设我有多个 test_suites UPT_Synergy_graph22.csv、UPT_C7000_graph22.csv、SAT-Synergy-gen2_graph22.csv 的 csv 文件,像这样我有 10 个更多 csv 文件,这些文件在所有文件 -build_id 中具有相同的列并通过百分比。我需要为所有这些文件绘制折线图。其中 build id 是 x 轴,pass-percent 是 y 轴。我需要为每个 csv 文件获取折线图(每个测试套件的平均值)。
我只能获取一个 csv 文件的图表,无法获取所有文件的结果。
请帮我解决这个问题。下面是我用过的代码。或者建议我使用任何其他适合的模块。
import pandas as pd
import pygal
from pygal.style import Style
data_frame=pd.read_csv('C:\Users\shivarad\Documents\Scale_graph22.csv', dtype={"Pass
Percentage":int,"original_pass_percent":int})
a = []
b = []
line_chart=pygal.Line()
#line_chart.title='Graphical Representation of the pass percentage for a build ID---TEST SUITE: SCALE'
line_chart.x_labels=data_frame['build ID']
for index,row in data_frame.iterrows():
a.append(row["Pass Percentage"])
b.append(row["original_pass_percent"])
line_chart.add("Pass Percentage",a)
line_chart.add("original_pass_percent",b)
line_chart.render_in_browser()
#bar_chart.title='Graphical Representation of the pass percentage for a build ID---TEST SUITE: SCALE'
bar_chart = pygal.stackedBar(height=360, width=440,explicit_size=True)
bar_chart.title='Graphical Representation of the pass percentage for a build ID---TEST SUITE: SCALE'
bar_chart.x_labels=data_frame['build ID']
for index,row in data_frame.iterrows():
a.append(row["Pass Percentage"])
b.append(row["original_pass_percent"])
# adding the apeended list
bar_chart.add('Pass Percentage', a)
bar_chart.add('original_pass_percent', b)
# rendering the file
bar_chart.render_in_browser()}
- 回答:我需要为所有这些文件绘制折线图。其中
build id
是 x 轴,pass-percent
是 y 轴。
- 此代码将执行以下操作:
- 创建相关文件列表
- 遍历每个文件
- 创建数据框
- 使用标签绘制数据框中的两个指定列
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
%matplotlib inline
# graphing parameters
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = (16.0, 10.0)
p = Path(r'c:\Users\shivarad\Documents') # path to files
files = list(p.rglob('*graph22.csv')) # get files
# everything for here down, belongs in one Jupyter cell
plt.figure()
for f in files: # iterate through files
file_name = f.stem # get filename
df = pd.read_csv(f, dtype={'Pass Percentage': int, 'original_pass_percent': int}) # create dataframe
print(df.head()) # this is here to verify df has data; it can be commented out or removed
plt.plot('build ID', 'Pass Percentage', data=df, label=file_name) # plot the data from each file
plt.legend(bbox_to_anchor=(1.04, 0.5), loc='center left')
plt.savefig('test.jpg') # verify there's plot in the file
plt.show() # outside the loop
我只能获取一个 csv 文件的图表,无法获取所有文件的结果。
请帮我解决这个问题。下面是我用过的代码。或者建议我使用任何其他适合的模块。
import pandas as pd
import pygal
from pygal.style import Style
data_frame=pd.read_csv('C:\Users\shivarad\Documents\Scale_graph22.csv', dtype={"Pass
Percentage":int,"original_pass_percent":int})
a = []
b = []
line_chart=pygal.Line()
#line_chart.title='Graphical Representation of the pass percentage for a build ID---TEST SUITE: SCALE'
line_chart.x_labels=data_frame['build ID']
for index,row in data_frame.iterrows():
a.append(row["Pass Percentage"])
b.append(row["original_pass_percent"])
line_chart.add("Pass Percentage",a)
line_chart.add("original_pass_percent",b)
line_chart.render_in_browser()
#bar_chart.title='Graphical Representation of the pass percentage for a build ID---TEST SUITE: SCALE'
bar_chart = pygal.stackedBar(height=360, width=440,explicit_size=True)
bar_chart.title='Graphical Representation of the pass percentage for a build ID---TEST SUITE: SCALE'
bar_chart.x_labels=data_frame['build ID']
for index,row in data_frame.iterrows():
a.append(row["Pass Percentage"])
b.append(row["original_pass_percent"])
# adding the apeended list
bar_chart.add('Pass Percentage', a)
bar_chart.add('original_pass_percent', b)
# rendering the file
bar_chart.render_in_browser()}
- 回答:我需要为所有这些文件绘制折线图。其中
build id
是 x 轴,pass-percent
是 y 轴。 - 此代码将执行以下操作:
- 创建相关文件列表
- 遍历每个文件
- 创建数据框
- 使用标签绘制数据框中的两个指定列
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
%matplotlib inline
# graphing parameters
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = (16.0, 10.0)
p = Path(r'c:\Users\shivarad\Documents') # path to files
files = list(p.rglob('*graph22.csv')) # get files
# everything for here down, belongs in one Jupyter cell
plt.figure()
for f in files: # iterate through files
file_name = f.stem # get filename
df = pd.read_csv(f, dtype={'Pass Percentage': int, 'original_pass_percent': int}) # create dataframe
print(df.head()) # this is here to verify df has data; it can be commented out or removed
plt.plot('build ID', 'Pass Percentage', data=df, label=file_name) # plot the data from each file
plt.legend(bbox_to_anchor=(1.04, 0.5), loc='center left')
plt.savefig('test.jpg') # verify there's plot in the file
plt.show() # outside the loop