我正在生成带有两个不同轮廓线的图形。当我将鼠标悬停在图形上时,它会将菜单与两个轮廓的值进行比较,如图所示:
我通过以下代码获得了此结果:
# 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
答案 0 :(得分:2)
如果这是您想要的输出:
然后只包括:
figure.update_traces(hoverinfo = 'name+z')
然后,对于两个go.Contour
,删除hovertemplate
部分:
#hovertemplate = 'Z1 = %{z: .0f}<extra></extra>',
.
.
#hovertemplate = 'Z2 = %{z: .0f}<extra></extra>',
情节:
一些重要的详细信息:
如果在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()