I am trying to automatically convert an SVG graphic to EMF with Python, using Inkscape on the command line. My code is
from subprocess import call
import matplotlib.pyplot as plt
from numpy import linspace, sin
x = linspace(0,10,10)
y = sin(x)
plt.plot(x,y)
plt.savefig("source.svg")
for k in range(0,5):
call(["C:\Program Files\Inkscape\inkscape.exe", "--file", "source.svg", "--export-emf", "result" + str(k) + ".emf" ])
# this usually breaks down at k = 1 or 2
The "call" command works fine when called once. If I call it multiple times, e.g. on consecutive lines as shown above, Inkscape breaks down and I have to restart my python kernel and kill Inkscape via the Windows Task-Manager.
Any ideas why that might be?
Thanks!
Related
I am running this code using the healpy package. I am not using multiprocessing and I need it to run on a single core. It worked for a certain amount of time, but, when I run it now, the function healpy.projector.GnomonicProj.projmap takes all the available cores.
This is the incriminated code block:
def Stacking () :
f = lambda x,y,z: pixelfunc.vec2pix(xsize,x,y,z,nest=False)
map_array = pixelfunc.ma_to_array(data)
im = np.zeros((xsize, xsize))
plt.figure()
for i in range (nvoids) :
sys.stdout.write("\r" + str(i+1) + "/" + str(nvoids))
sys.stdout.flush()
proj = hp.projector.GnomonicProj(rot=[rav[i],decv[i]], xsize=xsize, reso=2*nRad*rad_deg[i]*60/(xsize))
im += proj.projmap(map_array, f)
im/=nvoids
plt.imshow(im)
plt.colorbar()
plt.title(title + " (Map)")
plt.savefig("../Plots/stackedMap_"+name+".png")
return im
Does someone know why this function is running in parallel? And most important, does someone know a way to run it in a single core?
Thank you!
In this thread they recommend to set the environment variable OMP_NUM_THREADS accordingly:
Worked with:
import os
os.environ['OMP_NUM_THREADS'] = '1'
import healpy as hp
import numpy as np
os.environ['OMP_NUM_THREADS'] = '1' have to be done before import numpy and healpy libraries.
As to the why: probably they use some parallelization techniques wrapped within their implementation of the functions you use. According to the name of the variable, I would guess OpenMP it is.
I am using the following code and python stops responding after executing plt.show(). I noticed removing the next input command removes the error, however, I need to retain both commands.
Next, I tried to sandwich plt.pause(2) between the two commands but here, python stops after I press any key once plot is displayed. Please help:
PS: I am using Atom editor with python 3.7.4
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import fsolve
def f(x):
y=2.0*np.sin(x**2)+3.0*x-10.0
return y
x=np.linspace(-5,3,100000)
plt.ion()
plt.plot(x,f(x))
plt.show()
plt.pause(2)
yy=input("pppp")
print(fsolve(f,2))
You have the following line in your code plt.ion(). If I remove the parentheses from the end of the line the program seems to work fine in Atom.
Introduction
As I am coming from matlab, I am used to an interactive interface where a script can update figures while it is running. During the processing each figure can be re-sized or even closed. This probably means that each figure is running in its own thread which is obviously not the case with matplotlib.
IPython can imitate the Matlab behavior using the magic command %pylab or %matplotlib which does something that I don't understand yet and which is the very point of my question.
My goal is then to allow standalone Python scripts to work as Matlab does (or as IPython with %matplotlib does). In other words, I would like this script to be executed from the command line. I am expecting a new figure that pop-up every 3 seconds. During the execution I would be able to zoom, resize or even close the figure.
#!/usr/bin/python
import matplotlib.pyplot as plt
import time
def do_some_work():
time.sleep(3)
for i in range(10):
plt.plot([1,2,3,4])
plt.show() # this is way too boilerplate, I'd like to avoid it too.
do_some_work()
What alternative to %matplotlib I can use to manipulate figures while a script is running in Python (not IPython)?
What solutions I've already investigated?
I currently found 3 way to get a plot show.
1. %pylab / %matplotlib
As tom said, the use of %pylab should be avoided to prevent the namespace to be polluted.
>>> %pylab
>>> plot([1,2,3,4])
This solution is sweet, the plot is non-blocking, there is no need for an additionnal show(), I can still add a grid with grid() afterwards and I can close, resize or zoom on my figure with no additional issues.
Unfortunately the %matplotlib command is only available on IPython.
2. from pylab import * or from matplotlib.pyplot import plt
>>> from pylab import *
>>> plot([1,2,3,4])
Things are quite different here. I need to add the command show() to display my figure which is blocking. I cannot do anything but closing the figure to execute the next command such as grid() which will have no effect since the figure is now closed...
** 3. from pylab import * or from matplotlib.pyplot import plt + ion()**
Some suggestions recommend to use the ion() command as follow:
>>> from pylab import *
>>> ion()
>>> plot([1,2,3,4])
>>> draw()
>>> pause(0.0001)
Unfortunately, even if the plot shows, I cannot close the figure manually. I will need to execute close() on the terminal which is not very convenient. Moreover the need for two additional commands such as draw(); pause(0.0001) is not what I am expecting.
Summary
With %pylab, everything is wonderful, but I cannot use it outside of IPython
With from pylab import * followed by a plot, I get a blocking behavior and all the power of IPython is wasted.
from pylab import * followed by ion offers a nice alternative to the previous one, but I have to use the weird pause(0.0001) command that leads to a window that I cannot close manually (I know that the pause is not needed with some backends. I am using WxAgg which is the only one that works well on Cygwin x64.
This question advices to use matplotlib.interactive(True). Unfortunately it does not work and gives the same behavior as ion() does.
Change your do_some_work function to the following and it should work.
def do_some_work():
plt.pause(3)
For interactive backends plt.pause(3) starts the event loop for 3 seconds so that it can process your resize events. Note that the documentation says that it is an experimental function and that for complex animations you should use the animation module.
The, %pylab and %matplotlib magic commands also start an event loop, which is why user interaction with the plots is possible. Alternatively, you can start the event loop with %gui wx, and turn it off with %gui. You can use the IPython.lib.guisupport.is_event_loop_running_wx() function to test if it is running.
The reason for using ion() or ioff() is very well explained in the 'What is interactive mode' page. In principle, user interaction is possible without IPython. However, I could not get the interactive-example from that page to work with the Qt4Agg backend, only with the MacOSX backend (on my Mac). I didn't try with the WX backend.
Edit
I did manage to get the interactive-example to work with the Qt4Agg backend by using PyQt4 instead of PySide (so by setting backend.qt4 : PyQt4 in my ~/.config/matplotlibrc file). I think the example doesn't work with all backends. I submitted an issue here.
Edit 2
I'm afraid I can't think of a way of manipulating the figure while a long calculation is running, without using threads. As you mentioned: Matplotlib doesn't start a thread, and neither does IPython. The %pylab and %matplotlib commands alternate between processing commands from the read-eval-print loop and letting the GUI processing events for a short time. They do this sequentially.
In fact, I'm unable to reproduce your behavior, even with the %matplotlib or %pylab magic. (Just to be clear: in ipython I first call %matplotlib and then %run yourscript.py). The %matplotlib magic puts Matplotlib in interactive-mode, which makes the plt.show() call non-blocking so that the do_some_work function is executed immediately. However, during the time.sleep(3) call, the figure is unresponsive (this becomes even more apparent if I increase the sleeping period). I don't understand how this can work at your end.
Unless I'm wrong you'll have to break up your calculation in smaller parts and use plt.pause (or even better, the animation module) to update the figures.
My advice would be to keep using IPython, since it manages the GUI event loop for you (that's what pylab/pylot does).
I tried interactive plotting in a normal interpreter and it worked the way it is expected, even without calling ion() (Debian unstable, Python 3.4.3+, Matplotlib 1.4.2-3.1). If I recall it right, it's a fairly new feature in Matplotlib.
Alternatively, you can also use Matplotlib's animation capabilities to update a plot periodically:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
plt.ion()
tt = np.linspace(0, 1, 200)
freq = 1 # parameter for sine
t0 = time.time() # for measuring ellapsed time
fig, ax = plt.subplots()
def draw_func(i):
""" This function gets called repeated times """
global freq # needed because freq is changed in this function
xx = np.sin(2*np.pi*freq*tt)/freq
ax.set_title("Passed Time: %.1f s, " % (time.time()-t0) +
"Parameter i=%d" % i)
ax.plot(tt, xx, label="$f=%d$ Hz" % freq)
ax.legend()
freq += 1
# call draw_func every 3 seconds 1 + 4 times (first time is initialization):
ani = animation.FuncAnimation(fig, draw_func, np.arange(4), interval=3000,
repeat=False)
# plt.show()
Checkout matplotlib.animation.FuncAnimation for details. You'll find further examples in the examples section.
I am trying to plot a simple graph using gnuplot and installed the gnuplot-py binding. I am using the script below and I am getting the below error -
gnuplot> set terminal aqua
line 0: unknown or ambiguous terminal type; type just 'set terminal' for a list
I am running on a mac.
Here is the code I am using --
import numpy as np
import Gnuplot
def gnudemo2():
#Create some data
x = np.linspace(0,10,100)
y1 = x**2
y2 = 10*np.sin(np.pi*x)
#Instantiate Gnuplot object
g = Gnuplot.Gnuplot(persist=1)
g('set terminal png size 800,600 enhanced font "Helvetica,8" ')
g('set style data lines')
g('set output \'output1.png\'')
#Create the Gnuplot data
d1 = Gnuplot.PlotItems.Data(x, y1, with_='lp', title='d1')
d2 = Gnuplot.PlotItems.Data(x,y2, with_='l', title='d2')
g.plot(d1,d2)
# when executed, just run gnudemo2():
if __name__ == '__main__':
gnudemo2()
I am setting the terminal to png but still its trying to use the aqua term.
I installed the aquaterm as well. But it still would not work. What could be going on here?
If I use the same lines inside gnuplot on the command line, everything works fine. So I know the commands are correct. But something is missing when I run them through python.
In Spyder 2 (Anaconda distribution) and in the IPython QT Console I'm able to print results of symbolic calculations (from an answer I got for a previous post) but I can't get equations in strings to print with the a IPython's Rich Display System:
from sympy import *
from IPython.display import display, Math
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x, y, z = symbols('x y z')
#----- prints correctly
ii = integrate(x**2 + x + 1, x)
display(ii)
#----- does not print
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
The above prints the results of the integrate correctly but the Math() does not print (no error -- just skips it). Note it all works in SciPy web notebook.
Thank you!
The Math class doesn't generate the rendered image from your Latex, that's why it doesn't work directly.
To get what you want you need to use this code
from IPython.display import Image, display
from IPython.lib.latextools import latex_to_png
eq = r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'
data = latex_to_png(eq, wrap=True)
display(Image(data=data))
Then you will see the right image
Hello guys im still having the problem, whe i use
from IPython.display import display, Math, Latex
display((Math(r'P(Purchase|Male)= \frac{Numero\ total\ de\ compras\ hechas\ por\ hombres\}{Total\ de\ hombres\ en\ el\ grupo\} = \frac{Purchase\cap Male}{Male}')))
on jupyter it works fine, but when i do the exact same code on spyder it doesnt work
im using python 3.6 , spyder 3.3.3
also i tried the marked asnwer but latex_to_png makes a NoneType Object on spyder