search

Home  >  Q&A  >  body text

Select/pin individual pixels on Plotly heatmap

<p>On Plotly heatmaps, it is sometimes useful to have 2 selection modes: </p> <ul> <li><p>Rectangular selection (already available in modbar)</p> </li> <li><p><strong>Select/fix individual pixels</strong>: I'm trying to achieve this by recycling an existing "drawcircle" button that I don't need. When clicked, the pixel should highlight, or have a colored disk (or red "pin" like the Google Maps UI) on top of it </p> </li> </ul> <p>Problem: When the <code>drawcircle</code> tool is selected in the modbar, the <code>plotly_click</code> event does not fire (so we cannot get the coordinates), and <code>plotly_selected </code> No initial mouse click position is given. (I don't want to make a true circle, I just want to use the first click). See also Event handlers in JavaScript</p> <p> <pre class="brush:js;toolbar:false;">const z = Array.from({ length: 50 }, () => Array.from({ length: 50 }, () => Math.floor(Math.random() * 255))); const plot = document.querySelector("#plot"); const data = [{ type: 'heatmap', z: z }]; const layout = { 'yaxis': { 'scaleanchor': 'x' } }; const config = { modeBarButtons: [ ["zoom2d"], ["zoomIn2d"], ["zoomOut2d"], ["autoScale2d"], ["select2d"], ["drawcircle"] ], displaylogo: false, displayModeBar: true }; Plotly.newPlot('plot', data, layout, config); plot.on("plotly_selected", (data) => { console.log(data); }); plot.on('plotly_click', (data) => { console.log(data); });</pre> <pre class="brush:html;toolbar:false;"><script src="https://cdn.plot.ly/plotly-2.16.2.min.js"></script> <div id="plot"></div></pre> </p> <p><strong>How do I use the Select/Fix Pixels/Points mode bar tool on a Plotly heatmap? </strong></p> <p>Note: Python documentation is more complete than the JS version: Add/Remove Modal Bar Buttons</p>
P粉806834059P粉806834059491 days ago662

reply all(1)I'll reply

  • P粉662361740

    P粉6623617402023-09-03 09:53:17

    Studying your example, I found that your console.log does not provide the expected data because the data is circular. However, since data.points has a single element, you can get the coordinates via data.points[0].x, data.points[0]. They are y and data.points[0].z respectively.

    const z = Array.from({
      length: 50
    }, () => Array.from({
      length: 50
    }, () => Math.floor(Math.random() * 255)));
    const plot = document.querySelector("#plot");
    const data = [{
      type: 'heatmap',
      z: z
    }];
    const layout = {
      'yaxis': {
        'scaleanchor': 'x'
      }
    };
    const config = {
      modeBarButtons: [
        ["zoom2d"],
        ["zoomIn2d"],
        ["zoomOut2d"],
        ["autoScale2d"],
        ["select2d"],
        ["drawcircle"]
      ],
      displaylogo: false,
      displayModeBar: true
    };
    Plotly.newPlot('plot', data, layout, config);
    
    plot.on("plotly_selected", (data) => {
      console.log(data);
    });
    plot.on('plotly_click', function(data) {
      console.log({x: data.points[0].x, y: data.points[0].y, z: data.points[0].z});
    });
    <script src="https://cdn.plot.ly/plotly-2.16.2.min.js"></script>
    <div id="plot"></div>

    reply
    0
  • Cancelreply