我正在嘗試使用具有以下結構的 matplotlib 和 pandas 從 csv 繪制圖形:

總共有 36 種可能的狀態,我希望它們中的每一種都有一條線。有 100 次迭代。迭代將從0開始,每次增加1。每個狀態都會有每次第 i 次迭代的記錄。這些線以迭代作為 x 軸,實用程式作為 y 軸繪制,如下所示。

我已經從 csv 檔案中制作了一個資料框,但據我所知。
value_headers = ['State', 'Iteration', 'Utility']
value_df = pd.read_csv('example.csv', names = value_headers, index_col='State')
編輯:這是我的資料框的字典
在過去的一個小時里,我一直在為此絞盡腦汁,但無濟于事。我什至不知道如何用簡潔的句子來描述這個問題以進行故障排除。另外,我在stackoverflow上找不到類似于我的問題。任何建議將不勝感激。
uj5u.com熱心網友回復:
如果我正確閱讀了您的 DataFrame,您需要確定給定狀態識別符號何時出現在 csv 的第一列中,然后將這些值一起使用來繪制圖。根據您的單個值為 的示例Iteration,我建議如下(假設您的 DataFrame 被呼叫df)
import matplotlib.pyplot as plt
import pandas as pd
fig, ax = plt.subplots()
df = df.iloc[1:,:]
df = df.reset_index()
# the state identifier is now a column with the header 'index'
for idx in df['index'].unique():
subset = df[df['index']==idx]
ax.plot(subset['Iteration'], subset['Utility'])
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451519.html
標籤:Python matplotlib
