Plotly:如何编辑统一的 hoverlabel?

Plotly: How to edit unified hoverlabel?

我正在生成具有两条不同等高线的图表。当我将鼠标悬停在图表上时,它会将菜单与图像中两个轮廓的值进行比较:

我用这段代码得到了这个结果:

# IMPORT
import plotly.graph_objs as go
import plotly.offline as pyo
import numpy as np

# CONSTANTS SET UP
N = 11
x_min = 0
x_max = 10
y_min = 0
y_max = 10
fontsize = 18

# COLORS SET UP
blue = '#6683f3'
orange = '#ff9266'
black = '#212121'

# DEFINE THE MESH GRID
x = np.linspace(x_min, x_max, N)
y = np.linspace(y_min, y_max, N)
XX, YY = np.meshgrid(x, y)

# CALCULATE Z1 AND Z2
Z1 = XX + YY
Z2 = XX - YY

# DEFINE THE TRACES LIST
data = [go.Contour(z = Z1,
                   transpose = True,
                   name = 'Z1',
                   zmin = np.min(Z1),
                   zmax = np.max(Z1) + 1,
                   hovertemplate = 'Z1 = %{z: .0f}<extra></extra>',
                   contours_coloring = 'lines',
                   showscale = False,
                   showlegend = True,
                   colorscale = [[0, orange], [1, orange]],
                   line_width = 4,
                   ncontours = 20,
                   contours = dict(showlabels = True,
                                   labelformat = '.0f',
                                   labelfont = dict(size = 18))),
        go.Contour(z = Z2,
                   transpose = True,
                   name = 'Z2',
                   zmin = np.min(Z2),
                   zmax = np.max(Z2) + 1,
                   hovertemplate = 'Z2 = %{z: .0f}<extra></extra>',
                   contours_coloring = 'lines',
                   showscale = False,
                   showlegend = True,
                   colorscale = [[0, blue], [1, blue]],
                   line_width = 4,
                   ncontours = 20,
                   contours = dict(showlabels = True,
                                   labelformat = '.0f',
                                   labelfont = dict(size = fontsize)))]

# DEFINE THE LAYOUT
layout = go.Layout(plot_bgcolor = black,
                   hovermode = 'x')

# DEFINE THE FIGURE
figure = go.Figure(data = data,
                   layout = layout)

# IMPROVE LEGEND AND HOVERLABEL ASPECT
figure.update_layout(legend = dict(itemsizing = 'constant',
                                   font = dict(size = fontsize)),
                     hoverlabel = dict(font_size = fontsize))

# PLOT THE FIGURE
pyo.plot(figure)

在布局定义部分,如果我将 hovermode = 'x' 替换为 hovermode = 'x unified',我会得到以下结果:

我想从此菜单中删除包含有关 x 的信息的第一行。老实说,我不知道从哪里开始,我试图咨询 plotly documentation 但我没有得到任何线索。
我会得到什么(手动编辑的图像):

一种可能的替代方法是改进第一张图片中出现的菜单的外观:

版本信息:

Python  4.7.0
dash    1.12.0
plotly  4.7.0

如果这是您想要的输出:

然后只包括:

figure.update_traces(hoverinfo = 'name+z')

并且,对于 go.Contour,删除 hovertemplate 部分:

 #hovertemplate = 'Z1 = %{z: .0f}<extra></extra>',
 .
 .
 #hovertemplate = 'Z2 = %{z: .0f}<extra></extra>',

剧情:

一些重要细节:

如果在 go.Contour() 中定义了悬停模板,figure.update_traces(hoverinfo = 'name+z') 或类似的设置似乎将无效。至少那是我发现的情况。如果您在测试后有任何其他发现,请告诉我!

完整代码:

# IMPORT
import plotly.graph_objs as go
import plotly.offline as pyo
import numpy as np

# CONSTANTS SET UP
N = 11
x_min = 0
x_max = 10
y_min = 0
y_max = 10
fontsize = 18

# COLORS SET UP
blue = '#6683f3'
orange = '#ff9266'
black = '#212121'

# DEFINE THE MESH GRID
x = np.linspace(x_min, x_max, N)
y = np.linspace(y_min, y_max, N)
XX, YY = np.meshgrid(x, y)

# CALCULATE Z1 AND Z2
Z1 = XX + YY
Z2 = XX - YY

# DEFINE THE TRACES LIST
data = [go.Contour(z = Z1,
                   transpose = True,
                   name = 'Z1',
                   zmin = np.min(Z1),
                   zmax = np.max(Z1) + 1,
                   #hovertemplate = 'Z1 = %{z: .0f}<extra></extra>',
                   contours_coloring = 'lines',
                   showscale = False,
                   showlegend = True,
                   colorscale = [[0, orange], [1, orange]],
                   line_width = 4,
                   ncontours = 20,
                   contours = dict(showlabels = True,
                                   labelformat = '.0f',
                                   labelfont = dict(size = 18))),
        go.Contour(z = Z2,
                   transpose = True,
                   name = 'Z2',
                   zmin = np.min(Z2),
                   zmax = np.max(Z2) + 1,
                  # hovertemplate = 'Z2 = %{z: .0f}<extra></extra>',
                   contours_coloring = 'lines',
                   showscale = False,
                   showlegend = True,
                   colorscale = [[0, blue], [1, blue]],
                   line_width = 4,
                   ncontours = 20,
                   contours = dict(showlabels = True,
                                   labelformat = '.0f',
                                   labelfont = dict(size = fontsize)))]

# DEFINE THE LAYOUT
layout = go.Layout(plot_bgcolor = black,
                   hovermode = 'x unified')

# DEFINE THE FIGURE
figure = go.Figure(data = data,
                   layout = layout)

figure.update_traces(hoverinfo = 'name+z')

figure.update_layout(legend = dict(itemsizing = 'constant',
                                   font = dict(size = fontsize)),
                     hoverlabel = dict(font_size = fontsize))

figure.show()