Plotly x-axis hover text issue

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 13:07:14

问题


When using a Scatter plot with Plotly i am unable to get hover text when i have more than 1 marker with the same x-axis value.

Can anyone please help me fix this?

Or is this a bug with plotly?

Check the marker that is closest to the line.

Code is here 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);

回答1:


You are probably looking for hovermode which should be set to closest in your case.

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>


来源:https://stackoverflow.com/questions/45101562/plotly-x-axis-hover-text-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!