plotly

Plotly.Io.Show()

When working with Plotly, you will often come across the fig.show() function. It is a very useful function that is used with the Plotly library to display the figures.

Without much further delay, let’s dive in the discuss on how this function can help us display the Plotly figures.

Plotly.Io.Show()

The function takes a syntax as shown in the following:

plotly.io.show(fig, renderer=None, validate=True, width, height, config, **kwargs)

The function parameters are as follows:

  1. Fig – Refers to the figure or object to display.
  2. renderer – Defines a name of a registered renderer. You can specify more than one renderer using the + operator. If no renderer is specified, the function defaults to the one defined in the plotly.renderes.default.
  3. validate – Determines if the figure should be validated first before display. Defaults to true.
  4. width – Specifies the width in pixels used for the figure.
  5. height – Determines the height in pixels used to plot the figure.
  6. config – Represents a dictionary of parameters used to configure the figure.

The function returns a None value but displays the figure using the specified renderer.

Displaying a Figure

To display a Plotly figure using various renderers, we can use the various methods as discussed in the following sections:

import plotly.express as px
df = px.data.stocks()
fig = px.line(df,x="date", y="AMZN")

The given code starts by importing the Plotly express module. Next, we plot a simple line graph of the stocks for Amazon.
To display the figure, we can use the show function as shown in the following:

fig.show()

The previous function displays the figure in the working environment as shown in the following:

In most cases, we can omit the show() function and allow the figure to display itself as follows:

fig

Although the renderer framework is flexible, calling the fig without the show() function will only work if the last expression evaluates to a figure. The plotly.py file must be running from an IPython Kernel.

If you are looking to pass the parameters such as width and height, you can pass the figure to the io.show() function as shown in the following:

import plotly.io as io
io.show(fig=fig, width=1200, height=800)

In this case, we use the io.show() function with the specific parameters such as width and height.

The resulting figure is as follows:

NOTE: The previous image is not to scale and does not represent the accurate image dimensions.

Specifying the Default Renderer

The default set renderer is responsible for handling the display of the plotted figures in Plotly. However, we can customize the renderer by editing the plotly.io.renderers object.

Keep in mind that the renderer is automatically chosen in popular environments such as Jupyter Notebooks, Jupyter Lab, Visual Studio Code Notebooks, Google Collab, Kaggle, and IPython shell.

To show the default renderers, run the following command:

import plotly.io as io
io.renderers

The previous code returns the default and available renderers as shown in the following:

Keep in mind that the default renderer depends on the environment in which you are running the code.

To set the default renderer, we can run the following code:

import plotly.io as io
io.renderers.default = "browser"

The previous code replaces the default renderer with the browser.

Although customizing the renderer can be helpful in custom environments, sticking with the default can be beneficial. Plotly chooses the best one for that scenario.

Conclusion

Using this article, you learned the various ways of configuring how Plotly displays the various figures. The plotly.io.show() function is a great tool when you need to customize the Plotly renderers.

Thanks for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list