Skip to content

Commit 5bb7b49

Browse files
add full_figure docs and test
1 parent 732be17 commit 5bb7b49

File tree

4 files changed

+183
-2
lines changed

4 files changed

+183
-2
lines changed

doc/python/figure-introspection.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.2'
9+
jupytext_version: 1.4.2
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.7.7
24+
plotly:
25+
description: How to dig into and learn more about the figure data structure.
26+
display_as: file_settings
27+
language: python
28+
layout: base
29+
name: Introspecting Figures
30+
order: 35
31+
page_type: u-guide
32+
permalink: python/figure-introspection/
33+
thumbnail: thumbnail/violin.jpg
34+
---
35+
36+
### The Figure Lifecycle
37+
38+
As explained in the [Figure Data Structure documentation](/python/figure-structure/), when building a figure object with Plotly.py, it is not necessary to populate every possible attribute. At render-time, figure objects (whether generated via [Plotly Express](/python/plotly-express/) or [Graph Objects](/python/graph-objects/) are passed from Plotly.py to [Plotly.js](/javascript/), which is the Javascript library responsible for turning JSON descriptions of figures into graphical representations.
39+
40+
As part of this rendering process, Plotly.js will determine, based on the attributes that have been set, which other attributes require values in order to draw the figure. Plotly.js will then apply either static or dynamic defaults to all of the remaining required attributes and render the figure. A good example of a static default would be the text font size: if unspecified, the default value is always the same. A good example of a dynamic default would be the range of an axis: if unspecified, the default will be computed based on the range of the data in traces associated with that axis.
41+
42+
43+
### Introspecting Plotly Express Figures
44+
45+
Figure objects created by [Plotly Express](/python/plotly-express/) have a number of attributes automatically set, and these can be introspected using the Python `print()` function, or in JupyterLab, the special `fig.show("json")` renderer, which gives an interactive drilldown interface with search:
46+
47+
```python
48+
import plotly.express as px
49+
50+
fig = px.scatter(x=[10, 20], y=[20, 10], height=400, width=400)
51+
fig.show()
52+
print(fig)
53+
```
54+
55+
We can learn more about the attributes Plotly Express has set for us with the Python `help()` function:
56+
57+
```python
58+
help(fig.data[0].__class__.mode)
59+
```
60+
61+
### Accessing Javascript-Computed Defaults
62+
63+
_new in 4.10_
64+
65+
The `.full_figure_for_development()` method provides Python-level access to the default values computed by Plotly.js. This method requires [the Kaleido package](/python/static-image-export/), which is easy to install and also used for [static image export](/python/static-image-export/).
66+
67+
By way of example, here is an extremely simple figure created with [Graph Objects](/python/graph-objects/) (although it could have been made with [Plotly Express](/python/plotly-express/) as well just like above) where we have disabled the default template for maximum readability. Note how in this figure the text labels on the markers are clipped, and sit on top of the markers.
68+
69+
```python
70+
import plotly.graph_objects as go
71+
72+
fig = go.Figure(
73+
data=[go.Scatter(
74+
mode="markers+text",
75+
x=[10,20],
76+
y=[20, 10],
77+
text=["Point A", "Point B"]
78+
)],
79+
layout=dict(height=400, width=400, template="none")
80+
)
81+
fig.show()
82+
```
83+
84+
Let's print this figure to see the very small JSON object that is passed to Plotly.js as input:
85+
86+
```python
87+
print(fig)
88+
```
89+
90+
Now let's look at the "full" figure after Plotly.js has computed the default values for every necessary attribute.
91+
92+
> Heads-up: the full figure is quite long and intimidating, and this page is meant to help demystify things so **please read on**!
93+
94+
Please also note that the `.full_figure_for_development()` function is really meant for interactive learning and debugging, rather than production use, hence its name and the warning it produces by default, which you can see below, and which can be supressed with `warn=False`.
95+
96+
```python
97+
full_fig = fig.full_figure_for_development()
98+
print(full_fig)
99+
```
100+
101+
As you can see, Plotly.js does a lot of work filling things in for us! Let's look at the examples described at the top of the page of static and dynamic defaults. If we look just at `layout.font` and `layout.xaxis.range` we can see that the static default font size is 12 and that the dynamic default range is computed to be a bit beyond the data range which was 10-20:
102+
103+
```python
104+
print("full_fig.layout.font.size: ", full_fig.layout.font.size)
105+
print("full_fig.layout.xaxis.range: ", full_fig.layout.xaxis.range)
106+
```
107+
108+
### Learning About Attributes
109+
110+
111+
What else can we use this `full_fig` for? Let's start by looking at the first entry of the `data`
112+
113+
```python
114+
print(full_fig.data[0])
115+
```
116+
117+
We see that this is an instance of `go.Scatter` (as expected, given the input) and that it has an attribute we've maybe never heard of called `cliponaxis` which by default seems to be set to `True` in this case. Let's find out more about this attribute using the built-in Python `help()` function
118+
119+
```python
120+
help(go.Scatter.cliponaxis)
121+
```
122+
123+
Aha! This explains why in our original figure above, the text was cut off by the edge of the plotting area! Let's try forcing that to `False`, and let's also use the attribute `textposition` which we see in the full figure is by default set to `"middle center"` to get our text off of our markers:
124+
125+
```python
126+
fig.update_traces(cliponaxis=False, textposition="top right")
127+
fig.show()
128+
```
129+
130+
We can use this technique (of making a figure, and querying Plotly.js for the "full" version of that figure, and then exploring the attributes that are automatically set for us) to learn more about the range of possibilities that the figure schema makes available. We can drill down into `layout` attributes also:
131+
132+
```python
133+
help(go.layout.XAxis.autorange)
134+
```
135+
136+
### More about Layout
137+
138+
In the figure we introspected above, we had added [a `scatter` trace](/python/line-and-scatter/), and Plotly.js automatically filled in for us the `xaxis` and `yaxis` values of that trace object to be `x` and `y`, and then also filled out the corresponding `layout.xaxis` and `layout.yaxis` objects for us, complete with their [extensive set of defaults for gridlines, tick labels and so on](/python/axes/).
139+
140+
If we create a figure with [a `scattergeo` trace](/python/scatter-plots-on-maps/) instead, however, Plotly.js will fill in a totally different set of objects in `layout`, corresponding to [a `geo` subplot, with all of its defaults for whether or not to show rivers, lakes, country borders, coastlines etc](https://plotly.com/python/map-configuration/).
141+
142+
```python
143+
import plotly.graph_objects as go
144+
145+
fig = go.Figure(
146+
data=[go.Scattergeo(
147+
mode="markers+text",
148+
lat=[10, 20],
149+
lon=[20, 10],
150+
text=["Point A", "Point B"]
151+
)],
152+
layout=dict(height=400, width=400,
153+
margin=dict(l=0,r=0,b=0,t=0),
154+
template="none")
155+
)
156+
fig.show()
157+
full_fig = fig.full_figure_for_development()
158+
print(full_fig)
159+
```
160+
161+
If I then set `showrivers=True` and re-query the full figure, I see that new keys have appeared in the `layout.geo` object for `rivercolor` and `riverwidth`, showing the dynamic nature of these defaults.
162+
163+
```python
164+
fig.update_geos(showrivers=True)
165+
full_fig = fig.full_figure_for_development()
166+
print(full_fig.layout.geo)
167+
```
168+
169+
### Reference
170+
171+
You can learn more about [all the available attributes in the plotly figure schema](/python/reference/) (and read about its [high-level structure](/python/figure-structure/)) or about [all the classes and functions in the `plotly` module](/python-api-reference/).
172+
173+
```python
174+
175+
```

doc/python/figure-structure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Attributes are referred to in text and in the [Figure Reference](/python/referen
5757

5858
The [`plotly.graph_objects` module contains an automatically-generated hierarchy of Python classes](/python/graph-objects/) which represent non-leaf attributes in the figure schema and provide a Pythonic API for them. When [manipulating a `plotly.graph_objects.Figure` object](/python/creating-and-updating-figures/), attributes can be set either directly using Python object attributes e.g. `fig.layout.title.font.family="Open Sans"` or using [update methods and "magic underscores"](/python/creating-and-updating-figures/#magic-underscore-notation) e.g. `fig.update_layout(title_font_family="Open Sans")`
5959

60-
When building a figure, it is *not necessary to populate every attribute* of every object. At render-time, the JavaScript layer will compute default values for each required unspecified attribute, depending upon the ones that are specified, as documented in the [Figure Reference](/python/reference/). An example of this would be `layout.xaxis.range`, which may be specified explicitly, but if not will be computed based on the range of `x` values for every trace linked to that axis. The JavaScript layer will ignore unknown attributes or malformed values, although the `plotly.graph_objects` module provides Python-side validation for attribute values. Note also that if [the `layout.template` key is present (as it is by default)](/python/templates/) then default values will be drawn first from the contents of the template and only if missing from there will the JavaScript layer infer further defaults. The built-in template can be disabled by setting `layout.template="none"`.
60+
When building a figure, it is *not necessary to populate every attribute* of every object. At render-time, [the JavaScript layer will compute default values](/python/figure-introspection/) for each required unspecified attribute, depending upon the ones that are specified, as documented in the [Figure Reference](/python/reference/). An example of this would be `layout.xaxis.range`, which may be specified explicitly, but if not will be computed based on the range of `x` values for every trace linked to that axis. The JavaScript layer will ignore unknown attributes or malformed values, although the `plotly.graph_objects` module provides Python-side validation for attribute values. Note also that if [the `layout.template` key is present (as it is by default)](/python/templates/) then default values will be drawn first from the contents of the template and only if missing from there will the JavaScript layer infer further defaults. The built-in template can be disabled by setting `layout.template="none"`.
6161

6262
### The Top-Level `data` Attribute
6363

packages/python/plotly/plotly/io/_kaleido.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def full_figure_for_development(fig, warn=True, as_dict=False):
291291
if scope is None:
292292
raise ValueError(
293293
"""
294-
Image export using the "kaleido" engine requires the kaleido package,
294+
Full figure generation requires the kaleido package,
295295
which can be installed using pip:
296296
$ pip install -U kaleido
297297
"""

packages/python/plotly/plotly/tests/test_optional/test_kaleido/test_kaleido.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def test_kaleido_engine_to_image_returns_bytes():
2828
assert result.startswith(b"<svg")
2929

3030

31+
def test_kaleido_fulljson():
32+
empty_fig = dict(data=[], layout={})
33+
result = pio.full_figure_for_development(empty_fig, warn=False, as_dict=True)
34+
assert result["layout"]["calendar"] == "gregorian"
35+
36+
3137
def test_kaleido_engine_to_image():
3238
with mocked_scope() as scope:
3339
pio.to_image(fig, engine="kaleido", validate=False)

0 commit comments

Comments
 (0)