Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

python - Overlaying multiple histograms using pandas

I have two or three csv files with the same header and would like to draw the histograms for each column overlaying one another on the same plot.

The following code gives me two separate figures, each containing all histograms for each of the files. Is there a compact way to go about plotting them together on the same figure using pandas/matplot lib? I imagine something close to this but using dataframes.

Code:

import pandas as pd
import matplotlib.pyplot as plt

df =  pd.read_csv('input1.csv')
df2 = pd.read_csv('input2.csv')
df.hist(bins=20)
df2.hist(bins=20)

plt.show()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
In [18]: from pandas import DataFrame

In [19]: from numpy.random import randn

In [20]: df = DataFrame(randn(10, 2))

In [21]: df2 = DataFrame(randn(10, 2))

In [22]: axs = df.hist()

In [23]: for ax, (colname, values) in zip(axs.flat, df2.iteritems()):
   ....:     values.hist(ax=ax, bins=10)
   ....:

In [24]: draw()

givesenter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...