mplleaflet in Jupyter - avoid whitespace with notebook mode? - python

mplleaflet works great with '%matplotlib inline' mode in the Jupyter Notebook, but when enabling '%matplotilib notebook' mode, there's a big chunk of whitespace between the cell and the map. Can this be avoided?
Code:
import matplotlib.pyplot as plt
import mplleaflet
%matplotlib notebook
#some latitudes bracketing seattle
lats = [47.5062,47.7062]
#longitude
lons = [-122.3321]*len(lats)
fig,ax=plt.subplots(figsize=(8,8))
ax.scatter(lons, lats, alpha=0) #invisible data points, just to scale the map
mplleaflet.display(fig=fig)
The output looks like this:

The problem is that there is a figure created which is supposed to be shown in the notebook. However, you do not want to show the mplleaflet figure as an interactive figure with the %matplotlib notebook backend.
One idea is to not use the %matplotlib notebook such that the area where the figure mplleaflet figure would be placed is not filled with the unused figure.
Alternatively use %%capture in order to suppress the output when using %matplotlib notebook. Then call mplleaflet.display in a new non-captured cell:

Related

Plotly Mapbox images showing blank with blue background (Jupyter Notebook)

I am running the following code to plot points against a city backdrop using Mapbox within Plotly in a Jupyter Notebook, but the plot does not show up, I just get a blue background.
I suspect that I am not using the token correctly?
import plotly.express as px
MBToken = 'pk.[mypublickey]'
px.set_mapbox_access_token(MBToken)
fig = px.scatter_mapbox(dfMaster.dropna()
, lat="latitude"
, lon="longitude"
, color="nta"
, size="count_of_testers"
#, color_continuous_scale=px.colors.cyclical.IceFire
#, size_max=15
#, zoom=10
)
fig.show()
#fig = px.scatter(x='latitude',y='longitude',data_frame=df)
#fig.show()
Running that gives me:
It does not appear to be a Plotly issue, the commented out code creates a scatter plot (although that has the same blue background, but the points show)
Some other posts have pointed to Jupyter offline mode being the possible culprit, but adding this did not resolve
import plotly.offline as pyo
pyo.init_notebook_mode()
Additionally, tried starting up the Jupyter notebook with a higher data rate limit as suggested, but no luck there either
This ended up being a silent data integrity error, as the size field was a string and needed to be converted into numeric

Include output from %matplotlib notebook backend as SVG in ipynb

This answer from a few years ago shows how you can make jupyter notebook create graphs as svg. The solution is to tell the InlineBackend to use svg as output.
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
plt.plot(...)
This will cause all images to be in svg format inside the notebook as well as in the produced ipynb file; the file will have a line like
"data": { "image/svg+xml": [ "<?xml .....
in it.
The problem is now that this does not work if the %matplotlib notebook backend is used. %config InlineBackend does not change anything for the notebook backend, hence the output file contains a PNG image
"data": { "text/html": [ "<img src=\"data:image/png;base64,iVBORw0....
So the question is: How do I get the ipynb file to include a static version of the plot that is created with the %matplotlib notebook backend as SVG image?
There is a small comment by #mark jay from one month ago, who wanted to do exactly what I would like to do now, but there is no answer or hint to that comment.
In my code I have plotted directly from the dataframe:
%matplotlib notebook
import pandas as pd
df = pd.read_sql(sql1, connection)
...
...
df.plot(subplots=True, kind='bar')
This functions perfectly well without importing matplotlib.pyplot but it also can't be coerced to create the graphic as an svg. I suppose if the base case would work, I could modify the plotting code so it did not involve pandas or dataframes.
Since apparently even after a bounty period noone was able to provide a solution, a workaround may be the following.
Create you notebook with %matplotlib notebook. Once you're satisfied with the result, save it.
Use a copy of it and replace %matplotlib notebook with
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
Rerun the complete notebook. Save the result.
Open the resulting ipynb file in a text editor and replace the previous two lines again with %matplotlib notebook.
The final result will be a ipynb with svg images. But once opened and run, it will use the notebook backend for figure creation.
From whatI understand from reading about matplotlib backends, nbagg, which is called using %matplotlib notebook uses the Agg (Anti-Grain Geometry) render which is not capable of rendering vector graphics. Unfortunately this is the only out of the box way of using an interactive inline backend for Jupyter.
Docs Link https://matplotlib.org/faq/usage_faq.html#what-is-interactive-mode
Similar Answer How to make matplotlibs nbagg backend generate SVGs?
If you don't need the interactivity just keep use
import pandas as pd
from IPython.display import SVG, display
from numpy import ndarray
def svg_add(chart, size=(4,4), dpi=100):
"""Takes a chart, optional tuple of ints for size, int for dpi
default is 4 by 4 inches with 100 dpi"""
if type(chart) == ndarray:
fig = chart[0].get_figure()
fig.set_size_inches(size)
fig.savefig("mybar.svg", dpi=dpi)
display(SVG(filename='mybar.svg'))
else:
fig = chart.get_figure()
fig.set_size_inches(size)
fig.savefig("mybar.svg", dpi=dpi)
display(SVG(filename='mybar.svg'))
then
df = pd.DataFrame([[2,5]],columns=['a','b'])
bar_chart = df.plot(subplots=False, kind='bar')
svg_add(chart=bar_chart,size=(3,3),dpi=100)
#or
#svg_add(bar_chart,(3,3),100)

unwanted blank subplots in matplotlib

I am new to matplotlib and seaborn and is currently trying to practice the two libraries using the classic titanic dataset. This might be elementary, but I'm trying to plot two factorplots side by side by inputting the argument ax = matplotlib axis as shown in the code below:
import matploblib.pyplot as plt
import seaborn as sns
%matplotlib inline
fig, (axis1,axis2) = plt.subplots(1,2,figsize=(15,4))
sns.factorplot(x='Pclass',data=titanic_df,kind='count',hue='Survived',ax=axis1)
sns.factorplot(x='SibSp',data=titanic_df,kind='count',hue='Survived',ax=axis2)
I was expecting the two factorplots side by side, but instead of just that, I ended up with two extra blank subplots as shown above
Edited: image was not there
Any call to sns.factorplot() actually creates a new figure, although the contents are drawn to the existing axes (axes1, axes2). Those figures are shown together with the original fig.
I guess the easiest way to prevent those unused figures from showing up is to close them, using plt.close(<figure number>).
Here is a solution for a notebook
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
%matplotlib inline
titanic_df = pd.read_csv(r"https://github.com/pcsanwald/kaggle-titanic/raw/master/train.csv")
fig, (axis1,axis2) = plt.subplots(1,2,figsize=(15,4))
sns.factorplot(x='pclass',data=titanic_df,kind='count',hue='survived',ax=axis1)
sns.factorplot(x='sibsp',data=titanic_df,kind='count',hue='survived',ax=axis2)
plt.close(2)
plt.close(3)
(For normal console plotting, remove the %matplotlib inline command and add plt.show() at the end.)

Blank image when saving an imshow matplotlib figure in iPython notebook

I'm using iPython notebook w/ matplotlib to display a bunch of images inline, but now it's come time to save a number of these images (think for-loop, i.e. not a small number of images to save). My issue seems to be something to do with how I'm using iPython since I could do this alright when my script was a standalone.
%matplotlib inline
import matplotlib.pyplot as plt
....
grid_z2 = griddata(....)
fig = plt.figure()
ax = fig.add_axes([1,1,1,1])
plt.imshow(grid_z2.transpose(),origin='Lower')
plt.colorbar()
plt.draw()
fig.savefig('slicemap.png')
I have also tried plt.savefig(), fig1 = plt.gcf() before plt.imshow then trying to save fig1... always every single time a blank file.
Any suggestions?

displaying charts from python executables in shell

I'm using python 2.7 on Ubuntu to draw charts from text files containing data.
My point is, when using python executables in shell, I have no problem recording plots, but if I want them shown on my screen instead I have to go through the graphic interface. If possible, I would very much like to skip that part and get a dynamic display that I can interact with (as if I were to run my script from a python shell)!!
a MWE of what i'm doing is :
MWE
import numpy as np
import matplotlib.pyplot as plt
with open('filename','r') as myfile:
DATA = np.genfromtxt(myfile,unpack=True)
fig = plt.figure()
... my plot configuration ...
plt.savefig("image_name"+'.png')
plt.close()
end of MWE
Using this script image_name.png appears in my repertory. I tried replacing the last 2 lines with plt.plot() and plt.draw() but nothing happened.
Many thanks!
Michel
(edited)

Categories

Resources