How to save a Plotly graph in a ipynb file? - python

I understand that it is possible to export a Plotly graph, and that I can display it.
While sharing notebooks, the matplotlib plots remain intact in the Jupyter Notebooks, however, the Plotly graphs do not. They simply disappear
I understand that the Plotly graph is browser rendered, but is there any way I can store the graph in the ipynb file when I export it?
Is there any way that I can display the Plotly graph, just like the matplotlib graph?
Edit: As suggested in an answer, I tried to save it to a figure object, and display that, but no luck there either :/

If you put the code for a series of graphs in a single cell, execute it and save it, I think it will be displayed the next time you open it.
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()

Related

Update plotly chart in jupyter notebook

I'm creating a plotly chart in jupyter notebook. Because I'm testing some algorithm I want to add data after the initial fig2.show(). But when I update the data and call fig2.show again a new chart is being rendered. How can I update the chart instead of creating a new chart?
This should be an easy task - but it's way to hard to find for me in the documentation.
import plotly.offline as pyo
import plotly.graph_objs as go
import numpy as np
# Set notebook mode to work in offline
pyo.init_notebook_mode()
fig2 = go.Figure(data=go.Scatter(x=moreX2, y=moreY2))
fig2.show()
Then I'm using update:
moreX2.append(2)
moreY2.append(5)
fig2.data[0].update({"x": moreX2, "y": moreY2})
The chart isn't being rerendered and I tried to call fig.show() again, which just creates a new chart.
This answer worked for me, and seems like the ideal answer to this question as well. I've voted to mark this as a duplicate.
To summarize: by wrapping the Figure in a FigureWidget, we can now do exactly what the OP wanted. Although on Colab I did have to give some extra permission by calling the following:
from google.colab import output
output.enable_custom_widget_manager()

Alternatives of saving an interactive figure in plotly other than Interactive HTML Export

I stumbled upon a need where I need to save an interactive figure which is plotted in Python by using plotly.
After hours of googling and whatnot, I found 2 solutions given in the plotly documentation to save a figure:
1. Static Image Export
2. Interactive HTML Export in Python
Interactive HTML Export fulfilled my need but left me wondering can't I use other interactive image formats i.e gif etc.
Please use the below code to find ways of saving this figure other than .html.
df = px.data.gapminder()
fig = px.scatter_geo(df, locations="iso_alpha", color="continent",
hover_name="country", size="pop",
animation_frame="year",
projection="natural earth")
fig.show() # Used for showing output in jupyter notebook
fig.write_html('output.html') # actually used for exporting as an .html file
Thank you in advance.

Writing Data Point Name Just Above the Data Point in Python Plotly

I have a dataset and I plotted using plotly python
The name of the models is shown as a legend on the right side which makes it a little difficult to interpret. Is there any way to write the model name just above the color circle represent on the graph similar to what shown below? Something similar in mathplotlib will also help me.
You need to add labels in you scatter statement.
import plotly.express as px
#df data
fig = px.scatter(df, x="latency", y="AP", text="type", size_max=60)
fig.update_traces(textposition='top center')

jupyter notebook: show seaborn plot again

Trying to build seaborn FacetGrid plots in a Jupyter notebook. Upon creation, it displays just fine. But if I manipulate the chart and want to see it again, I cannot. I can only get the object listing.
How can I show the chart a second time?
You need to state the figure in a new cell to let it be displayed with the inline backend.
g.fig
g.fig doesn't work.Instead, g.figure works out.

How do you get the range of the x-axis of a Plotly graph?

I'm trying to retrieve the range of the x-axis when you zoom in a plotly scatter plot, but when I tried to access it using:
figure['layout']['xaxis']['range']
it just returns 'None'.
This is the code that I am using to create the graph:
# self.plot contains the dataframe passed to the function to be plotted
self.plot = df
# Creates the plotly plot figure
self.fig = self.plot.iplot(asFigure=True,kind='scatter', xTitle='Date', yTitle='Temperature')
# Displays the plot
iplot(self.fig, show_link=False)
I'm using cufflinks to create the plot from a pandas DataFrame so I don't explicitly set the layout.xaxis.range to anything. Also I'm using a Jupyter Notebook to display the graph, if that helps at all. So is there any way of getting the range of the x-axis of the current view window of the plot? Thanks in advance!
That does not seem to be possible at the moment using python. There's a post on community.plot.ly that says:
Graph parameters don’t dynamically change with chart actions. However,
as you zoom, the plot does emit data regarding the new x-axis range,
you’re unable to to access this information directly in Python.
Instead you’d have to use javascript:
https://plot.ly/javascript/zoom-events/

Categories