python - Passing arguments to plt.savefig in Matplotlib -


i wrote function save plots in matplotlib. when wanted use calling plt.savefig(fname=path, dpi=dpi, format=ext, bbox_inches="tight") got following error.

  file "/home/jruota/.local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 697, in savefig     res = fig.savefig(*args, **kwargs)   file "/home/jruota/.local/lib/python2.7/site-packages/matplotlib/figure.py", line 1572, in savefig     self.canvas.print_figure(*args, **kwargs) typeerror: print_figure() takes @ least 2 arguments (5 given) 

this error not occur when change call plt.savefig(path, dpi=dpi, format=ext, bbox_inches="tight"). guess error has * , ** argument packing , unpacking unsure how exactly. explanation appreciated.

first, it's clear if

plt.savefig(path, dpi=dpi, format=ext, bbox_inches="tight") 

is working fine, may use it.

the error comes fname not being named argument. the documentation may bit confusing people not familiar call signatures in python @ point.

the signature matplotlib.pyplot.savefig(*args, **kwargs), argument fname needs set , further keyword arguments can specified. argument must not keyword argument of course. in

fname = "myfile.png" plt.savefig(fname, dpi=100) 

fname argument, while in

fname = "myfile.png" plt.savefig(fname=fname, dpi=100) 

fname keyword argument, such no argument specifying filename found.

you may test self-defined function, if want:

def f(*args,**kwargs):     print "args: ",  args     print "kwargs: ", kwargs 

then

f("q", k="w")  # prints args:  ('q',)                #        kwargs:  {'k': 'w'} 

while

f(fname="q", k="w") # prints args:  ()                     #        kwargs:  {'k': 'w', 'fname': 'q'} 

in latter case args empty.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -