How to prevent bar plots from superimposing on each other in pandas?

I am working with some data in pandas and I want to generate 2 separate bar plots based on the data. However, my two bar plots instead get superimposed on each-other (they are in the same graph). Here is my code:

import math
import pandas as pd
pd.options.mode.chained_assignment = None  # default='warn'
import numpy as np
import matplotlib.pyplot as plt
from openpyxl import load_workbook

def gen(fileIn):
    DF= pd.read_excel(fileIn)
    
    overall = DF['Awesome'].value_counts(normalize = True) # Get the relative frequency of each unique value in the column Awesome
    print(overall.plot(kind = 'bar', figsize = (10,5)))
    
    spec = DF['Not Awesome'].value_counts(normalize = True) 
    print(spec.plot(kind = 'bar', color = 'red', figsize = (10,5)))
                
gen("my file path")

This is the output:
enter image description here

As you can see, the red color from the ‘Not Awesome’ column get supplanted on the ‘Awesome’ column’s relative frequency values. I just want the two bar plots to be separate. I looked at the documentation in https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html for the plot function, however, there don’t seem to be any parameters I can use to turn off the superimposition which seems to be a default.

>Solution :

You have to create two subplots:

def gen(fileIn):
    DF = pd.read_excel(fileIn)
    
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

    overall = DF['Awesome'].value_counts(normalize=True)
    print(overall.plot(kind='bar', ax=ax1))
    
    spec = DF['Not Awesome'].value_counts(normalize=True) 
    print(spec.plot(kind='bar', color='red', ax=ax2))

Output:

enter image description here

But you can also want:

def gen(fileIn):
    DF = pd.read_excel(fileIn)

    overall = DF['Awesome'].value_counts(normalize=True)
    spec = DF['Not Awesome'].value_counts(normalize=True) 

    ax = (pd.concat([overall, spec], keys=['Awesome', 'Not Awesome'], axis=1)
            .plot(kind='bar', color=['blue', 'red'], figsize=(10,5)))

Output:

enter image description here

Leave a Reply