【问题标题】:how to get the multiple csv files on to the graph using Plotly library in python or any module that can get the results on browser?如何使用 python 中的 Plotly 库或任何可以在浏览器上获取结果的模块将多个 csv 文件放到图形上?
【发布时间】:2020-10-30 12:14:05
【问题描述】:

假设我有多个 csv 文件用于 test_suites UPT_Synergy_graph22.csvUPT_C7000_graph22.csvSAT-Synergy-gen2_graph22.csv像这样我还有 10 个 csv 文件,它们在所有文件 build_id 和通过百分比中具有相同的列。我需要为所有这些文件绘制折线图。其中 build id 是 x 轴,pass-percent 是 y 轴。我需要为每个 csv 文件获取折线图(每个测试套件的平均值)。每个csv文件对应一个test_suite。

我只能获得一个 csv 文件的图表。我可以使用 matplotlib 获取图形,但我想要网页/浏览器上的图形,我知道我们可以使用 Plotly 模块/bookeh 或任何能够在网络上获得相同图形结果的模块会有所帮助。

请帮我解决这个问题。下面是我使用的代码。帮助我使用 Plotly 模块转换代码以获取上述 csv 文件的折线图。

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

【问题讨论】:

  • @Andrea Blengino,你能帮我用 Plotly python 库获取折线图吗

标签: python csv graph plotly data-visualization


【解决方案1】:

我建议您将之前的文件连接或合并。因为你在files 变量中有你的文件列表,并且列都是一样的,可能

dt = {'Pass Percentage': int, 'original_pass_percent': int}
df = pd.concat([pd.read_csv(f).assign(filename=f, dtype=dt) for f in files])

那么你就可以使用plotly了:

import plotly.express as px
px.line(df, x='build ID', y='Pass Percentage', color='filename')

假设“构建 ID”和“通过百分比”是列名。

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2019-07-22
    相关资源
    最近更新 更多