Normally when using IPython + Jupyter + Matplotlib, I choose a Matplotlib backend using the %matplotlib magic in IPython.
However, sometimes I want to change to a different backend for one specific cell, and then immediately revert to the backend that I was using before. For example, I sometimes want to use the widget backend for a single plot, but I don't want to have it active for the rest of my notebook.
I could of course put %matplotlib widget at the top of the cell and then %matplotlib inline at the bottom of the cell, but is there a nicer solution? Either using a single "temporary" %matplotlib magic (happy to write my own if it's not complicated), or using a context manager.
Is it as simple as this, or is there more to it?
#contextmanager
def mpl_backend(backend):
original = matplotlib.get_backend()
yield matplotlib.use(backend)
matplotlib.use(original)
Related
I am trying to scale my decision tree to fit notebook but it appears not to scale properly. I have to keep scrolling for a better view. Can I please have some help on how to fix this. Attach is a pic of how it looks like.
from graphviz import Source
from sklearn import tree
from IPython.display import SVG
graph = Source( tree.export_graphviz(dt_classifier, out_file=None, feature_names=X.columns))
SVG(graph.pipe(format='svg'))
Perhaps it's not relevant any more, since this question has been open for about six months now. However, I just stumbled into it, as apparently 83 other readers, and I just crafted my way around this. The easy way is to use the pydot package (pip install pydot), and then add the default size. I have also been using %matplotlib inline so that it displays nicely within the notebook but without using the svg module. With your example:
%matplotlib inline
from graphviz import Source
from sklearn import tree
import pydot
dot_data = tree.export_graphviz(dt_classifier, out_file=None, feature_names=X.columns))
pdot = pydot.graph_from_dot_data(dot_data)
# Access element [0] because graph_from_dot_data actually returns a list of DOT elements.
pdot[0].set_graph_defaults(size = "\"15,15\"")
graph = Source(pdot[0].to_string())
graph
I also added rotate=True to export_graphviz so that it displays in horizontal style, the root of the tree is directly visible, and is easier to follow. Of course, you can play around with size so as to reach something that is acceptable for you.
I have searched this for long time, similar questions have been asked but none solved my problem.
Usually I run my py files by ctrl+shift+r, and what I expect is to show the graphs in side the session, like the way it works for Jupyter notebook. But instead it either omits the graph, or show it separately and if I have several graphs they will all pop once and lock the session.
Something like %matplotlib inline but for default python files.
So far I have tried to add the env var for Pycharm, and tried to play around with plt.isinterative, nothing worked :(
Thank you.
I want to do something like the following using a loop and PyPlot to plot in figure windows. My question is how to save the figure windows to a movie file within the loop?
using PyPlot
for k=1:5
pcolormesh(rand(10,10))
if k==1; colorbar(); end
# save figure window to movie file here??
sleep(.5)
end
This is possible by directly using the animation submodule of matplotlib (the underlying Python library that PyPlot.jl wraps). However, it is painful; see e.g. the following notebook (in Spanish):
https://github.com/dpsanders/fisica_computacional/blob/master/como_animar.ipynb
The simplest way, however, seems to be using Plots.jl:
https://github.com/tbreloff/ExamplePlots.jl/blob/master/docs/pyplot_examples.md#functions-adding-data-and-animations.
I have some python simulation code that runs for a minute or two and displays the output of the simulation in real-time.
I would like to show results on two different figure windows. Because I'm using an object oriented design, it's a little difficult to switch between the figures using the figure(x) command, so I want to save a reference to each figure to each variable and use that to return to them. I've set it up like the following:
import pylab as p
f0 = p.figure()
f1 = p.figure()
ax0 = f0.add_subplot(111)
ax0.plot(range(0,50))
ax1 = f1.add_subplot(111)
ax1.plot(range(0,20))
ax1.text(0,1,"This is updatable",weight='bold',fontsize=16)
ax0.text(0,1,"This one, drawn first, is not.",weight='bold',fontsize=16)
p.pause(5)
The problem is that once I've created and accessed the second figure, the first figure is no longer accessible.
I am using matplotlib and running Python 2.7.6 using Pycharm CE with the MacOSX interactive backend.
Does anyone know how to set this up? Am I using the wrong graphing package for this kind of problem?
UPDATE: I found that if I closed the first window just using my mouse to access the on-window controls, then the second window would be updated. That doesn't solve my problem, though.
This is a bit workaround-y but I found one solution is to make whichever figure I want updated to be the current figure, using the command:
p.figure(f0.number)
I wasn't aware I could access the number of the figure like this, but this does seem to get around my problem!
I'd like the background of my matplotlib plots to be transparent in my IPython notebook. This may sound silly because the notebook itself defaults to a white background but:
1) I use a solarized background and
2) more importantly, I want them to be transparent for when I embed the notebook directly into my blog via nbconvert.
It's easy enough to use something like savefig('file', transparent=True) , but I'm not saving the figures, I am displaying them inline (by calling IPython with ipython notebook --matplotlib inline.
I've been playing around with the IPython notebook configuration file, especially with c.InlineBackend.rc. For example, I upgraded to the dev version of matplotlib to get access to its new savefig.transparent rcParam, and tried configuring that with c.InlineBackend.rc = {'savefig.transparent': True}, but as expected it only affects plots saved with savefig.
Note that I am using the recent IPython 2.0 release. This must be possible somehow, right? Any light that you can shed would be appreciated.
Just to follow up, the issue opened on Github by tillsten has been patched so something like this:
rcParams['figure.facecolor'] = (0,0,0,0)
should work now after you update IPython. Three cheers for open source.
The inline plots are html objects (<img>) with class ui-resizable. So you can change their default behavior by customizing the CSS for your notebooks:
locate your settings for notebooks: in a terminal, type
ipython locate
in the indicated directory, go to subdir profile_default\static\custom (or any profile you want to use instead)
edit or create a CSS file named custom.css
put this in it:
img.ui-resizable
{
opacity:0.4;
}
Close your notebooks, kill IPython and restart it (so that it recreates the served files).
It should work with exported notebooks, as long as you export them as html and you change the css there too.
It's not exactly what you want, but it does the job.