| 25 | |
| 26 | |
| 27 | def get_figure(df, x_col, y_col, selectedpoints, selectedpoints_local): |
| 28 | |
| 29 | if selectedpoints_local and selectedpoints_local['range']: |
| 30 | ranges = selectedponts_local['range'] |
| 31 | selection_bounds = { |
| 32 | 'x0': ranges['x'][0], |
| 33 | 'x1': ranges['x'][1], |
| 34 | 'y0': ranges['y'][0], |
| 35 | 'y1': ranges['y'][1] |
| 36 | } |
| 37 | else: |
| 38 | selection_bounds = { |
| 39 | 'x0': np.min(df[x_col]), |
| 40 | 'x1': np.max(df[x_col]), |
| 41 | 'y0': np.min(df[y_col]), |
| 42 | 'y1': np.max(df[y_col]) |
| 43 | } |
| 44 | |
| 45 | # set which points are selected with the `selectedpoints` property |
| 46 | # and style those points with the `selected` and `unselected` |
| 47 | # attribute. see |
| 48 | # https://medium.com/@plotlygraphs/notes-from-the-latest-plotly-js-release-b035a5b43e21 |
| 49 | # for an explanation |
| 50 | fig = px.scatter(df, x=df[x_col], y=df[y_col], text=df.index) |
| 51 | |
| 52 | fig.update_traces(selectedpoints=selectedpoints, |
| 53 | customdata=df.index, |
| 54 | mode='markers+text', |
| 55 | marker={ |
| 56 | 'color': 'rgba(0, 116, 217, 0.7)', |
| 57 | 'size': 20 |
| 58 | }, |
| 59 | unselected={ |
| 60 | 'marker': { |
| 61 | 'opacity': 0.3 |
| 62 | }, |
| 63 | 'textfont': { |
| 64 | 'color': 'rgba(0, 0, 0, 0)' |
| 65 | } |
| 66 | }) |
| 67 | |
| 68 | fig.update_layout(margin={ |
| 69 | 'l': 20, |
| 70 | 'r': 0, |
| 71 | 'b': 15, |
| 72 | 't': 5 |
| 73 | }, |
| 74 | dragmode='select', |
| 75 | hovermode=False) |
| 76 | |
| 77 | fig.add_shape( |
| 78 | dict( |
| 79 | { |
| 80 | 'type': 'rect', |
| 81 | 'line': { |
| 82 | 'width': 1, |
| 83 | 'dash': 'dot', |
| 84 | 'color': 'darkgrey' |