Pandas 当使用plotly+时,如何为数据帧中的多个直方图使用特定的容器列表;袖扣?

Pandas 当使用plotly+时,如何为数据帧中的多个直方图使用特定的容器列表;袖扣?,pandas,histogram,plotly,Pandas,Histogram,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)

使用
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()
pd.DataFrame({
    'firstHistogram': np.random.randn(10000),
    'secondHistogram': 0.2 * np.random.randn(10000)
}).iplot(kind='hist', bins=100)

这也可以通过
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))
进一步说,
plotly
允许通过
袖扣
模块直接连接到
熊猫
,该模块允许执行以下操作:

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()
pd.DataFrame({
    'firstHistogram': np.random.randn(10000),
    'secondHistogram': 0.2 * np.random.randn(10000)
}).iplot(kind='hist', bins=100)

但这里有一个陷阱:
iplot
袖扣提供的
iplot
方法似乎不接受
bin
的列表。 如上例所示,当提供一个数字时,该数字用于独立地对两个数据集进行装箱,这会导致不相等的装箱,并可能产生误导性结果(请参见上图中的等高)

虽然使用
histnorm='density'
选项可以在一定程度上缓解这种影响,但人们可能希望看到每个箱子的计数,而不是密度


有办法解决这个问题吗?

据我所知,没有直接的袖扣方法。在我看来,代码中显示的输出是错误的,也就是说,我认为这是袖扣中的一个缺陷

但只需几行代码,就可以轻松模仿袖扣的功能。您可以使用
袖扣获得相同的布局。getLayout()
,只需将
barmode
设置为
overlay


我已经为此添加了一个更新。 您现在应该能够指定
bin=(开始、结束、大小)

现在应返回:

太好了,非常感谢!plotly上的文档应该更新以反映此功能:我已经提交了一个pull请求:正如我所见,此功能仍然不存在