Search code examples
pythonpandashistogram

Multiple histograms from multiple dataframes into one in pandas


I am beginner in python and pandas I have three CSV data. I want to make one histogram from these three dataframe. I used this code

import pandas as pd
from matplotlib import pyplot as plt
X = pd.read_csv("data1.csv")
Y = pd.read_csv("data2.csv")
Z = pd.read_csv("data3.csv")

X.hist(column='speed', weights=X.ID,figsize=(20,10), stacked=True, bins=50, color = 'Blue', )
Y.hist(column='speed', weights=Y.ID,figsize=(20,10), stacked=True, bins=50, color = 'Red')
Z.hist(column='speed', weights=Z.ID,figsize=(20,10), stacked=True, bins=50, color = 'Grey')
plt.rc('xtick',labelsize=25)
plt.rc('ytick',labelsize=25)

enter image description here

enter image description here

enter image description here

but I got three different histograms. How to make these three into one histogram with three colour of each histogram included?


Solution

  • I would go for this:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    X = pd.read_csv("data1.csv")
    Y = pd.read_csv("data2.csv")
    Z = pd.read_csv("data3.csv")
    
    plt.hist([X.speed.values.flatten(), Y.speed.values.flatten(), Z.speed.values.flatten()], weights=[X.ID.values.flatten(), Y.ID.values.flatten(), Z.values.flatten()], label=['X', 'Y', 'Z'])
    plt.legend()
    plt.rc('xtick', labelsize=25)
    plt.rc('ytick', labelsize=25)