如何在 Python Plotly 中将鼠标悬停在某个组上时突出显示所有值?

plotlypythonserver side programmingprogramming

Plotly 具有对数据值进行分组的功能。您还可以将鼠标悬停在某个组上时突出显示所有值。在本教程中,我们将使用 plotly.io 来生成图形。它包含许多自定义图表的方法。

按照下面给出的步骤,在悬停时突出显示组中的所有值。

步骤 1

导入 plotly.io 模块并将别名作为 pio

import plotly.io as pio

步骤 2

创建一个值列表以形成字典。

fonts = ['Arial', 'Arial', 'Courier', 'Arial', 'Courier', 'Arial']
shade = ['bold','bold','italic','italic','bold','bold']
score = [1,2,3,4,5,6]

步骤 3

根据 X 轴和 Y 轴坐标值创建散点图,并对字体应用 groupby 并为值字典设置样式。

data = [dict(
   type = 'scatter',
   x = shade,
   y = score,
   mode = 'markers',
   transforms = [dict(
      type = 'groupby',
      groups = fonts,
      styles = [
         dict(target = 'Arial', value = dict(marker = dict(color = 'blue'))),
         dict(target = 'Courier', value = dict(marker =
         dict(color = 'red'))),
         dict(target = 'bold', value = dict(marker = dict(color = 'black'))),
         dict(target = 'italic', value = dict(marker =
         dict(color = 'green')))
      ]
   )]
)]

步骤 4

让我们用值字典生成图形并绘制图形。它定义如下,

fig_dict = dict(data=data)
pio.show(fig_dict, validate=False)

示例

以下是悬停时突出显示组中的所有值的完整代码 -

import plotly.io as pio

fonts = ['Arial', 'Arial', 'Courier', 'Arial', 'Courier', 'Arial']
shade = ['bold','bold','italic','italic','bold','bold']
score = [1,2,3,4,5,6]

data = [dict(
   type = 'scatter',
   x = shade,
   y = score,
   mode = 'markers',
   transforms = [dict(
      type = 'groupby',
      groups = fonts,
      styles = [
         dict(target = 'Arial', value = dict(marker = dict(color = 'blue'))),
         dict(target = 'Courier', value = dict(marker =
         dict(color = 'red'))),
         dict(target = 'bold', value = dict(marker = dict(color = 'black'))),
         dict(target = 'italic', value = dict(marker =
         dict(color = 'green')))
      ]
   )]
)]

fig_dict = dict(data=data)
pio.show(fig_dict, validate=False)

输出

它将在浏览器上显示以下输出:−


请注意,当您将鼠标悬停在某个点上时,它将突出显示其所有值。


相关文章