简体   繁体   中英

suplotting two data frame columns in pandas

I'm trying to figure out how to subplot two columns of a data frame using matplotlib. Here is my code:

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

The end result I'm aiming for is to see one set of color bars for 'Percentage variance (%)', and another set of color bars for 'Net Weight (%)' both on the same figure.

For some reason matplotlib ignores the plt.subplot(2,1,1) commmand and won't place the bar graph onto the designated subplot.

Has anyone ever encountered this and know of a fix/workaround?

The following works fine for me. You need to specify in which subplot you want to plot using the ax keyword of the dataframe's plot() method.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM