GeezerVison is showing plotly.com — plotly.io package — 7.1.0 documentation
plotly.io package¶
-
plotly.io.from_json(value, output_type='Figure', skip_invalid=False, engine=None)¶
Construct a figure from a JSON string
- Parameters
value (str or bytes) – String or bytes object containing the JSON representation of a figure
output_type (type or str (default 'Figure')) – The output figure type or type name.
One of: graph_objects.Figure, ‘Figure’, graph_objects.FigureWidget, ‘FigureWidget’
skip_invalid (bool (default False)) – False if invalid figure properties should result in an exception.
True if invalid figure properties should be silently ignored.
engine (str (default None)) –
- The JSON decoding engine to use. One of:
if “json”, parse JSON using built in json module
if “orjson”, parse using the faster orjson module, requires the orjson
package
if “auto” use orjson module if available, otherwise use the json module
If not specified, the default engine is set to the current value of
plotly.io.json.config.default_engine.
- Raises
ValueError – if value is not a string, or if skip_invalid=False and value contains
invalid figure properties
- Returns
-
- Return type
Figure or FigureWidget
-
plotly.io.full_figure_for_development(fig: Union[dict, plotly.graph_objects._figure.Figure], warn: bool = True, as_dict: bool = False) → Union[plotly.graph_objects._figure.Figure, dict]¶
Compute default values for all attributes not specified in the input figure and
returns the output as a “full” figure. This function calls Plotly.js via Kaleido
to populate unspecified attributes. This function is intended for interactive use
during development to learn more about how Plotly.js computes default values and is
not generally necessary or recommended for production use.
- Parameters
fig – Figure object or dict representing a figure
warn (bool) – If False, suppress warnings about not using this in production.
as_dict (bool) – If True, output is a dict with some keys that go.Figure can’t parse.
If False, output is a go.Figure with unparseable keys skipped.
- Returns
The full figure
- Return type
plotly.graph_objects.Figure or dict
-
plotly.io.get_chrome(path: Optional[Union[str, pathlib.Path]] = None) → pathlib.Path¶
Get the path to the Chrome executable for Kaleido.
This function is used by the plotly_get_chrome command line utility.
- Parameters
path (str or Path or None) – The path to the directory where Chrome should be installed.
If None, the default download path will be used.
-
plotly.io.read_json(file, output_type='Figure', skip_invalid=False, engine=None)¶
Construct a figure from the JSON contents of a local file or readable
Python object.
Note: A figure converted to JSON with one version of Plotly.py may not be compatible with another version.
- Parameters
file (str or readable) – A string containing the path to a local file or a read-able Python
object (e.g. a pathlib.Path object or an open file descriptor)
output_type (type or str (default 'Figure')) – The output figure type or type name.
One of: graph_objects.Figure, ‘Figure’, graph_objects.FigureWidget, ‘FigureWidget’
skip_invalid (bool (default False)) – False if invalid figure properties should result in an exception.
True if invalid figure properties should be silently ignored.
engine (str (default None)) –
- The JSON decoding engine to use. One of:
if “json”, parse JSON using built in json module
if “orjson”, parse using the faster orjson module, requires the orjson
package
if “auto” use orjson module if available, otherwise use the json module
If not specified, the default engine is set to the current value of
plotly.io.json.config.default_engine.
- Returns
-
- Return type
Figure or FigureWidget
-
plotly.io.show(fig, renderer=None, validate=True, **kwargs)¶
Show a figure using either the default renderer(s) or the renderer(s)
specified by the renderer argument
- Parameters
fig (dict of Figure) – The Figure object or figure dict to display
renderer (str or None (default None)) – A string containing the names of one or more registered renderers
(separated by ‘+’ characters) or None. If None, then the default
renderers specified in plotly.io.renderers.default are used.
validate (bool (default True)) – True if the figure should be validated before being shown,
False otherwise.
width (int or float) – An integer or float that determines the number of pixels wide the
plot is. The default is set in plotly.js.
height (int or float) – An integer or float specifying the height of the plot in pixels.
The default is set in plotly.js.
config (dict) – A dict of parameters to configure the figure. The defaults are set
in plotly.js.
- Returns
-
- Return type
None
-
plotly.io.to_html(fig, config=None, auto_play=True, include_plotlyjs=True, include_mathjax=False, post_script=None, full_html=True, animation_opts=None, default_width='100%', default_height='100%', validate=True, div_id=None)¶
Convert a figure to an HTML string representation.
- Parameters
fig – Figure object or dict representing a figure
config (dict or None (default None)) – Plotly.js figure config options
auto_play (bool (default=True)) – Whether to automatically start the animation sequence on page load
if the figure contains frames. Has no effect if the figure does not
contain frames.
include_plotlyjs (bool or string (default True)) – Specifies how the plotly.js library is included/loaded in the output
div string.
If True, a script tag containing the plotly.js source code (~3MB)
is included in the output. HTML files generated with this option are
fully self-contained and can be used offline.
If ‘cdn’, a script tag that references the plotly.js CDN is included
in the output. The url used is versioned to match the bundled plotly.js.
HTML files generated with this option are about 3MB smaller than those
generated with include_plotlyjs=True, but they require an active
internet connection in order to load the plotly.js library.
If ‘directory’, a script tag is included that references an external
plotly.min.js bundle that is assumed to reside in the same
directory as the HTML file.
If a string that ends in ‘.js’, a script tag is included that
references the specified path. This approach can be used to point
the resulting HTML file to an alternative CDN or local bundle.
If False, no script tag referencing plotly.js is included. This is
useful when the resulting div string will be placed inside an HTML
document that already loads plotly.js. This option is not advised
when full_html=True as it will result in a non-functional html file.
include_mathjax (bool or string (default False)) – Specifies how the MathJax.js library is included in the output html
div string. MathJax is required in order to display labels
with LaTeX typesetting.
If False, no script tag referencing MathJax.js will be included in the
output.
If ‘cdn’, a script tag that references a MathJax CDN location will be
included in the output. HTML div strings generated with this option
will be able to display LaTeX typesetting as long as internet access
is available.
If a string that ends in ‘.js’, a script tag is included that
references the specified path. This approach can be used to point the
resulting HTML div string to an alternative CDN.
post_script (str or list or None (default None)) – JavaScript snippet(s) to be included in the resulting div just after
plot creation. The string(s) may include ‘{plot_id}’ placeholders
that will then be replaced by the id of the div element that the
plotly.js figure is associated with. One application for this script
is to install custom plotly.js event handlers.
full_html (bool (default True)) – If True, produce a string containing a complete HTML document
starting with an <html> tag. If False, produce a string containing
a single <div> element.
animation_opts (dict or None (default None)) – dict of custom animation parameters to be passed to the function
Plotly.animate in Plotly.js. See
https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
for available options. Has no effect if the figure does not contain
frames, or auto_play is False.
default_width (number or str (default '100%')) – The default figure width/height to use if the provided figure does not
specify its own layout.width/layout.height property. May be
specified in pixels as an integer (e.g. 500), or as a css width style
string (e.g. ‘500px’, ‘100%’).
default_height (number or str (default '100%')) – The default figure width/height to use if the provided figure does not
specify its own layout.width/layout.height property. May be
specified in pixels as an integer (e.g. 500), or as a css width style
string (e.g. ‘500px’, ‘100%’).
validate (bool (default True)) – True if the figure should be validated before being converted to
JSON, False otherwise.
div_id (str (default None)) – If provided, this is the value of the id attribute of the div tag. If None, the
id attribute is a UUID.
- Returns
Representation of figure as an HTML div string
- Return type
str
-
plotly.io.to_image(fig: Union[dict, plotly.graph_objects._figure.Figure], format: Optional[str] = None, width: Optional[int] = None, height: Optional[int] = None, scale: Optional[Union[int, float]] = None, validate: bool = True) → bytes¶
Convert a figure to a static image bytes string
- Parameters
fig – Figure object or dict representing a figure
format (str) –
- (optional) The desired image format. One of:
’png’
’jpg’ or ‘jpeg’
’webp’
’svg’
’pdf’
Defaults to plotly.io.defaults.default_format.
width (int) – (optional) The width of the exported image in layout pixels. If the
scale property is 1.0, this will also be the width of the exported image
in physical pixels. Defaults to plotly.io.defaults.default_width.
height (int) – (optional) The height of the exported image in layout pixels. If the
scale property is 1.0, this will also be the height of the exported image
in physical pixels. Defaults to plotly.io.defaults.default_height.
scale (int or float) – (optional) The scale factor to use when exporting the figure. A scale
factor larger than 1.0 will increase the image resolution with respect
to the figure’s layout pixel dimensions, while a scale factor of
less than 1.0 will decrease the image resolution. Defaults to
plotly.io.defaults.default_scale.
validate (bool) – (optional) True if the figure should be validated before being converted
to an image, False otherwise. Defaults to True.
- Returns
The image data
- Return type
bytes
-
plotly.io.to_json(fig, validate=True, pretty=False, remove_uids=True, engine=None)¶
Convert a figure to a JSON string representation
- Parameters
fig – Figure object or dict representing a figure
validate (bool (default True)) – True if the figure should be validated before being converted to
JSON, False otherwise.
pretty (bool (default False)) – True if JSON representation should be pretty-printed, False if
representation should be as compact as possible.
remove_uids (bool (default True)) – True if trace UIDs should be omitted from the JSON representation
engine (str (default None)) –
- The JSON encoding engine to use. One of:
”json” for an engine based on the built-in Python json module
”orjson” for a faster engine that requires the orjson package
”auto” for the “orjson” engine if available, otherwise “json”
If not specified, the default engine is set to the current value of
plotly.io.json.config.default_engine.
- Returns
Representation of figure as a JSON string
- Return type
str
See also
to_json_plotlyConvert an arbitrary plotly graph_object or Dash component to JSON
-
plotly.io.to_templated(fig, skip=('title', 'text'))¶
Return a copy of a figure where all styling properties have been moved
into the figure’s template. The template property of the resulting figure
may then be used to set the default styling of other figures.
- Parameters
fig – Figure object or dict representing a figure
skip – A collection of names of properties to skip when moving properties to
the template. Defaults to (‘title’, ‘text’) so that the text
of figure titles, axis titles, and annotations does not become part of
the template
Examples
Imports
>>> import plotly.graph_objects as go
>>> import plotly.io as pio
Construct a figure with large courier text
>>> fig = go.Figure(layout={'title': 'Figure Title',
... 'font': {'size': 20, 'family': 'Courier'},
... 'template':"none"})
>>> fig
Figure({
'data': [],
'layout': {'font': {'family': 'Courier', 'size': 20},
'template': '...', 'title': {'text': 'Figure Title'}}
})
Convert to a figure with a template. Note how the ‘font’ properties have
been moved into the template property.
>>> templated_fig = pio.to_templated(fig)
>>> templated_fig.layout.template
layout.Template({
'layout': {'font': {'family': 'Courier', 'size': 20}}
})
>>> templated_fig
Figure({
'data': [], 'layout': {'template': '...', 'title': {'text': 'Figure Title'}}
})
Next create a new figure with this template
>>> fig2 = go.Figure(layout={
... 'title': 'Figure 2 Title',
... 'template': templated_fig.layout.template})
>>> fig2.layout.template
layout.Template({
'layout': {'font': {'family': 'Courier', 'size': 20}}
})
The default font in fig2 will now be size 20 Courier.
Next, register as a named template…
>>> pio.templates['large_courier'] = templated_fig.layout.template
and specify this template by name when constructing a figure.
>>> go.Figure(layout={
... 'title': 'Figure 3 Title',
... 'template': 'large_courier'})
Figure(...)
Finally, set this as the default template to be applied to all new figures
>>> pio.templates.default = 'large_courier'
>>> fig = go.Figure(layout={'title': 'Figure 4 Title'})
>>> fig.layout.template
layout.Template({
'layout': {'font': {'family': 'Courier', 'size': 20}}
})
- Returns
-
- Return type
go.Figure
-
plotly.io.write_html(fig, file, config=None, auto_play=True, include_plotlyjs=True, include_mathjax=False, post_script=None, full_html=True, animation_opts=None, validate=True, default_width='100%', default_height='100%', auto_open=False, div_id=None)¶
Write a figure to an HTML file representation
- Parameters
fig – Figure object or dict representing a figure
file (str or writeable) – A string representing a local file path or a writeable object
(e.g. a pathlib.Path object or an open file descriptor)
config (dict or None (default None)) – Plotly.js figure config options
auto_play (bool (default=True)) – Whether to automatically start the animation sequence on page load
if the figure contains frames. Has no effect if the figure does not
contain frames.
include_plotlyjs (bool or string (default True)) – Specifies how the plotly.js library is included/loaded in the output
div string.
If True, a script tag containing the plotly.js source code (~3MB)
is included in the output. HTML files generated with this option are
fully self-contained and can be used offline.
If ‘cdn’, a script tag that references the plotly.js CDN is included
in the output. The url used is versioned to match the bundled plotly.js.
HTML files generated with this option are about 3MB smaller than those
generated with include_plotlyjs=True, but they require an active
internet connection in order to load the plotly.js library.
If ‘directory’, a script tag is included that references an external
plotly.min.js bundle that is assumed to reside in the same
directory as the HTML file. If file is a string to a local file
path and full_html is True, then the plotly.min.js bundle is copied
into the directory of the resulting HTML file. If a file named
plotly.min.js already exists in the output directory then this file
is left unmodified and no copy is performed. HTML files generated
with this option can be used offline, but they require a copy of
the plotly.min.js bundle in the same directory. This option is
useful when many figures will be saved as HTML files in the same
directory because the plotly.js source code will be included only
once per output directory, rather than once per output file.
If a string that ends in ‘.js’, a script tag is included that
references the specified path. This approach can be used to point
the resulting HTML file to an alternative CDN or local bundle.
If False, no script tag referencing plotly.js is included. This is
useful when the resulting div string will be placed inside an HTML
document that already loads plotly.js. This option is not advised
when full_html=True as it will result in a non-functional html file.
include_mathjax (bool or string (default False)) – Specifies how the MathJax.js library is included in the output html
div string. MathJax is required in order to display labels
with LaTeX typesetting.
If False, no script tag referencing MathJax.js will be included in the
output.
If ‘cdn’, a script tag that references a MathJax CDN location will be
included in the output. HTML div strings generated with this option
will be able to display LaTeX typesetting as long as internet access
is available.
If a string that ends in ‘.js’, a script tag is included that
references the specified path. This approach can be used to point the
resulting HTML div string to an alternative CDN.
post_script (str or list or None (default None)) – JavaScript snippet(s) to be included in the resulting div just after
plot creation. The string(s) may include ‘{plot_id}’ placeholders
that will then be replaced by the id of the div element that the
plotly.js figure is associated with. One application for this script
is to install custom plotly.js event handlers.
full_html (bool (default True)) – If True, produce a string containing a complete HTML document
starting with an <html> tag. If False, produce a string containing
a single <div> element.
animation_opts (dict or None (default None)) – dict of custom animation parameters to be passed to the function
Plotly.animate in Plotly.js. See
https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
for available options. Has no effect if the figure does not contain
frames, or auto_play is False.
default_width (number or str (default '100%')) – The default figure width/height to use if the provided figure does not
specify its own layout.width/layout.height property. May be
specified in pixels as an integer (e.g. 500), or as a css width style
string (e.g. ‘500px’, ‘100%’).
default_height (number or str (default '100%')) – The default figure width/height to use if the provided figure does not
specify its own layout.width/layout.height property. May be
specified in pixels as an integer (e.g. 500), or as a css width style
string (e.g. ‘500px’, ‘100%’).
validate (bool (default True)) – True if the figure should be validated before being converted to
JSON, False otherwise.
auto_open (bool (default True)) – If True, open the saved file in a web browser after saving.
This argument only applies if full_html is True.
div_id (str (default None)) – If provided, this is the value of the id attribute of the div tag. If None, the
id attribute is a UUID.
- Returns
-
- Return type
None
-
plotly.io.write_image(fig: Union[dict, plotly.graph_objects._figure.Figure], file: Union[str, pathlib.Path], format: Optional[str] = None, scale: Optional[Union[int, float]] = None, width: Optional[int] = None, height: Optional[int] = None, validate: bool = True)¶
Convert a figure to a static image and write it to a file or writeable
object
- Parameters
fig – Figure object or dict representing a figure
file (str or writeable) – A string representing a local file path or a writeable object
(e.g. a pathlib.Path object or an open file descriptor)
format (str) –
- (optional) The desired image format. One of:
’png’
’jpg’ or ‘jpeg’
’webp’
’svg’
’pdf’
Defaults to the file extension of file, if given; otherwise
defaults to plotly.io.defaults.default_format.
width (int) – (optional) The width of the exported image in layout pixels. If the
scale property is 1.0, this will also be the width of the exported image
in physical pixels. Defaults to plotly.io.defaults.default_width.
height (int) – (optional) The height of the exported image in layout pixels. If the
scale property is 1.0, this will also be the height of the exported image
in physical pixels. Defaults to plotly.io.defaults.default_height.
scale (int or float) – (optional) The scale factor to use when exporting the figure. A scale
factor larger than 1.0 will increase the image resolution with respect
to the figure’s layout pixel dimensions, while a scale factor of
less than 1.0 will decrease the image resolution. Defaults to
plotly.io.defaults.default_scale.
validate (bool) – (optional) True if the figure should be validated before being converted
to an image, False otherwise. Defaults to True.
- Returns
-
- Return type
None
-
plotly.io.write_images(fig: Union[List[Union[dict, plotly.graph_objects._figure.Figure]], dict, plotly.graph_objects._figure.Figure], file: Union[List[Union[str, pathlib.Path]], str, pathlib.Path], format: Optional[Union[List[Optional[str]], str]] = None, scale: Optional[Union[List[Optional[Union[int, float]]], int, float]] = None, width: Optional[Union[List[Optional[int]], int]] = None, height: Optional[Union[List[Optional[int]], int]] = None, validate: Union[List[bool], bool] = True) → None¶
Write multiple images to files or writeable objects. This is much faster than
calling write_image() multiple times. This function can only be used with the Kaleido
engine, v1.0.0 or greater.
This function accepts the same arguments as write_image() (minus the engine argument),
except that any of the arguments may be either a single value or an iterable of values.
If multiple arguments are iterable, they must all have the same length.
- Parameters
fig – List of figure objects or dicts representing a figure.
Also accepts a single figure or dict representing a figure.
file (str, pathlib.Path, or list of (str or pathlib.Path)) – List of str or pathlib.Path objects representing local file paths to write to.
Can also be a single str or pathlib.Path object if fig argument is
a single figure or dict representing a figure.
format (str or list of (str or None)) –
- (optional) The image format to use for exported images. One of:
’png’
’jpg’ or ‘jpeg’
’webp’
’svg’
’pdf’
Defaults to the file extension of file, if given; otherwise
defaults to plotly.io.defaults.default_format.
Pass a list to specify formats for each figure in the list passed to fig.
Pass a str to apply the same format to all exported images.
width (int or list of (int or None)) – (optional) The width of the exported image in layout pixels. If the scale
property is 1.0, this will also be the width of the exported image
in physical pixels. Defaults to plotly.io.defaults.default_width.
Pass a list to specify widths for each figure in the list passed to fig.
Pass an int to apply the same width to all exported images.
height (int or list of (int or None)) – (optional) The height of the exported image in layout pixels. If the scale
property is 1.0, this will also be the height of the exported image
in physical pixels. Defaults to plotly.io.defaults.default_height.
Pass a list to specify heights for each figure in the list passed to fig.
Pass an int to apply the same height to all exported images.
scale (int, float, or list of (int, float, or None)) – (optional) The scale factor to use when exporting the figure. A scale factor
larger than 1.0 will increase the image resolution with respect
to the figure’s layout pixel dimensions, while a scale factor of
less than 1.0 will decrease the image resolution. Defaults to
plotly.io.defaults.default_scale.
Pass a list to specify scale for each figure in the list passed to fig.
Pass an int or float to apply the same scale to all exported images.
validate (bool or list of bool) – (optional) True if the figure should be validated before being converted
to an image, False otherwise. Defaults to True.
Pass a list to specify validation for each figure in the list passed to fig.
Pass a boolean to apply the same validation setting to all figures.
- Returns
-
- Return type
None
-
plotly.io.write_json(fig, file, validate=True, pretty=False, remove_uids=True, engine=None)¶
Convert a figure to JSON and write it to a file or writeable
object.
Note: A figure converted to JSON with one version of Plotly.py may not be compatible with another version.
- Parameters
fig – Figure object or dict representing a figure
file (str or writeable) – A string representing a local file path or a writeable object
(e.g. a pathlib.Path object or an open file descriptor)
pretty (bool (default False)) – True if JSON representation should be pretty-printed, False if
representation should be as compact as possible.
remove_uids (bool (default True)) – True if trace UIDs should be omitted from the JSON representation
engine (str (default None)) –
- The JSON encoding engine to use. One of:
”json” for an engine based on the built-in Python json module
”orjson” for a faster engine that requires the orjson package
”auto” for the “orjson” engine if available, otherwise “json”
If not specified, the default engine is set to the current value of
plotly.io.json.config.default_engine.
- Returns
-
- Return type
None
plotly.io.base_renderers module¶
plotly.io.kaleido module¶
plotly.io.sg_scraper module¶
|