glS*_*glS 4 histogram pandas plotly
当使用绘制直方图时matplotlib,手动给出垃圾箱列表相对容易,例如此处所示。
一个简单的例子如下:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.hist(np.random.randn(10000), bins=np.arange(-4, 4, 0.1))
ax.hist(0.2 * np.random.randn(10000), bins=np.arange(-4, 4, 0.1))
plt.show()
Run Code Online (Sandbox Code Playgroud)

也可以使用以下命令等效地完成此操作pandas.DataFrame:
pd.DataFrame({
'firstHistogram': np.random.randn(10000),
'secondHistogram': 0.2 * np.random.randn(10000)
}).plot(kind='hist', bins=np.arange(-4, 4, 0.1))
Run Code Online (Sandbox Code Playgroud)
更进一步,plotly允许直接pandas通过cufflinks模块进行接口,从而可以执行以下操作:
pd.DataFrame({
'firstHistogram': np.random.randn(10000),
'secondHistogram': 0.2 * np.random.randn(10000)
}).iplot(kind='hist', bins=100)
Run Code Online (Sandbox Code Playgroud)
但这很重要:所iplot提供的方法cufflinks似乎不接受的列表bins。当像上面的示例中那样提供数字时,该数字用于独立地对两个数据集进行装箱,这导致装箱不均等,并可能产生误导性的结果(请参见上图中的相同高度)。
尽管使用该histnorm='density'选项可以稍微减轻这种影响,但您可能希望查看每个仓的计数而不是密度。
有没有解决的办法?
我为此添加了更新。您现在应该可以指定bins=(start,end,size)
pd.DataFrame({
'firstHistogram': np.random.randn(10000),
'secondHistogram': 0.2 * np.random.randn(10000)}).iplot(kind='hist',bins=(-4,4,.08))
Run Code Online (Sandbox Code Playgroud)
现在应该返回: 自定义垃圾箱
| 归档时间: |
|
| 查看次数: |
1187 次 |
| 最近记录: |