3

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.

Maq92
  • 295
  • 1
  • 4
  • 16

2 Answers2

4

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']))
j-i-l
  • 10,281
  • 3
  • 53
  • 70
  • Thak You very much :) – Maq92 Aug 10 '14 at 18:00
  • @Maq92: Well your code has several `plt.pause` calls in it, try to remove the last one before saving the current figure. – j-i-l Aug 11 '14 at 12:13
  • No, the last pause have to be there. I'm talking about this time before "updateing" grapht (getting new point and draw it). I'm going to draw some huge graphs with it and don't want to wait few hours for it :) Is there any way to boost it? – Maq92 Aug 11 '14 at 12:16
  • @Maq92 Do you need to draw the current figure in every step when you add new data? Based on the size of the data you want to show, `plt.draw` just takes some time and at least to my knowledge there is not much you can do to speed it up. But for the example code you have here, the time you need to wait should virtually be determined by the `plt.pause` calls. – j-i-l Aug 11 '14 at 12:43
  • This will be a program that draws (and live update) a graph based on data recived from external equipement. Hm, ok thanks for help :) – Maq92 Aug 11 '14 at 12:54
  • @Maq92 This might be helpful for what you plan to do: http://stackoverflow.com/questions/10944621/dynamically-updating-plot-in-matplotlib – j-i-l Aug 11 '14 at 13:34
4

If you are going to be running this automatically it is better to use the OO interface rather than the state machine interface:

import matplotlib.pyplot as plt
import os

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

# create the figure and axes
fig, ax = plt.subplots(1, 1)
ax.set_xlim(0, 200)
ax.set_ylim(0, 200)

# create the line2D artist
ln, = ax.plot(x, y, 'g-', linewidth=1.5, markersize=4)
# do the looping
for i in range(0,20):
    # add to the data lists
    x.append(i)
    y.append(i+2)
    # update the data in the line2D object
    ln.set_xdata(x)
    ln.set_ydata(y)
    # force the figure to re-draw
    fig.canvas.draw()
    # pause, let the gui re-draw it's self
    plt.pause(0.1)
# pause again?
plt.pause(5)
# save the figure
fig.savefig(os.path.join(home1,  '1'),format='png') 

[not tested, due to discovering an unrelated bug in RC3]

tacaswell
  • 84,579
  • 22
  • 210
  • 199