pandas 使用熊猫叠加多个直方图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19280336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overlaying multiple histograms using pandas
提问by Everaldo Aguiar
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.
我有两个或三个具有相同标题的 csv 文件,并且想为在同一绘图上相互重叠的每一列绘制直方图。
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 thisbut using dataframes.
以下代码为我提供了两个单独的图形,每个图形都包含每个文件的所有直方图。是否有一种紧凑的方法可以使用 pandas/matplot lib 将它们绘制在同一个图形上?我想象一些接近于此但使用数据帧的东西。
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()
回答by Phillip Cloud
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()
gives
给

