Skip to content

Commit d56abff

Browse files
committed
add examples for new autorangeoptions and
1 parent d79add7 commit d56abff

File tree

1 file changed

+126
-20
lines changed

1 file changed

+126
-20
lines changed

doc/python/axes.md

Lines changed: 126 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ fig.show()
544544

545545
#### Setting the Range of Axes Manually
546546

547-
The visible x and y axis range can be configured manually by setting the `range` axis property to a list of two values, the lower and upper boundary.
547+
The visible x and y axis range can be configured manually by setting the `range` axis property to a list of two values, the lower and upper bound.
548548

549549
Here's an example of manually specifying the x and y axis range for a faceted scatter plot created with Plotly Express.
550550

@@ -559,6 +559,41 @@ fig.update_yaxes(range=[3, 9])
559559
fig.show()
560560
```
561561

562+
#### Setting only a Lower or Upper Bound for Range
563+
564+
*New in 5.16*
565+
566+
You can also set just a lower or upper bound manually and have autorange applied to the other bound by setting it to `None`. In the following example, we set a an upper bound of 4.5 on the x axes, while specifying `None` for the lower bound, meaning it will use autorange. On the y axes, we set the lower bound, and use `None` for the upper bound, meaning that uses autorange.
567+
568+
```python
569+
import plotly.express as px
570+
df = px.data.iris()
571+
572+
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
573+
fig.update_xaxes(range=[None, 4.5])
574+
fig.update_yaxes(range=[3, None])
575+
576+
fig.show()
577+
```
578+
579+
#### Setting a Maximum and Minimum Allowed Axis Value
580+
581+
*New in 5.16*
582+
583+
When setting a range manually, you can also set a `maxallowed` or `minallowed` for an axis. With this set, you won't be able to pan further than the min or max allowed. In this example, we've set the minimum allowed on the x-axis to 1 and the maximum allowed on the y-axis to 10.
584+
585+
```python
586+
import plotly.express as px
587+
588+
df = px.data.iris()
589+
590+
fig = px.scatter(df, x="sepal_width", y="sepal_length")
591+
fig.update_xaxes(range=[1.5, 4.5], minallowed=1)
592+
fig.update_yaxes(range=[3, 9], maxallowed=10)
593+
594+
fig.show()
595+
```
596+
562597
#### Disabling Pan/Zoom on Axes (Fixed Range)
563598

564599
Pan/Zoom can be disabled for a given axis by setting `fixedrange` to `True`.
@@ -693,6 +728,22 @@ fig.update_yaxes(range=[9, 3])
693728
fig.show()
694729
```
695730

731+
*New in 5.16*
732+
733+
To use a reversed axis while specifying only an upper or lower bound for the range, set `autorange="reversed"`:
734+
735+
```python
736+
import plotly.express as px
737+
df = px.data.iris()
738+
739+
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
740+
fig.update_yaxes(range=[9, None],
741+
autorange="reversed"
742+
)
743+
744+
fig.show()
745+
```
746+
696747
### Axis range for log axis type
697748

698749
If you are using a `log` type of axis and you want to set the range of the axis, you have to give the `log10` value of the bounds when using `fig.update_xaxes` or `fig.update_layout`. However, with `plotly.express` functions you pass directly the values of the range bounds (`plotly.express` then computes the appropriate values to pass to the figure layout).
@@ -717,25 +768,6 @@ fig.update_yaxes(type="log")
717768
fig.show()
718769
```
719770

720-
#### <code>nonnegative</code>, <code>tozero</code>, and <code>normal</code> Rangemode
721-
722-
The axis auto-range calculation logic can be configured using the `rangemode` axis parameter.
723-
724-
If `rangemode` is `"normal"` (the default), the range is computed based on the min and max values of the input data. If `"tozero"`, the range will always include zero. If `"nonnegative"`, the range will not extend below zero, regardless of the input data.
725-
726-
Here is an example of configuring a faceted scatter plot created using Plotly Express to always include zero for both the x and y axes.
727-
728-
```python
729-
import plotly.express as px
730-
df = px.data.iris()
731-
732-
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
733-
fig.update_xaxes(rangemode="tozero")
734-
fig.update_yaxes(rangemode="tozero")
735-
736-
fig.show()
737-
```
738-
739771
#### Setting the ___domain of the axis
740772

741773
```python
@@ -773,6 +805,80 @@ fig.update_xaxes(matches='x')
773805
fig.show()
774806
```
775807

808+
#### <code>nonnegative</code>, <code>tozero</code>, and <code>normal</code> Rangemode
809+
810+
When you don't specify a range, autorange is used. It's also used for bounds set to `None` when providing a `range`.
811+
812+
The axis auto-range calculation logic can be configured using the `rangemode` axis parameter.
813+
814+
If `rangemode` is `"normal"` (the default), the range is computed based on the min and max values of the input data. If `"tozero"`, the range will always include zero. If `"nonnegative"`, the range will not extend below zero, regardless of the input data.
815+
816+
Here is an example of configuring a faceted scatter plot created using Plotly Express to always include zero for both the x and y axes.
817+
818+
```python
819+
import plotly.express as px
820+
df = px.data.iris()
821+
822+
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
823+
fig.update_xaxes(rangemode="tozero")
824+
fig.update_yaxes(rangemode="tozero")
825+
826+
fig.show()
827+
```
828+
829+
#### Autorange Options
830+
831+
*New in 5.16*
832+
833+
You can further configure how autorange is applied using `autorangeoptions` to specify maximum or minimum values or values to include.
834+
835+
##### Specifying Minimum and Maximum Allowed Values
836+
837+
Using `autorangeoptions.maxallowed`, you can specify an exact value to use as the autorange maximum. With `autorangeoptions.minallowed`, you can specify an exact value to use as the autorange minimum.
838+
839+
```python
840+
import plotly.express as px
841+
842+
df = px.data.iris()
843+
844+
fig = px.scatter(df, x="sepal_width", y="sepal_length")
845+
fig.update_yaxes(autorangeoptions=dict(minallowed=3))
846+
fig.update_xaxes(autorangeoptions=dict(maxallowed=5))
847+
848+
fig.show()
849+
```
850+
851+
##### Clip Minimum and Maximum
852+
853+
You can also clip an axis range at a specific maximum or minimum value with `autorangeoptions.clipmax` and `autorangeoptions.clipmin`.
854+
855+
```python
856+
import plotly.express as px
857+
858+
df = px.data.iris()
859+
860+
fig = px.scatter(df, x="sepal_width", y="sepal_length")
861+
fig.update_yaxes(autorangeoptions=dict(clipmin=5))
862+
fig.update_xaxes(autorangeoptions=dict(clipmax=4))
863+
864+
fig.show()
865+
```
866+
867+
##### Specify Values to be Included
868+
869+
Use `autorangeoptions.include` to specify a value that should always be included within the calculated autorange. In this example, we specify that for the autorange calculated on the x-axis, 5 should be included.
870+
871+
```python
872+
import plotly.express as px
873+
874+
df = px.data.iris()
875+
876+
fig = px.scatter(df, x="sepal_width", y="sepal_length")
877+
fig.update_xaxes(autorangeoptions=dict(include=5))
878+
879+
fig.show()
880+
```
881+
776882
#### Reference
777883

778884
See https://plotly.com/python/reference/layout/xaxis/ and https://plotly.com/python/reference/layout/yaxis/ for more information and chart attribute options!

0 commit comments

Comments
 (0)