Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically saving plot, python, matplotlib

I'd like to save my plot after it's finished. I've tried something like this:

import matplotlib.pyplot as plt
import os

plt.ion()
x = []
y = []
home = os.sep.join((os.path.expanduser('~'), 'Desktop'))
home1 = home + '\nowy'

for i in range(0,20):
    x.append(i)
    y.append(i+2)

    plt.plot(x, y, 'g-', linewidth=1.5, markersize=4)
    plt.axis(xmin = 0,xmax = 200,ymin=0,ymax=200)
    plt.show()
    plt.pause(0.1)
plt.pause(5)
plt.savefig(os.sep.join(home1 + '1'),format = 'png') 

But it does not work. Thers is an error:

[Errno 22] invalid mode ('wb') or filename: 'C\\:\\\\\\U\\s\\e\\r\\s\\\\\\M\\i\\c\\h\\a\\l\\\\\\D\\e\\s\\k\\t\\o\\p\\\n\\o\\w\\y\\p\\l\\o\\t\\1.png'

Can anyone tell me how to save this plot exactly in direction "home1", please? I've serched for sollution for a while but nothing worked.

like image 1000
Maq92 Avatar asked Aug 09 '26 13:08

Maq92


1 Answers

Try it with os.path.join:

plt.savefig(os.path.join(os.path.expanduser('~'), 'Desktop', 'nowy1.png')) 

Alternatively you can use os.sep.join, which makes your code more compatible. It could look somehow like this:

plt.savefig(os.sep.join([os.path.expanduser('~'), 'Desktop', 'nowy1.png']))
like image 129
jojo Avatar answered Aug 11 '26 02:08

jojo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!