javascript - Plotly x轴悬停文本问题

标签 javascript plotly scatter-plot mousehover

在 Plotly 中使用散点图时,当我有超过 1 个具有相同 x 轴值的标记时,我无法获取悬停文本。

谁能帮我解决这个问题吗?

或者这是plotly 的一个错误吗?

检查最接近该线的标记。

代码在这里https://jsfiddle.net/qjdt92h2/

var trace1 = {
  x: [13.5, 12, 13, 14,13],
  y: [15, 17, 13.6, 17,18],
  text: ['4.17 below the mean', '4.17 below the mean', '0.17 below the mean', '0.17 below the mean', '0.83 above the mean', '7.83 above the mean'],
  mode: 'markers',
  name: 'Grade / Mean grade',
  marker:{
  color: 'rgb(255, 99, 132)'
  }


};

var trace2 = {
  x: [0, 20],
  y: [0, 20],
  mode: 'lines',
  name: 'Guide line',
   marker:{
  color: '#023587'
  }
};


var data = [ trace1, trace2];

var layout = {
  title:'Line and Scatter Plot'
};

Plotly.newPlot('myDiv', data, layout);

最佳答案

您可能正在寻找hovermode根据您的情况,应将其设置为最接近

var trace1 = {
  x: [13.5, 12, 13, 14, 13],
  y: [15, 17, 13.6, 17, 18],
  text: ['4.17 below the mean', '4.17 below the mean', '0.17 below the mean', '0.17 below the mean', '0.83 above the mean', '7.83 above the mean'],
  mode: 'markers',
  name: 'Grade / Mean grade',
  marker: {color: 'rgb(255, 99, 132)'}
 };

var trace2 = {
  x: [0, 20],
  y: [0, 20],
  mode: 'lines',
  name: 'Guide line',
  marker:{color: '#023587'}
};


var data = [trace1, trace2];
var layout = {
  title:'Line and Scatter Plot',
  hovermode: 'closest'
};

Plotly.newPlot('myDiv', data, layout);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myDiv"></div>

关于javascript - Plotly x轴悬停文本问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45101562/

相关文章:

javascript - 为什么是 $(window).scrollTop();报零?

javascript - 如何更改jsf primefaces中确认框的位置?

javascript - 为什么我们不能像向现有对象添加新属性一样向对象构造函数添加新属性?

javascript - svg 矩形不同边上的标签

r - 单击 R Shiny 中绘图图例中的名称时发生的事件

r - 在没有互联网的情况下从 R 中的 Plotly 导出 PNG 文件

matlab - 散点图可视化matlab中的相同点

R - 添加质心到散点图

python - 如何注释条形之间的差异?

octave - 散点图图例和填充在 Octave 中不起作用