Matplotlib to plotly offline - python

I have some matplotlib graphs that need to be viewed offline in a browser, I was using MPLD3 to render them before, but given the need to view the plots without an internet connection, I'm considering using plotly. Is there a way to view matplotlib plotly graphs offline?

A minimal example of converting a matplotlib figure to plotly would look like this.
import matplotlib.pyplot as plt
import plotly
import plotly.plotly as py
import plotly.tools as tls
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9], "o")
plotly_fig = tls.mpl_to_plotly(fig)
plotly.offline.plot(plotly_fig, filename="plotly version of an mpl figure")
Just posting this as the documentation was somewhat hard to follow.

import plotly.tools as tls
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
x = np.random.random(100) ### toy data
y = np.random.random(100) ### toy data
## matplotlib fig
fig, axes = plt.subplots(2,1, figsize = (10,6))
axes[0].plot(x, label = 'x')
axes[1].scatter(x,y)
## convert and plot in plotly
plotly_fig = tls.mpl_to_plotly(fig) ## convert
iplot(plotly_fig)

How about this page at Section Offline Use
BTW: You can also write a static image file as described here
import plotly.io as pio
import plotly.graph_objs as go
fig = go.Figure()
# Do some fig.add_scatter() stuff here
pio.write_image(fig, 'fig1.png')

Related

Python Why does my chart disappear when using matplotlip.pyplot.figure(figsize)

When rendering matplotlib charts using pyscript and using figure(figsize) the chart disappears and replaces the x and y axes. Why is this happening?
With figsize
Without figsize
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pyodide.http import open_url
url = open_url('../data/salaries.csv')
df = pd.read_csv(url)
df_no_outliers=df[(df.salary_in_usd>min) & (df.salary_in_usd<max)]
top_jobs=df_no_outliers.job_title.value_counts()[:7]
plt.figure(2)
barplot = sns.barplot(x='work_year', y='salary_in_usd', data=df_no_outliers)
plt.tight_layout()
plt.figure(figsize=(15,8))
plt

How to Change x-axis to logarithmic in PLOTLY histogram

How to make x-axis of the following histogram, logarithmic?
The following code:
data_list = [1,1,5,5,5,100,100]
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Histogram(x=data_list, nbinsx=100))
import plotly.offline as py
py.init_notebook_mode(connected=False)
py.offline.plot(fig, filename = 'test.html')
Produces typical histogram.
How can I change the x-axis logarithmic?
I also tried:
data_list = [1,1,5,5,5,100,100]
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Histogram(x=data_list, nbinsx=100))
fig.update_layout(xaxis_type="log")
fig.update_xaxes(tick0=0, dtick=1, range=[0,2.5])
import plotly.offline as py
py.init_notebook_mode(connected=False)
py.offline.plot(fig, filename = 'test.html')
but this will result in non-logarithmic bins, and essentially, the bin at 100 disappears!
As said above in the comments, logarithmic axes are not possible with plotly. You could try to create bins using numpy and create a bar plot with those.

How to get the color of a seaborn/matplotlib bar graph

I am building a seaborn graphics with this code :
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
%matplotlib inline
df=pd.DataFrame(data=['a','b','c','d','a','a','b','c','a','a'],columns['datos'])
tabla=df['datos'].value_counts().reset_index()
fig, ax1 = plt.subplots(figsize=(6,4))
sns.set()
sns.barplot(x='index', y='datos', data=tabla, ax=ax1)
It does work properly....but How could I get the code of the colors used for every bar?
I have tried with :
f.get_color()
ax.get_color()
but no success at all...
thanks in advance
waly
I admit it's an ugly way, but you will need to access the children of your plot. Note that this probably won't work if you plot more than just the countplot, since you will get all used Rectangle colors in your plot.
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import pandas as pd
import seaborn as sns
%matplotlib inline
sns.set()
df=pd.DataFrame(data=['a','b','c','d','a','a','b','c','a','a'],columns=['datos'])
tabla=df['datos'].value_counts().reset_index()
fig, ax1 = plt.subplots(figsize=(6,4))
sns.barplot(x='index', y='datos', data=tabla, ax=ax1)
bars = [r for r in ax1.get_children() if type(r)==Rectangle]
colors = [c.get_facecolor() for c in bars[:-1]] # I think the last Rectangle is the background.

Python Plotly heatmap subplots - remove internal y-axis numbers and ticks

How do I remove the numbers and ticks from the internal y-axis in the Plot.ly heatmap subplot graph below? Both plots share the same y-axis range so there is no reason to show both.
import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools
import pandas as pd
import numpy as np
dfl = []
dfl.append(pd.DataFrame(np.random.random((100,100,))))
dfl.append(pd.DataFrame(np.random.random((100,100,))))
fig = tools.make_subplots(rows=1, cols=len(dfl) ,print_grid=False);
for index, a in enumerate(dfl):
sn = str(index)
data = go.Heatmap(
z=a.values.tolist(),
colorscale='Viridis',
colorbar=dict(title='units'),
)
fig.append_trace(data, 1, index+1)
fig['layout']['xaxis'+str(index+1)].update(title='xaxis '+str(index))
fig['layout']['yaxis1'].update(title='y-axis')
fig['layout'].update(height=600, width=800, title='heatmap subplots')
py.iplot(fig)
Simply pass the setting 'shared_yaxes=True' to the tools.make_subplots function call, that is:
fig = tools.make_subplots(rows=1, cols=len(dfl) ,print_grid=False, shared_yaxes=True)
Updated answer for newer versions of Plotly. Based upon the v4 migration guide.
Differences include
calling the make_subplots function from the plotly.subplots library, instead of plotly.tools, and
using fig.show inplace of py.iplot to show the figure in Jupyter.
The code below was tested in Plotly version 5.1.0 using Jupyter Notebook.
import plotly
import plotly.graph_objs as go
import pandas as pd
import numpy as np
dfl = []
dfl.append(pd.DataFrame(np.random.random((100,100,))))
dfl.append(pd.DataFrame(np.random.random((100,100,))))
fig = plotly.subplots.make_subplots(rows=1, cols=len(dfl) ,print_grid=False, shared_yaxes=True);
for index, a in enumerate(dfl):
sn = str(index)
data = go.Heatmap(
z=a.values.tolist(),
colorscale='Viridis',
colorbar=dict(title='units'),
)
fig.append_trace(data, 1, index+1)
fig['layout']['xaxis'+str(index+1)].update(title='xaxis '+str(index))
fig['layout']['yaxis1'].update(title='y-axis')
fig['layout'].update(height=600, width=800, title='heatmap subplots')
fig.show()
The output:

Stop seaborn plotting multiple figures on top of one another

I'm starting to learn a bit of python (been using R) for data analysis. I'm trying to create two plots using seaborn, but it keeps saving the second on top of the first. How do I stop this behavior?
import seaborn as sns
iris = sns.load_dataset('iris')
length_plot = sns.barplot(x='sepal_length', y='species', data=iris).get_figure()
length_plot.savefig('ex1.pdf')
width_plot = sns.barplot(x='sepal_width', y='species', data=iris).get_figure()
width_plot.savefig('ex2.pdf')
You have to start a new figure in order to do that. There are multiple ways to do that, assuming you have matplotlib. Also get rid of get_figure() and you can use plt.savefig() from there.
Method 1
Use plt.clf()
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
length_plot = sns.barplot(x='sepal_length', y='species', data=iris)
plt.savefig('ex1.pdf')
plt.clf()
width_plot = sns.barplot(x='sepal_width', y='species', data=iris)
plt.savefig('ex2.pdf')
Method 2
Call plt.figure() before each one
plt.figure()
length_plot = sns.barplot(x='sepal_length', y='species', data=iris)
plt.savefig('ex1.pdf')
plt.figure()
width_plot = sns.barplot(x='sepal_width', y='species', data=iris)
plt.savefig('ex2.pdf')
I agree with a previous comment that importing matplotlib.pyplot is not the best software engineering practice as it exposes the underlying library. As I was creating and saving plots in a loop, then I needed to clear the figure and found out that this can now be easily done by importing seaborn only:
since version 0.11:
import seaborn as sns
import numpy as np
data = np.random.normal(size=100)
path = "/path/to/img/plot.png"
plot = sns.displot(data) # also works with histplot() etc
plot.fig.savefig(path)
plot.fig.clf() # this clears the figure
# ... continue with next figure
alternative example with a loop:
import seaborn as sns
import numpy as np
for i in range(3):
data = np.random.normal(size=100)
path = "/path/to/img/plot2_{0:01d}.png".format(i)
plot = sns.displot(data)
plot.fig.savefig(path)
plot.fig.clf() # this clears the figure
before version 0.11 (original post):
import seaborn as sns
import numpy as np
data = np.random.normal(size=100)
path = "/path/to/img/plot.png"
plot = sns.distplot(data)
plot.get_figure().savefig(path)
plot.get_figure().clf() # this clears the figure
# ... continue with next figure
Create specific figures and plot onto them:
import seaborn as sns
iris = sns.load_dataset('iris')
length_fig, length_ax = plt.subplots()
sns.barplot(x='sepal_length', y='species', data=iris, ax=length_ax)
length_fig.savefig('ex1.pdf')
width_fig, width_ax = plt.subplots()
sns.barplot(x='sepal_width', y='species', data=iris, ax=width_ax)
width_fig.savefig('ex2.pdf')
I've found that if the interaction is turned off seaborn plot the heatmap normally.

Categories