密谋:如何编辑统一的悬浮标签?

时间:2020-06-01 22:41:11

标签: python html css plotly contour

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

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

1 个答案:

答案 0 :(得分:2)

如果这是您想要的输出:

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()