I am working in a jupyter notebook that produces several figures, each of which gets saved to an svg file.
In each call to savefig() I am using bbox_inches='tight' as such:
ax.figure.savefig(path.join(graphics_dir,'filename1.svg'),bbox_inches='tight')
ax.figure.savefig(path.join(graphics_dir,'filename2.svg'),bbox_inches='tight')
...
Is there some configuration setting I can use to set bbox_inches='tight' for the whole notebook so I don't have to pass it in every time?
Thanks!
The rcParameter
savefig.bbox : tight
This can be set in the matplotlib rc file, or via code on top of your notebook, like
plt.rcParams["savefig.bbox"] = "tight"
Related
I'm creating an interactive plot using Pywedge Bibliothek in jupyter notebook using this code
x=pw.Pywedge_Charts(df, c=None, y='Number_Trips')
charts=x.make_charts()
charts
an example of the output is as follows
and i can desactivate as well as activate the day i want to see.. my question is how can i save it and keep it interactive to use it in a website or if it is even possible?
The way to save interactivity is to save the source of df and the code to generate the plot. To open an interactive plot, run the script that generates it.
With the bokeh package, I created a graph visualization and with show(plot) the output is shown in Jupyter but also at the same time a new tab in the browser is opened with the output. I only want to plot it in a Jupyter cell without opening the plot as a new html file. Is this somehow possible?
#Booketh plot
output_file("thisisaplot.html")
show(plot)
According to the documentation, you should be able to do that by calling output_notebook() before calling show().
Note: As pointed out by #bigreddot, if you were using output_file in the kernel, changing to output_notebook may not take place right away. You will need to either call reset_output(), or restart your kernel for the change to take place.
I want to save a plot created using matplotlib to a file but I do not want it to show as inline plot in Spyder IDE. My code:
import matplotlib.pyplot as plt
from math import sin,pi
import numpy as np
x = np.linspace(0,2*pi,100)
y = np.sin(x)
plt.plot(x,y)
plt.savefig('sin.png')
When I run this code, the plot keep showing in IPython console as inline plot whereas I just want to save it to a file. How can I fix this problem?
add plt.close() after plt.savefig().
This behaviour is steered by some of the settings in spyder.
First, you may of course opt not to use IPython at all. Instead if the script is executed in a new python console, it will not pop up at all without you specifying plt.show() at the end.
If however you want to use IPython, but not get any graphical output, you may deactivate the graphical output for the IPython console. I.e. no checkmark at "Activate support". This will then also require you to call plt.show() to actually show the figure in a new window.
Note that changing those settings will require to restart Spyder.
Those are the general settings. If you want this behaviour only for a single script, use plt.close() at the end of a cell/script.
I want to show plots in a separate window. Currently I have the IPython graphics backend set to "automatic".
When I re-run the code (or plot another figure), Spyder opens a new plot window. Is it possible to refresh the figure in the window that is already opened instead of opening a new one?
The GUI window that opens when you call plt.show() is bound to a figure. You cannot change the figure inside it. (Well, to be precise, there might be an option of obtaining a handle from the operating system and manipulating its content, but I assume this is not worth the effort.)
Re-running the code actually means that you produce a new figure since the code does not know that it's been run before.
So, exchanging the figure or reusing the window to plot a different figure is not possible.
What is possible however is to use the figure and manipulate the figure while it's open. This is done via plt.ion(). After calling this command in IPython you can adapt the figure, e.g. adding new lines to it etc.
See this example:
At IN [6] the window opens and when IN [7] is executed, the figure stays open and the content changes.
Sure, it is possible with Spyder while in the same running kernel. Try the following example using num as parameter to plt.figure(), where num will always refer to the same figure and refresh it if already opened. Also works with plt.subplots().
import matplotlib.pyplot as plt
from scipy import *
t = linspace(0, 0.1,1000)
w = rand(1)*60*2*pi
fig = plt.figure(num=10, clear=True, figsize = [10,8])
plt.plot(t,cos(w*t))
plt.plot(t,cos(w*t-2*pi/3))
plt.plot(t,cos(w*t-4*pi/3))
I am using matplotlib to draw charts and graphs.
When I plot the chart using the command show() my code blocks at this command.
I would like to refresh my list of values with new data , and than refresh the image on the background. How to do that without closing each time the window with the graph?
Below is the code I am using
import pylab
a = (1,2,3,4)
pylab.plot(a)
pylab.show() # blocks here
In IPython started with -pylab it should not block.
Otherwise:
With ion() you turn the interactive mode on. show() does not block your system
anymore. Every draw() or plot(x, y) updated your plot.
ioff() turns interactive mode off. Useful if you add lots of data and don't
want to update every little detail.
See also: http://www.scipy.org/Cookbook/Matplotlib/Animations
If you are not using the IPython shell but instead running a program, you probably want to do:
pyplot.draw()
after a plot(), possibly followed by
raw_input("Press enter when done...")
so as to wait for the user before plotting something else.
If you do pyplot.ion() at the beginning of your program, doing draw() can often even be skipped.
pyplot.show() is actually an infinite loop that handles events in the main plotting window (such as zooming, panning, etc.).
On MacOS X i had the problem that unblocking only produced a white screen. In the end #tyleha's suggestion using %pylab directly in the note book helped.
In fact it's suggested when using the deprecated the -pylab flag:
bash:~/Projects/plyground $ python -m IPython notebook -pylab
WARNING: `-pylab` flag has been deprecated.
Use `--matplotlib <backend>` and import pylab manually.
[E 21:09:05.446 NotebookApp] Support for specifying --pylab on the command line has been removed.
[E 21:09:05.447 NotebookApp] Please use `%pylab` or `%matplotlib` in the notebook itself.
This works by invoking Ipython with the -wthread (or the -pylab) option. It will not block on show anymore.