简体   繁体   中英

How to assign a variable name to matplotlib figure?

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM