# 2 questions : savefig + show

hello here my code that works

x=var('x')
f(x)=exp(-1/x^2)
p=[]
legendes=[]
for n in range(8):
dn(x)=diff(f(x), x, n)
print("f^(",n,")(x)=",dn(x))
delta=n/2
p.append(\
plot(dn(x)/factorial(n),-4+delta,4-delta)\
+text("n="+str(n),(0,0.1*factorial(n)),color="green",bounding_box={'boxstyle':'round', 'fc':'w'}))
show(p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7])
#save(p[0]+p[1]+p[2]+p[3]+p[4]+p[5]+p[6]+p[7],"~/Desktop/toto.png")

#savefig("~/Desktop/toto.png")


my questions :

a) how can i automatize the show, can i write. For example :

show(p[i] for in in range(8))


dont give the image but a code that says : <πππππππππΒ ππππππΒ <πππππ‘ππ>Β ππΒ πΆπ‘π·πΌπΎππ·πππ»πΆ>

b) i can't find the way to save instead of show

in the help, the formulation is something like A.save but i can't get it, dont understand how to write this type of code with mine ?

thanks :-)

edit retag close merge delete

Sort by Β» oldest newest most voted

For (a) you can use show(*[p[i] for i in range(8)]).

For (b), one option is to construct a graphics array:

P = graphics_array([p[i] for i in range(8)], 2, 4)  # 2x4 array of the images
P.save(FILENAME)

more

thanks very much it works very weel

so if in understand well : these both codes are equivalent, aren't they ?

show(*[p[i] for i in range(8)])

show(graphics_array([p[i] for i in range(8)], 2, 4))


so that * is a sort of shortcut of graphics_array() ?

for the second :

P = graphics_array([p[i] for i in range(8)], 2, 4)
#show(P)
P.save("~/Desktop/toto.png")


gives

[Errno 2] No such file or directory: '~/Desktop/toto.png'


i will try to find it out alone

Vinz

more

The use of ~ in filenames is a shell thing, and Python doesn't understand it, or at least this function doesn't understand it. Use the full path: /home/username/toto.png or whatever.

( 2023-01-03 05:54:39 +0200 )edit

* is Python syntax. It passes to show not a single argument, which is a list, but instead passes each of that list's entries as the arguments. So show(*[1,2,3]) is equivalent to show(1,2,3).

( 2023-01-03 05:56:46 +0200 )edit