|
6 | 6 | extension: .md |
7 | 7 | format_name: markdown |
8 | 8 | format_version: '1.2' |
9 | | - jupytext_version: 1.3.2 |
| 9 | + jupytext_version: 1.3.0 |
10 | 10 | kernelspec: |
11 | 11 | display_name: Python 3 |
12 | 12 | language: python |
@@ -716,5 +716,91 @@ fig.update_layout( |
716 | 716 | fig.show() |
717 | 717 | ``` |
718 | 718 |
|
| 719 | +### Drawing shapes on Cartesian plots |
| 720 | + |
| 721 | +_introduced in plotly 4.7_ |
| 722 | + |
| 723 | +You can create layout shapes programatically, but you can also draw shapes manually by setting the `dragmode` to one of the shape-drawing modes: `'drawline'`,`'drawopenpath'`, `'drawclosedpath'`, `'drawcircle'`, or `'drawrect'`. If you need to switch between different shape-drawing or other dragmodes (panning, selecting, etc.), [modebar buttons can be added](/python/configuration-options#add-optional-shapedrawing-buttons-to-modebar) in the `config` to select the dragmode. If you switch to a different dragmode such as pan or zoom, you will need to select the drawing tool in the modebar to go back to shape drawing. |
| 724 | + |
| 725 | +This shape-drawing feature is particularly interesting for annotating graphs, in particular [image traces](/python/imshow) or [layout images](/python/images). |
| 726 | + |
| 727 | +Once you have drawn shapes, you can select and modify an existing shape by clicking on its boundary (note the arrow pointer). Its fillcolor turns to pink to highlight the activated shape and then you can |
| 728 | +- drag and resize it for lines, rectangles and circles/ellipses |
| 729 | +- drag and move individual vertices for closed paths |
| 730 | +- move individual vertices for open paths. |
| 731 | + |
| 732 | +An activated shape is deleted by cliking on the `eraseshape` button. |
| 733 | + |
| 734 | +Drawing or modifying a shape triggers a `relayout` event, which [can be captured by a callback inside a Dash application](https://dash.plotly.com/interactive-graphing). |
| 735 | + |
| 736 | +```python |
| 737 | +import plotly.graph_objects as go |
| 738 | +fig = go.Figure() |
| 739 | +text="Click and drag here <br> to draw a rectangle <br><br> or select another shape <br>in the modebar" |
| 740 | +fig.add_annotation( |
| 741 | + x=0.5, |
| 742 | + y=0.5, |
| 743 | + text=text, |
| 744 | + xref="paper", |
| 745 | + yref="paper", |
| 746 | + showarrow=False, |
| 747 | + font_size=20 |
| 748 | +) |
| 749 | +# shape defined programatically |
| 750 | +fig.add_shape(editable=True, |
| 751 | + x0=-1, x1=0, y0=2, y1=3, |
| 752 | + xref='x1', yref='y1') |
| 753 | +# define dragmode and add modebar buttons |
| 754 | +fig.update_layout(dragmode='drawrect') |
| 755 | +fig.show(config={'modeBarButtonsToAdd':['drawline', |
| 756 | + 'drawopenpath', |
| 757 | + 'drawclosedpath', |
| 758 | + 'drawcircle', |
| 759 | + 'drawrect', |
| 760 | + 'eraseshape' |
| 761 | + ]}) |
| 762 | +``` |
| 763 | + |
| 764 | +### Style of user-drawn shapes |
| 765 | + |
| 766 | +The layout `newshape` attribute controls the visual appearance of new shapes drawn by the user. `newshape` attributes have the same names as layout shapes. |
| 767 | + |
| 768 | +_Note on shape opacity_: having a new shape's opacity > 0.5 makes it possible to activate a shape by clicking inside the shape (for opacity <= 0.5 you have to click on the border of the shape), but you cannot start a new shape within an existing shape (which is possible for an opacity <= 0.5). |
| 769 | + |
| 770 | +```python |
| 771 | +import plotly.graph_objects as go |
| 772 | +fig = go.Figure() |
| 773 | +text="Click and drag<br> to draw a rectangle <br><br> or select another shape <br>in the modebar" |
| 774 | +fig.add_annotation( |
| 775 | + x=0.5, |
| 776 | + y=0.5, |
| 777 | + text=text, |
| 778 | + xref="paper", |
| 779 | + yref="paper", |
| 780 | + showarrow=False, |
| 781 | + font_size=20 |
| 782 | +) |
| 783 | +# shape defined programatically |
| 784 | +fig.add_shape(line_color='yellow', |
| 785 | + fillcolor='turquoise', |
| 786 | + opacity=0.4, |
| 787 | + editable=True, |
| 788 | + x0=0, x1=1, y0=2, y1=3, |
| 789 | + xref='x1', yref='y1' |
| 790 | +) |
| 791 | +fig.update_layout(dragmode='drawrect', |
| 792 | + # style of new shapes |
| 793 | + newshape=dict(line_color='yellow', |
| 794 | + fillcolor='turquoise', |
| 795 | + opacity=0.5)) |
| 796 | +fig.show(config={'modeBarButtonsToAdd':['drawline', |
| 797 | + 'drawopenpath', |
| 798 | + 'drawclosedpath', |
| 799 | + 'drawcircle', |
| 800 | + 'drawrect', |
| 801 | + 'eraseshape' |
| 802 | + ]}) |
| 803 | +``` |
| 804 | + |
719 | 805 | ### Reference |
720 | 806 | See https://plotly.com/python/reference/#layout-shapes for more information and chart attribute options! |
0 commit comments