我正在绘制某些类别中各个办公室的交叉表。我想放一张水平堆叠的条形图,在其中标记每个办公室及其值。
这是一些示例代码:
df = pd.DataFrame({'office1': pd.Series([1,np.nan,np.nan], index=['catA', 'catB', 'catC']),
'office2': pd.Series([np.nan,8,np.nan], index=['catA', 'catB', 'catC']),
'office3': pd.Series([12,np.nan,np.nan], index=['catA', 'catB', 'catC']),
'office4': pd.Series([np.nan,np.nan,3], index=['catA', 'catB', 'catC']),
'office5': pd.Series([np.nan,5,np.nan], index=['catA', 'catB', 'catC']),
'office6': pd.Series([np.nan,np.nan,7], index=['catA', 'catB', 'catC']),
'office7': pd.Series([3,np.nan,np.nan], index=['catA', 'catB', 'catC']),
'office8': pd.Series([np.nan,np.nan,11], index=['catA', 'catB', 'catC']),
'office9': pd.Series([np.nan,6,np.nan], index=['catA', 'catB', 'catC']),
})
ax = df.plot.barh(title="Office Breakdown by Category", legend=False, figsize=(10,7), stacked=True)
Run Code Online (Sandbox Code Playgroud)
这给了我一个很好的起点:
经过研究,我想到了以下代码,可以在“类别”轴上正确排列标签:
def annotateBars(row, ax=ax):
for col in row.index:
value = row[col]
if (str(value) != 'nan'):
ax.text(value/2, labeltonum(row.name), col+","+str(value))
def …
Run Code Online (Sandbox Code Playgroud) 我正在尝试按照Mike Bostock 的 v4 示例和带有鼠标悬停事件的v3 示例制作 D3 和弦图。
在上面的 v3 示例中,有一个淡入淡出功能可以突出显示鼠标悬停组的特定功能区:
function fade(opacity) {
return function(d, i) {
svg.selectAll("ribbons")
.filter(function(d) {
return d.source.index != i && d.target.index != i;
})
.transition()
.style("opacity", opacity);
};
}
Run Code Online (Sandbox Code Playgroud)
尽管在我的一生中,我无法让它在我的 v4 示例中工作,尽管我试图将它放在类似的位置:
//Draw the ribbons that go from group to group
g.append("g")
.attr("class", "ribbons")
.selectAll("path")
.data(function(chords) { return chords; })
.enter().append("path")
.attr("d", ribbon)
.style("fill", function(d) { return color(d.target.index); })
.style("stroke", function(d) { return d3.rgb(color(d.target.index)).darker(); })
.on("mouseover", fade(.1)) /* …
Run Code Online (Sandbox Code Playgroud)