1

I am trying to assign a particular variable name to the generated matplotlib picture. However, I am not able to do so. Here is the code:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
step = 0.04
maxval = 1.0
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

matplotlib.pyplot.jet()
r = np.linspace(0,1.25,50)
p = np.linspace(0,2*np.pi,50)
R,P = np.meshgrid(r,p)
X,Y = R*np.cos(P),R*np.sin(P)

Z = ((R**2 - 1)**2)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet)
ax.set_zlim3d(0, 1)
ax.set_xlabel(r'$\phi_\mathrm{real}$')
ax.set_ylabel(r'$\phi_\mathrm{im}$')
ax.set_zlabel(r'$V(\phi)$')

kurs = 252

plt.savefig(kurs, format='png')

As you can see, I am trying to assign a variable named 'kurs' as the name of the picture. Any help will be appreciated. Thank you.

3
  • What do you mean by "assign a variable name to a figure"? Do you want to use the contents of a variable as the filename? Do you want some kind of object which represents the "png" output (i.e. a numpy array)? Normally, one would just give the savefig a filename and that is the end of the story. Commented Aug 12, 2012 at 19:17
  • @pelson - sir, I was willing to save a file with a randomly selected unique name, every time the script is called. Exactly, I was willing to use the content of the variable as the filename. Commented Aug 13, 2012 at 21:35
  • Ok, thanks. Looks like @joaquin's answer is ideal then. :-) Commented Aug 14, 2012 at 7:08

1 Answer 1

4

The name of a file must be a string, not a number. Try:

kurs = '252'

Still it would be more convenient to use something line '252.png'.
If you are assigning the names from some kind of number generator you could do:

for i in range(10):
    ...................
    ...................
    kurs = "%i.png" % i
    plt.savefig(kurs, format='png')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you joaquin. You solved my problem! I am retrieving a random integer to serve as an identifier for the saved image. Now, I will convert it into string - thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.