按照Plotly的指导,我想绘制类似以下代码的图表:
import plotly.plotly as py
import plotly.figure_factory as ff
import numpy as np
# Add histogram data
x1 = np.random.randn(200) - 2
x2 = np.random.randn(200)
x3 = np.random.randn(200) + 2
x4 = np.random.randn(200) + 4
# Group data together
hist_data = [x1, x2, x3, x4]
group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']
# Create distplot with custom bin_size
fig = ff.create_distplot(hist_data, group_labels, bin_size = [.1, .25, .5, 1])
# Plot!
py.iplot(fig, filename = 'Distplot with Multiple Bin Sizes')
然而,我有一个真实的数据集,其样本量不均匀(即组1的计数与组2中的计数不同等)。此外,它是以名称-值对的格式呈现。
以下是一些虚拟数据用于说明:
# Add histogram data
x1 = pd.DataFrame(np.random.randn(100))
x1['name'] = 'x1'
x2 = pd.DataFrame(np.random.randn(200) + 1)
x2['name'] = 'x2'
x3 = pd.DataFrame(np.random.randn(300) - 1)
x3['name'] = 'x3'
df = pd.concat([x1, x2, x3])
df = df.reset_index(drop = True)
df.columns = ['value', 'names']
df
如您所见,每个名称(x1,x2,x3)都有不同的计数,并且“names”列是我想要用作颜色的列。
有人知道我如何在plotly中绘制这个图吗?
FYI,在R中,非常简单,我只需调用ggplot,并使用aes(fill = names)。
任何帮助将不胜感激,谢谢!