情节:如何编辑统一的悬浮标签?

问题描述 投票:1回答:1

我正在生成带有两条不同轮廓线的图形。当我将鼠标悬停在图形上时,它会将菜单与两个轮廓的值进行比较,如图所示:

enter image description here

我通过以下代码获得了此结果:

# 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',则会得到以下结果:

enter image description here

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

enter image description here

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

  • 为两个轮廓添加独特的背景
  • 为每个轮廓添加彩色线

版本信息:

Python  4.7.0
dash    1.12.0
plotly  4.7.0
html css hover plotly contour
1个回答
3
投票

如果这是您想要的输出:

enter image description here

然后只包括:

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

并且,对于go.Contour,都将hovertemplate部分放下:

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

图:

enter image description here

一些重要细节:

[如果figure.update_traces(hoverinfo = 'name+z')中定义了悬停模板,则go.Contour()或类似设置似乎没有效果。至少我发现是这种情况。经过测试后,如果您还有其他发现,请告诉我!

完整代码:

# 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()
© www.soinside.com 2019 - 2024. All rights reserved.