Plotly-Dash:如何打印条形图的hoverData?

时间:2019-03-29 05:27:24

标签: python plotly-dash plotly-python

我的Dash应用程序中有条形图。我想将鼠标悬停在特定栏上时打印悬停数据。

我在回调中尝试了go.Bar()的hoverData属性。

# mark all the places where A changes in vertical direction
# pad in such a way that the first change in each column is up and the last down
B = np.empty(np.array([1,0])+A.shape, int)
B[:-1] = A
B[1:-1] ^= A[:-1]
B[-1] = A[-1]

# retrieve coordinates of switch points
x, y = np.where(B.T)
# group in pairs, the differences in y are the hole depths
x = x[::2]
d = np.subtract(*y.reshape(-1, 2).T[::-1])

# exclude holes that were introduced by padding
x, d = x[y[1::2]!=len(A)], d[y[1::2]!=len(A)]

# now we have the column numbers and hole depths
x
# array([1, 2, 2, 3, 5, 6, 6, 7, 7, 8, 8, 9])
d
# array([3, 1, 1, 1, 3, 8, 2, 1, 2, 1, 1, 3])

# the sum of the depths
d.sum()
# 27

# and the rows with holes
unq = np.unique(y[1::2])
# make sure not to count padded holes
unq.size - (unq[-1] == len(A))
# 7

预期结果:悬停的栏的悬停数据

实际结果:无

1 个答案:

答案 0 :(得分:0)

您需要返回 hover_data对象,而不是打印

@app.callback(Output('hover', 'children'), [Input('main_graph', 'hoverData')])
def disp_hover_data(hover_data):
    return hover_data

现在,如果您检查“服务器”日志(即运行该终端的终端),则会看到hover_data打印在其中。如果您对其进行了修复,它将显示在hover中。