Python 如何在Plotly中将一组值悬停时突出显示所有值

Python 如何在Plotly中将一组值悬停时突出显示所有值

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)

输出

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

Python 如何在Plotly中将一组值悬停时突出显示所有值

注意,当你将鼠标悬停在一个点上时,它会突出显示所有的值。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程