簡體   English   中英

支持熊貓中的兩個數據框列

[英]suplotting two data frame columns in pandas

我試圖弄清楚如何使用matplotlib對數據框的兩列進行子圖繪制。 這是我的代碼:

plt.figure(figsize=(10,10))
plt.subplot(2,1,1)
df[['Percentage variance (%)', 'Net weight (%)']].plot(kind='bar')

我想要的最終結果是在同一圖上看到一組“百分比差異(%)”的顏色條,以及另一組“凈重(%)”的顏色條。

由於某種原因,matplotlib會忽略plt.subplot(2,1,1)命令,並且不會將條形圖放置到指定的子圖上。

有沒有人遇到過這種情況,並且知道解決方法/解決方法?

以下對我來說很好用。 您需要使用數據框的plot()方法的ax關鍵字指定要在哪個子圖上plot()

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({ 'Percentage variance (%)' : [10,20,30],
                   'Net weight (%)':[22,16,18] })
plt.figure(figsize=(10,10))
ax = plt.subplot(2,1,1)
df[['Percentage variance (%)', 'Net weight (%)']].plot(kind='bar', ax=ax)

plt.show()

暫無
暫無

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

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