|
| 1 | +# Titles and Legends |
| 2 | + |
| 3 | +Deephaven Plotly Express provides ways to customize titles and legends with intuitive default behavior. |
| 4 | + |
| 5 | +## Default Title |
| 6 | + |
| 7 | +The names of the x and y axes are set to the column names when passing single column names in. |
| 8 | + |
| 9 | +```python order=line_plot,my_table |
| 10 | +import deephaven.plot.express as dx |
| 11 | +my_table = dx.data.stocks() |
| 12 | + |
| 13 | +# subset data for just DOG transactions |
| 14 | +dog_prices = my_table.where("Sym = `DOG`") |
| 15 | + |
| 16 | +# x and y axis titles are set to the column names, Timestamp and Price |
| 17 | +line_plot = dx.line(dog_prices, x="Timestamp", y="Price") |
| 18 | +``` |
| 19 | + |
| 20 | +## Plot by Titles and Legend |
| 21 | + |
| 22 | +When using the `by` argument, the legend is automatically generated. |
| 23 | +An entry is created for each unique value in the `by` column. |
| 24 | + |
| 25 | +```python order=line_plot,mytable |
| 26 | +import deephaven.plot.express as dx |
| 27 | +my_table = dx.data.stocks() |
| 28 | + |
| 29 | +# A legend entry is created for each unique value in the Sym column |
| 30 | +line_plot = dx.line(my_table, x="Timestamp", y="Price", by="Sym") |
| 31 | +``` |
| 32 | + |
| 33 | +## Titles and Legend for Multiple Columns |
| 34 | + |
| 35 | +When passing in a list of columns, the axis title for the corresponding axis is set to a new name, `value`. |
| 36 | +As with the `by` argument, the legend is automatically generated. |
| 37 | + |
| 38 | +```python order=line_plot,mytable |
| 39 | +import deephaven.plot.express as dx |
| 40 | +my_table = dx.data.stocks() |
| 41 | + |
| 42 | +# subset data for just DOG transactions |
| 43 | +dog_prices = my_table.where("Sym = `DOG`") |
| 44 | + |
| 45 | +# A legend entry is created for each unique column name in the y list and the y axis title is set to "value" |
| 46 | +line_plot = dx.line(dog_prices, x="Timestamp", y=["Price", "SPet500"]) |
| 47 | +``` |
| 48 | + |
| 49 | +## Title Customization |
| 50 | + |
| 51 | +### Custom Title |
| 52 | + |
| 53 | +Add a title to the plot with the `title` argument. |
| 54 | + |
| 55 | +```python order=line_plot,mytable |
| 56 | +import deephaven.plot.express as dx |
| 57 | +my_table = dx.data.stocks() |
| 58 | + |
| 59 | +# subset data for just DOG transactions |
| 60 | +dog_prices = my_table.where("Sym = `DOG`") |
| 61 | + |
| 62 | +# The plot title is set to "Price of DOG" |
| 63 | +line_plot = dx.line(dog_prices, x="Timestamp", y="Price", title="Price of DOG") |
| 64 | +``` |
| 65 | + |
| 66 | +### Custom Axis Titles |
| 67 | + |
| 68 | +Customize the titles of the x and y axes with the `xaxis_titles` and `yaxis_titles` arguments. |
| 69 | + |
| 70 | +```python order=line_plot,mytable |
| 71 | +import deephaven.plot.express as dx |
| 72 | +my_table = dx.data.stocks() |
| 73 | + |
| 74 | +# subset data for just DOG transactions |
| 75 | +dog_prices = my_table.where("Sym = `DOG`") |
| 76 | + |
| 77 | +# customize the x and y axis titles with xaxis_titles and yaxis_titles |
| 78 | +line_plot = dx.line(dog_prices, x="Timestamp", y="Price", xaxis_titles="Timestamp of Transaction", yaxis_titles="Price of DOG") |
| 79 | +``` |
| 80 | + |
| 81 | +## Legend Customization |
| 82 | + |
| 83 | +Legends are customizable with [unsafe_update_figure](unsafe-update-figure.md). Generally, legend customization is safe. |
| 84 | + |
| 85 | +### Legend Position |
| 86 | + |
| 87 | +By default, the legend is placed in the top right corner of the plot. |
| 88 | +Move the legend around the plot with `x`, `y`, `xanchor`, and `yanchor` arguments to the `update_layout` method. |
| 89 | +`xanchor` and `yanchor` set the anchor point of the legend, which determine which part of the legend box is used to position the legend. |
| 90 | +`x` and `y` set the position of the anchor point relative to the plot area. |
| 91 | +For `x` and `y`, 0 is the left or bottom of the plot area and 1 is the right or top of the plot area. |
| 92 | +Negative values or values above 1 for `x` and `y` move the anchor point outside the plot area. |
| 93 | +`xanchor` can be set to `"auto"`, `"left"`, `"center"`, or `"right"` and `yanchor` can be set to `"auto"`, `"top"`, `"middle"`, or `"bottom"`. |
| 94 | + |
| 95 | +### Legend Overlay |
| 96 | + |
| 97 | +Overlay the legend by positioning the top left corner of the legend just inside the plot area. |
| 98 | + |
| 99 | +```py order=legend_overlay_plot,tips |
| 100 | +import deephaven.plot.express as dx |
| 101 | + |
| 102 | +tips = dx.data.tips() |
| 103 | + |
| 104 | +def update(figure): |
| 105 | + # Update the layout to move the legend to the top left |
| 106 | + # x and y are set to move the top left corner of the legend just inside the plot area |
| 107 | + figure.update_layout( |
| 108 | + legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01) |
| 109 | + ) |
| 110 | + |
| 111 | +legend_overlay_plot = dx.scatter( |
| 112 | + tips, x="TotalBill", y="Tip", color="Day", unsafe_update_figure=update |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +### Horizontal Legend |
| 117 | + |
| 118 | +Customize the legend to be horizontal by updating the layout with `orientation="h"`. |
| 119 | +Move the legend just outside the bottom of the plot area by setting `y` to a negative value. |
| 120 | + |
| 121 | +```python order=legend_horizontal_plot,tips |
| 122 | +import deephaven.plot.express as dx |
| 123 | + |
| 124 | +tips = dx.data.tips() |
| 125 | + |
| 126 | +def update(figure): |
| 127 | + # Update the layout to move the legend to the bottom |
| 128 | + # y is negative to move the legend outside the bottom of the plot area |
| 129 | + # xanchor and x are set to center the legend |
| 130 | + figure.update_layout( |
| 131 | + legend=dict( |
| 132 | + orientation="h", |
| 133 | + yanchor="bottom", |
| 134 | + y=-0.3, |
| 135 | + xanchor="center", |
| 136 | + x=0.5) |
| 137 | + ) |
| 138 | + |
| 139 | +legend_horizontal_plot = dx.scatter( |
| 140 | + tips, x="TotalBill", y="Tip", color="Day", unsafe_update_figure=update |
| 141 | +) |
| 142 | +``` |
| 143 | + |
| 144 | +### Hide Legend |
| 145 | + |
| 146 | +Hide the legend by updating the layout with `showlegend=False`. |
| 147 | + |
| 148 | +```python order=legend_hidden_plot,tips |
| 149 | +import deephaven.plot.express as dx |
| 150 | + |
| 151 | +tips = dx.data.tips() |
| 152 | + |
| 153 | +def update(figure): |
| 154 | + figure.update_layout( |
| 155 | + showlegend=False |
| 156 | + ) |
| 157 | + |
| 158 | +legend_hidden_plot = dx.scatter( |
| 159 | + tips, x="TotalBill", y="Tip", color="Day", unsafe_update_figure=update |
| 160 | +) |
| 161 | +``` |
| 162 | + |
0 commit comments