4

I am trying to plot bar style histograms with multiple classifications as stored in a pandas dataframe. I can get the the histograms to plot using both the methods below, however they cover each other and I'd like to plot them side by side. In the second case, the histtype variable appears to be ignored.

Is there a way to do this using one of the two methods below?

I have tried like this

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
colour = 0
for var in array:
    l = [var]
    df_array=df.loc[df.var.isin(l) , :]
    df_array[v].dropna(inplace=True)
    plt3=sns.distplot(df_array[v],label=var,kde=False)
    colour += 1

Seaborn Type Plot

and like this

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
colour = 0
for var in array:
    df_array=df.loc[df.var.isin([var]) , :]
    df_array[v].dropna(inplace=True)
    plt4 = plt.hist(list(df_array[df_array['var'] == var][v]), 
                     histtype = 'bar', stacked = False)
    colour += 1

Matplotlib type Plot

I would like to produce a final plot similar to this Side by Side Histogram from Web

11
  • It is a bit unclear what you are asking. Do you want each iteration of the for loop to produce a new subplot? See matplotlib.org/examples/pylab_examples/subplots_demo.html You could also look at something like this: seaborn.pydata.org/examples/kde_ridgeplot.html Commented Sep 20, 2018 at 10:35
  • Thanks @KPLauritzen I have added a clarifying image above. I'd like all the histgrams on the same plot with the same bins. Commented Sep 20, 2018 at 21:18
  • Check Gustavo Bezerra's answer on this thread. It should be what you want. stackoverflow.com/questions/6871201/… Commented Sep 21, 2018 at 0:40
  • Thanks @Saedeas. I had seen that post. The solution doesn;t quite work in this isnstance as it requires all the lists to be generated as independent lists and then run through the histogram together. This would mean I have to name a new list inside my loop and then run the histogram. Commented Sep 21, 2018 at 1:20
  • 1
    I think the OP did it :) Commented Nov 5, 2018 at 17:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.