简体   繁体   中英

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)

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

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

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)

Merge your dataframes:

X = pd.read_csv("data1.csv")
Y = pd.read_csv("data2.csv")
Z = pd.read_csv("data3.csv")
df = X.merge(Y).merge(Z)

df.hist(...)

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