I just upgraded to IPython Notebook version 3.0 and it's disabling the formatting for seaborn. Here's some sample code that replicates the problem
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
data = np.random.randn(100)
fig,ax = plt.subplots(figsize = (11,8.5))
ax.plot(data)
This code works just fine in IPython Notebook V2.4.1 (see http://nbviewer.ipython.org/gist/anonymous/71733c24a68ee464ca40), but in IPython Notebook v3.0, the axes become invisible (see http://nbviewer.ipython.org/gist/anonymous/7525146b07709206908c).
Strangely, in V3, when I switch the order of the seaborn import and the matplotlib inline magic, the plot renders normally the first time I run, then if I re-run, the axes and gridlines disappear. So it seems to have something to do with the inline magic disabling seaborn properties.
Any workarounds, other than not re-executing my imports after the first time?
In iPython Notebook 3.0, add:
seaborn.set_style('darkgrid')
to restore Seaborn default color schemes.
Related
I have written a small library, whose functions generate plots using this structure:
from matplotlib import pyplot as plt, rcParams, rc_context
with rc_context():
print(f'Figure backend {plt.get_backend()}')
x = [1,2,3,4,5]
y = [1,2,3,4,5]
fig, ax = plt.subplots()
ax.scatter(x,y)
plt.show()
plt.close(fig)
I would like this library to be compatible with jupyter notebooks but I am having issues with the backends.
For example: In "qt" backends the plot window is closed inmediatly and in "nbAgg" backends the plot is deleted. This code reproduces the issue.
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt, rcParams, rc_context
%matplotlib notebook
with rc_context():
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
ts = ts.cumsum()
ts.plot()
plt.close()
One solution is removing the plt.close(fig) but that would leave the figures opened.
Another option is adding some criteria to keep/close the figures depending on the backend, but from what I have seen the nomenclature changes from one OS to another, or if running from a notebook. The latter option is not easy though.
I wonder if anyone would please share their experience to keep the matplotlib figures in notebooks.
I have some plotting functions that are called in a jupyter notebook. The plotting functions include the calls
plt.figure()
and
plt.show()
However, the notebooks add annotations to the plot after the fact. The annotations work properly with
%matplotlib nbagg
but not
%matplotlib inline
which creates a new figure instead of annotating the old one (edit: this is the same behaviour as running a script in terminal). It seems that there's a scoping issue that nbagg circumvents and that inline does not. See below for a minimal reproducible example.
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
def plotter():
plt.figure()
x = np.arange(-3,3,0.1)
y = np.exp(-x*x)
plt.scatter(x,y)
plt.show()
plotter()
plt.text(0, 0.5, 'gaus', horizontalalignment='center')
plt.show()
Question: is there a way I can get this to work in inline mode, e.g., by getting the most recently shown figure? I'd rather not edit the back-end plotting functions, which are not my own.
I am using PyCharm as the IDE for python, and when you make a plot (with the same code like pyplot.plot(...), pyplot.show()) pycharm displays it within its IDE. However, this looks like a static image. When you zoom in, the plot starts to blur.
In other IDE, pyplot creates an interactive plot. When you zoom in, it basically re-plots the curve. And you can also drag the plot. Is there anyway in PyCharm I can have the interactive plot from pyplot?
Just need to change your plotting backend.
If you're on macOS:
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use('macosx')
plt.plot(range(10))
should produce a new window that looks like this:
Or if you prefer a different backend or are on Windows (as #MichaelA said)
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use('Qt5Agg') # or can use 'TkAgg', whatever you have/prefer
plt.plot(range(10))
How can I display an image imported with numpy package using matplotlib in ipython?
It should be fairly easy with the command
import numpy as np
import matplotlib.pyplot as plt
im = np.array(Image.open('image.jpg'))
plt.imshow(im)
But the image does not show and I just get the output
<matplotlib.image.AxesImage at 0x7fb38e10ff10>
You must call plt.show() to actually bring up the windows.
You can get around this by using interactive mode. But for scripts it is better to simply call show() after completing all your plotting commands.
In IPython or Jupyter notebooks, if you want to show images as inline in the notebook and not in a separate window, implement the code shown below.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
im = np.array(Image.open('image.jpg'))
plt.imshow(im)
plt.show()
Attached below are two plots. The only difference in the script that produced them is that the second one had an additional line:
import seaborn as sns
I am not setting any seaborn style yet. Just importing seaborn is changing plot style though, even in plots not using seaborn. Is there any way I can import seaborn (to be used in other plots), and not have the style changed for plots that do not use it?
Check this
import seaborn.apionly as sns or from seaborn.apionly import lmplot
http://stanford.edu/~mwaskom/software/seaborn/whatsnew.html