|
| 1 | +# Migration to Plotly 3.0.0 |
| 2 | + |
| 3 | +Plotly 3 introduces enhancements to the plotly.py visualization library and demonstrates some of its features. |
| 4 | + |
| 5 | + |
| 6 | +# What have we Added? |
| 7 | +- Traces can be added and updated interactively by simply assigning to properties |
| 8 | +- The full Traces and Layout API is generated from the plotly schema to provide a great experience for interactive use in the notebook |
| 9 | +- Data validation covering the full API with clear, informative error messages |
| 10 | +- Jupyter friendly docstrings on constructor params and properties |
| 11 | +- Support for setting array properties as numpy arrays. When numpy arrays are used, ipywidgets binary serialization protocol is used to avoid converting these to JSON strings. |
| 12 | +- Context manager API for animation |
| 13 | +- Programmatic export of figures to static SVG images (and PNG and PDF with cairosvg installed). |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +``` |
| 18 | +# Load iris dataset |
| 19 | +iris_data = datasets.load_iris() |
| 20 | +feature_names = [name.replace(' (cm)', '').replace(' ', '_') for name in iris_data.feature_names] |
| 21 | +iris_df = pd.DataFrame(iris_data.data, columns=feature_names) |
| 22 | +iris_class = iris_data.target + 1 |
| 23 | +iris_df.head() |
| 24 | +``` |
| 25 | + |
| 26 | +| | sepal_length | sepal_width | petal_length | petal_width| |
| 27 | +| --- | --- | --- | --- | |
| 28 | +| 0 | 5.1 | 3.5 | 1.4 | 0.2 | |
| 29 | +| 1 | 4.9 | 3.0 | 1.4 | 0.2 | |
| 30 | +| 2 | 4.7 | 3.2 | 1.3 | 0.2 | |
| 31 | +| 3 | 4.6 | 3.1 | 1.5 | 0.2 | |
| 32 | +| 4 | 5.0 | 3.6 | 1.4 | 0.2 | |
| 33 | + |
| 34 | + |
| 35 | +## Create and display an empty FigureWidget |
| 36 | +A FigureWidget behaves almost identically to a Figure but it is also an ipywidget that can be displayed directly in the notebook without calling `iplot` |
| 37 | + |
| 38 | +``` |
| 39 | +f1 = FigureWidget() |
| 40 | +f1 |
| 41 | +``` |
| 42 | + |
| 43 | +# Tab completion |
| 44 | +Entering ``f1.add_<tab>`` displays add methods for all of the supported trace types |
| 45 | + |
| 46 | +Entering ``f1.add_scatter(<tab>)`` displays the names of all of the top-level properties for the scatter trace type |
| 47 | + |
| 48 | +Entering ``f1.add_scatter(<shift+tab>)`` displays the signature pop-up. Expanding this pop-up reveals the method doc string which contains the descriptions of all of the top level properties |
| 49 | + |
| 50 | +``` |
| 51 | +# f1.add_ |
| 52 | +# f1.add_scatter |
| 53 | +``` |
| 54 | + |
| 55 | +# Add scatter trace |
| 56 | +``` |
| 57 | +scatt1 = f1.add_scatter(x=iris_df.sepal_length, y=iris_df.petal_width) |
| 58 | +``` |
| 59 | + |
| 60 | +change the params |
| 61 | +``` |
| 62 | +# Set marker |
| 63 | +scatt1.mode = 'markers' |
| 64 | +scatt1.marker.size = 8 |
| 65 | +scatt1.marker.color = iris_class |
| 66 | +
|
| 67 | +# Change colorscale |
| 68 | +scatt1.marker.cmin = 0.5 |
| 69 | +scatt1.marker.cmax = 3.5 |
| 70 | +scatt1.marker.colorscale = [[0, 'red'], [0.33, 'red'], |
| 71 | + [0.33, 'green'], [0.67, 'green'], |
| 72 | + [0.67, 'blue'], [1.0, 'blue']] |
| 73 | +
|
| 74 | +scatt1.marker.showscale = True |
| 75 | +
|
| 76 | +# Fix up colorscale ticks |
| 77 | +scatt1.marker.colorbar.ticks = 'outside' |
| 78 | +scatt1.marker.colorbar.tickvals = [1, 2, 3] |
| 79 | +scatt1.marker.colorbar.ticktext = iris_data.target_names.tolist() |
| 80 | +
|
| 81 | +
|
| 82 | +# Set colorscale title |
| 83 | +scatt1.marker.colorbar.title = 'Species' |
| 84 | +scatt1.marker.colorbar.titlefont.size = 16 |
| 85 | +scatt1.marker.colorbar.titlefont.family = 'Rockwell' |
| 86 | +
|
| 87 | +# Add axis labels |
| 88 | +f1.layout.xaxis.title = 'sepal_length' |
| 89 | +f1.layout.yaxis.title = 'petal_width' |
| 90 | +
|
| 91 | +# Hover info |
| 92 | +scatt1.text = iris_data.target_names[iris_data.target] |
| 93 | +scatt1.hoverinfo = 'text+x+y' |
| 94 | +f1.layout.hovermode = 'closest' |
| 95 | +
|
| 96 | +f1 |
| 97 | +``` |
| 98 | + |
| 99 | +<iframe src="https://plot.ly/~jordanpeterson/1001.embed" width='100%', height=300></iframe> |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +# What have we Changed? |
| 104 | +- go.Figure() is not a dict |
| 105 | +- widgets in jupyter |
| 106 | +- |
| 107 | + |
| 108 | +``` |
| 109 | +import plotly.graph_objs as go |
| 110 | +scatter = go.Scatter() |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +# What have we Removed? |
| 116 | +We have removed the following methods from the `plotly.graph_objs` plotly objects: |
| 117 | +- `.to_string` |
| 118 | +- `.strip_style` |
| 119 | +- `.get_data` |
| 120 | +- `.validate` |
| 121 | +- `.to_dataframe` |
| 122 | + |
| 123 | +# What is Depreciated? |
| 124 | + |
| 125 | +Plotly Objects form a tree hierarchy. For instance we have `go.Scatter` and the nested attribute `Marker` lives under scatter at `go.Scatter.Marker`. Now params that live a few nodes down the tree under a plotly class must be referenced in the full path. |
| 126 | + |
| 127 | +Example: |
| 128 | +``` |
| 129 | +fig = go.Figure( |
| 130 | + data=[ |
| 131 | + go.Scatter( |
| 132 | + go.Scatter.Marker( |
| 133 | + symbol=0, |
| 134 | + ), |
| 135 | + x=[1,2,3], |
| 136 | + y=[1,2,3], |
| 137 | + ) |
| 138 | + ] |
| 139 | +) |
| 140 | +``` |
| 141 | + |
| 142 | +`go.Data` is depreciated: |
| 143 | + |
| 144 | +Instead of |
| 145 | + |
| 146 | +``` |
| 147 | +go.Data([]) |
| 148 | +``` |
| 149 | + |
| 150 | +drop the go.Data and use a `list` instead: |
| 151 | + |
| 152 | +``` |
| 153 | +[] |
| 154 | +``` |
0 commit comments