Repository: qutip/qutip

Issue:
Plotting on Bloch sphere
Hey QiTiP community,
I wanted to make a simple bloch sphere animation, but can only get one image from the save() function.
Only the first .png image has a bloch sphere in it, the others are empty. 
..............................................................
bloch_0.png
![bloch_0](https://cloud.githubusercontent.com/assets/8253676/5883346/bca4bdf6-a351-11e4-8d04-9f1ba385a42c.png)
..............................................................
bloch_1.png
![bloch_3](https://cloud.githubusercontent.com/assets/8253676/5883348/bca8d08a-a351-11e4-88b6-8c97800aedc3.png)
..............................................................
bloch_2.png
![bloch_2](https://cloud.githubusercontent.com/assets/8253676/5883349/bca9d25a-a351-11e4-8b10-eacea1b7437f.png)
..............................................................
bloch_3.png
![bloch_1](https://cloud.githubusercontent.com/assets/8253676/5883347/bca637ee-a351-11e4-8495-b5f12557ad6d.png)
..............................................................

Here is my simple piece of code mainly coped from the tutorial.

```
import numpy as np
from qutip import *
from scipy import *

b = Bloch()

for i in range(30):
    print i
    b.add_vectors([np.sin(i),0,np.cos(i)])
    b.save(dirc='temp')
```

I would be glad if someone could help me.

```
>>> about()

QuTiP: Quantum Toolbox in Python
Copyright (c) 2011 and later.
Paul D. Nation & Robert J. Johansson

QuTiP Version:      3.1.0.dev-23e5259
Numpy Version:      1.8.2
Scipy Version:      0.14.0
Cython Version:     0.21.1
Matplotlib Version: 1.3.1
Fortran mcsolver:   False
scikits.umfpack:    False
Python Version:     2.7.8
Platform Info:      Linux (x86_64)
Installation path:  /usr/local/lib/python2.7/dist-packages/qutip
```



Discussion:
Dear fromage88,
I was able to reproduce your problem. It seems, that the save() kinda dumps the figure.
Anyway I was able to produce a quick workaround:

```
import numpy as np
from qutip import *
from scipy import *

for i in range(5):
    b = Bloch()
    print (i)
    b.add_vectors([np.sin(i),0,np.cos(i)])
    b.save(name='bloch'+str(i))
```

I hope this is what you intended.
Furthermore, you could have a look at the Bloch3d class, as it can also create animations without the need of you to make gifs from 30+ pictures.
Regards,
mmensing

Thanks @fromage88 for the bug report and @mmensing for the proposed work around. I could also reproduce the bug and confirm that the work around works.

An alternative work around until this is fixed is directly access the underlying matplotlib figure instance:

```
import numpy as np
from qutip import *

b = Bloch()

for i in range(5):
    b.add_vectors([np.sin(i),0,np.cos(i)])
    b.render()
    b.fig.savefig("bloch_sf%d.png" % i)
```

Thanks for your work around suggestions. These will help me out for the moment.


Resolving patch:
diff --git a/qutip/bloch.py b/qutip/bloch.py
--- a/qutip/bloch.py
+++ b/qutip/bloch.py
@@ -654,16 +654,14 @@ def save(self, name=None, format='png', dirc=None):
                 os.makedirs(os.getcwd() + "/" + str(dirc))
         if name is None:
             if dirc:
-                plt.savefig(os.getcwd() + "/" + str(dirc) + '/bloch_' +
+                self.fig.savefig(os.getcwd() + "/" + str(dirc) + '/bloch_' +
                             str(self.savenum) + '.' + format)
             else:
-                plt.savefig(os.getcwd() + '/bloch_' + str(self.savenum) +
+                self.fig.savefig(os.getcwd() + '/bloch_' + str(self.savenum) +
                             '.' + format)
         else:
-            plt.savefig(name)
+            self.fig.savefig(name)
         self.savenum += 1
-        if self.fig:
-            plt.close(self.fig)
 
 
 def _hide_tick_lines_and_labels(axis):


Test patch: