簡體   English   中英

如何對給定的文本文件使用以下代碼 plot bar plot?

[英]How to plot bar plot using the following code for the given text file?

我的一個 HPC 應用程序有以下 output 文件 (output.txt),格式如下

Data(D) Number_of_Processors(P) Process_per_node(ppn) mode time

2048 4 1 0 0.001220
2048 4 1 1 0.000858
32768 4 1 0 0.008137
32768 4 1 1 0.032052
262144 4 1 0 0.078899
262144 4 1 1 0.103439
2048 4 8 0 0.118370
2048 4 8 1 0.064003
32768 4 8 0 0.197745
32768 4 8 1 0.116132
262144 4 8 0 0.502012
262144 4 8 1 0.717104

我在這里只提供了 12 行,但它們的數量是 240,你可以看到只有 3 種類型的 DATA 大小。 我對 C++ 很了解,但我不知道 python。 對於我的員工,我必須制作一個酒吧 plot 他們,像這樣-->

在此處輸入圖像描述

Y軸是時間,X軸是進程數,即P*ppn,3個數據大小分別有3個圖,一個是2048,一個是32768,最后一個是262144 .

我已經獲得了一個需要修改的示例代碼和 plot 一個看起來像上面的圖表。

這是圖形的示例代碼-->

#!/usr/bin/env python
# coding: utf-8

# In[47]:


import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

sns.set()


# In[48]:


demo_input_format = pd.DataFrame.from_dict({
    "D": [],
    "P": [],
    "ppn": [],
    "mode": [],  # 1 --> optimized, 0 --> standard
    "time": [],
})


# In[49]:


for execution in range(10):
    for P in [4, 16]:
        for ppn in [1, 8]:
            for D in [16, 256, 2048]:
                # Change with the actual data
                demo_input_format = demo_input_format.append({
                    "D": D, "P": P, "ppn": ppn, "mode": 1, "time": np.random.rand() / 10
                }, ignore_index=True)
                demo_input_format = demo_input_format.append({
                    "D": D, "P": P, "ppn": ppn, "mode": 0, "time": np.random.rand()
                }, ignore_index=True)

demo_input_format["(P, ppn)"] = list(map(lambda x, y: ("(" + x + ", " + y + ")"), map(str, demo_input_format["P"]), map(str, demo_input_format["ppn"])))

print(demo_input_format)

# In[50]:


sns.catplot(x="(P, ppn)", y="time", data=demo_input_format, kind="box", col="D", hue="mode")
plt.show()

# In[ ]:

如何修改它以從 output.txt 文件和 plot 條形 plot 像上面一樣獲取輸入? 謝謝你。 請幫忙。 :)

首先創建列P * ppn(P, ppn)

df['P * ppn'] = df.P * df.ppn
df['(P, ppn)'] = df.apply(lambda row: f'({row.P:d}, {row.ppn:d})', axis=1)

#          D  P  ppn  mode      time  P * ppn (P, ppn)
# 0     2048  4    1     0  0.001220        4   (4, 1)
# 1     2048  4    1     1  0.000858        4   (4, 1)
# 2    32768  4    1     0  0.008137        4   (4, 1)
# ...

然后將 dataframe melt()“長”形式

melted = df.melt(id_vars=['time', 'D', '(P, ppn)', 'mode'], value_vars='P * ppn')

#         time       D (P, ppn)  mode variable  value
# 0   0.001220    2048   (4, 1)     0  P * ppn      4
# 1   0.000858    2048   (4, 1)     1  P * ppn      4
# 2   0.008137   32768   (4, 1)     0  P * ppn      4
# ...

最后catplot() melted的 dataframe 沿網格列的數據大小為D

sns.catplot(
    x='(P, ppn)',
    y='time',
    col='D',
    hue='mode',
    data=melted,
    kind='bar',
)

處理貓圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM