【问题标题】:how to get the multiple csv files on a line graph using Plotly python library to get the results on browser?如何使用 Plotly python 库在折线图上获取多个 csv 文件以在浏览器上获取结果?
【发布时间】:2020-10-30 15:28:54
【问题描述】:

我有多个用于 test_suites Synergy_graph22.csv、C7000_graph22.csv、Synergy-gen2_graph22.csv 的 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

【问题讨论】:

    标签: python csv graph plotly data-visualization


    【解决方案1】:
    fig = go.Figure()
    for f in files: 
        file_name = f.stem  
        df = pd.read_csv(f, dtype={'Pass Percentage': int, 'original_pass_percent': 
             int})  
        print(df.head())  
        
        fig.add_trace(go.Scatter(x=df['build ID'], y=['Pass Percentage'],
                    mode='lines',
                    name=file_name))
    
    fig.show()
    

    【讨论】:

    • 嗨@nav610,感谢您的回复,但我看不到折线图,它只显示没有线条的空白图..请帮我解决这个问题
    • 我在上面的图表中添加了图表的快照
    • 看起来我打错了,说 y = ['Pass Perccentage'],有两个 C。编辑了代码。希望这能解决问题。
    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2017-06-22
    • 2021-03-07
    • 1970-01-01
    相关资源
    最近更新 更多