簡體   English   中英

如何在同一 plot 上使用不同長度的數據 plot 多個 matplotlib 條形圖?

[英]How do I plot multiple matplotlib bar charts on the same plot with varying lengths of data?

我正在制定一個預算計划,最終結果將是一個帶有多個條形的條形圖,顯示按日期划分的 6 個類別的總支出。 到目前為止,我已經能夠清理數據以顯示日期、類別和支出(從 Google 表格工作簿中提取):

def process_data(worksheet):
   data = worksheet.get_all_records()
   all_cost = []
   for i in range(len(data)):
       date = data[i].get('Timestamp').split()
       cost = data[i].get("Cost")
       category = data[i].get("Category")
       newdict = {"Date": date[0], "Spend": cost, "Category":category}
       all_cost.append(newdict)
   df = pd.DataFrame(all_cost)

output dataframe 看起來像這樣:

         Date   Spend               Category
0   11/25/2020   54.32                Grocery
1   11/25/2020   49.77               Projects
2   11/25/2020   34.22                    Gas
3   11/25/2020   46.00                Grocery
4   11/27/2020   37.00  Restaurant/Eating Out
5   11/27/2020   72.00                Grocery
6   11/27/2020  129.31               Projects
7   11/27/2020   32.00                  Bills
8   11/27/2020   69.42                Grocery
9   11/28/2020   12.69                    Gas
10  11/28/2020   69.69  Restaurant/Eating Out
11  11/28/2020    6.66                  Other
12  11/28/2020  444.21               Projects
13  11/29/2020   73.00                Grocery

然后我補充說:

new_df = df.groupby(["Date", "Category"])["Spend"].sum().reset_index()

為了總結每天的總分類支出:

     Date               Category   Spend
0   11/25/2020                    Gas   34.22
1   11/25/2020                Grocery  100.32
2   11/25/2020               Projects   49.77
3   11/27/2020                  Bills   32.00
4   11/27/2020                Grocery  141.42
5   11/27/2020               Projects  129.31
6   11/27/2020  Restaurant/Eating Out   37.00
7   11/28/2020                    Gas   12.69
8   11/28/2020                  Other    6.66
9   11/28/2020               Projects  444.21
10  11/28/2020  Restaurant/Eating Out   69.69

我想添加一個帶有 matplotlib 的條形圖來顯示每天每個類別花費的美元明細,但是我不確定如何執行此操作,因為記錄的類別數量因日期而異。

go 關於這樣做的最佳方法是在 append 中為每個類別設置一行“0”而無需花費嗎? 或者有沒有更簡單的方法來 plot 這個? 謝謝!

編輯:

我意識到我構建 dataframe 的方式並不理想。 我認為通過在示例中按如下方式構造數據會更容易實現我想要做的事情:

df_test = pd.DataFrame({"11/25/2020": {"Gas":34.22, "grocery": 69.68, "Bills":15, "Gas": 10}, "11/27/2020": {"Gas": 16, "Projects": 144, "Bills":20}})
print(df_test)

實現結果:

              11/25/2020  11/27/2020
Gas            10.00        16.0
grocery        69.68         NaN
Bills          15.00        20.0
Projects         NaN       144.0

但是,我不太確定如何迭代所有數據行以實現所需的 output。 此外,我知道我不能為此使用字典,因為它只會使用最后一組鍵/值,而我想對每個類別的金額求和。

我應該使用元組嗎? 我將如何遍歷每一行數據並對每個類別的元組值求和? 謝謝!

為了將plot多個matplotlib條形圖放在同一個plot上,理想的方法是利用子圖的概念。 下面顯示的是我使用隨機數據研究的一個簡短示例,但我相信您可以輸入自己的數據集和 plot 同樣:

代碼:

fig2, ax = plt.subplots(2,2,sharex='col',sharey='row')
x = np.arange(1,11)
y = np.arange(1,11)
z = np.random.randn(10)
 
#Subplot1
ax[0][0].plot(x,y,color='g',marker='p',label='axes1')
ax[0][0].set_title('Axes1')
ax[0][0].legend()
 
#Subplot2
ax[0][1].scatter(x,y,color='k',marker='^',label='axes2')
ax[0][1].set_title('Axes2')
ax[0][1].legend()
 
#Subplot3
ax[1][0].plot(x,z,marker='v',label='axes3')
ax[1][0].set_title('Axes3')
ax[1][0].legend()
 
#Subplot4
ax[1][1].scatter(x,z,marker='o',label='axes4')
ax[1][1].set_title('Axes4')
ax[1][1].legend()
 
plt.tight_layout()
plt.show()

Output;

這樣做您會意識到 output 圖像中具有不同數據長度(參見 x 和 y 軸)的子圖繪制如下:

輸出圖像

暫無
暫無

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

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