保存pylab图形时,无法从图1.png更改名称。还标记问题

时间:2014-07-28 18:34:21

标签: python matplotlib

此处参考是我编写的代码

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt


'''

#Cycloid

for x in range(-1,2):
    for y in range(-1,2):
        a=x+y
        x0=.25*x
        y0=.25*y
        label="parametric curve x0= " + str(x0) + " y0 = " + str(y0) + " a = " + str(a)

        mpl.rcParams['legend.fontsize'] = 10

        fig = plt.figure()
        ax = fig.gca(projection='3d')
        t = np.linspace(-4 * np.pi, 4 * np.pi, 100)
        z = np.linspace(0, 0, 100)
        x = x0*np.cos(t) + (y0-a)*np.sin(t) + a*t
        y = -x0*np.sin(t) + (y0-a)*np.cos(t) + a

        ax.plot(x, y, z, label=label)
        ax.legend()

        plt.show()
'''     
#Epicycloid:
for x in range(1,5):
    for y in range(1,3):
        x0=.25*x
        y0=.25*y
        bigR=x
        littleR=y
        label="parametric curve x0= " + str(x0) + " y0 = " + str(y0) + " R = " + str(bigR) + " r = " + str(littleR)
        mpl.rcParams['legend.fontsize'] = 10

        fig = plt.figure()
        ax = fig.gca(projection='3d')
        t = np.linspace(-4 * np.pi, 4 * np.pi, 100)
        z = np.linspace(0, 0, 100)
        x = (x0 - (bigR+littleR))*np.cos(((bigR+littleR)*t)/littleR) - y0*np.sin(((bigR+littleR)*t)/littleR) + (bigR+littleR)*np.cos(t)
        y = (x0 - (bigR+littleR))*np.sin(((bigR+littleR)*t)/littleR) + y0*np.cos(((bigR+littleR)*t)/littleR) + (bigR+littleR)*np.sin(t)

        print label
        ax.plot(x, y, z, label=label)
        ax.legend()

        plt.show()
'''

#Hypocycloid


for x in range(1,5):
    for y in range(1,3):
        x0=.25*x
        y0=.25*y
        bigR=x
        littleR=y
        mpl.rcParams['legend.fontsize'] = 10
        label="parametric curve x0= " + str(x0) + " y0 = " + str(y0) + " R = " + str(bigR) + " r = " + str(littleR)


        fig = plt.figure()
        ax = fig.gca(projection='3d')
        t = np.linspace(-4 * np.pi, 4 * np.pi, 100)
        z = np.linspace(0, 0, 100)
        x = (x0 - (bigR-littleR))*np.cos(((bigR-littleR)*t)/littleR) + y0*np.sin(((bigR-littleR)*t)/littleR) + (bigR-littleR)*np.cos(t)
        y = -(x0 - (bigR-littleR))*np.sin(((bigR-littleR)*t)/littleR) + y0*np.cos(((bigR-littleR)*t)/littleR) + (bigR-littleR)*np.sin(t)

        ax.plot(x, y, z, label=label)
        ax.legend()

        plt.show()
        '''

执行此操作时,当我尝试保存时,数字的这个默认名称为figure_1.png。我根本无法改变这一点,我觉得它是某种与图形相关的标志,允许我更改名称。

我还有一个关于标签的问题。在浏览循环时,我尝试在图表上添加标签。有时我会得到好东西,我希望它只是循环中的值。

像这样

parametric curve x0= 0.5 y0 = 0.25 R = 2 r = 1

其他时候,当我这样做时,我得到这些值的数组,这有点让我复杂化

parametric curve x0= [ 0.125       0.23049781  0.56321913  0.90485414  1.01164805  0.7526084
  0.18649705 -0.46522957 -0.93492719 -1.05071906 -0.82584499 -0.44639605
 -0.16452912 -0.15641785 -0.42360131 -0.7923222  -1.01120832 -0.89321771
 -0.42322251  0.22848358  0.79454766  1.05233408  0.94115992  0.59058379
  0.24445312  0.12295637  0.29910677  0.65802308  0.96153031  0.98231426
  0.63334151  0.02079104 -0.6090592  -1.         -1.02156988 -0.73565547
 -0.35756612 -0.13339346 -0.20165422 -0.51581597 -0.87057291 -1.01731728
 -0.80487448 -0.26752503  0.38860955  0.89363608  1.05707581  0.86743272
  0.49349357  0.18696505  0.14050725  0.37978957  0.74930762  0.9997975
  0.92885265  0.49686272 -0.14604858 -0.73729438 -1.04097369 -0.9723949
 -0.63954365 -0.27892409 -0.12151145 -0.26309742 -0.61079705 -0.93531    -1.
 -0.69531907 -0.10406738  0.53887281  0.97043225  1.03880018  0.78175076
  0.40092877  0.14661277  0.17687301  0.46910735  0.83290674  1.01711751
  0.85182598  0.34660868 -0.30951851 -0.8467599  -1.05766613 -0.9060262
 -0.54172642 -0.21369617 -0.12931255 -0.33814288 -0.70436855 -0.98313896
 -0.9585836  -0.56705157  0.06276937  0.67533853  1.02353006  0.99932284
  0.68807754  0.31675769  0.125     ] y0 = 0.5 R = [ 0.5         0.92199126  2.25287651  3.61941656  4.0465922   3.01043361
  0.7459882  -1.86091829 -3.73970877 -4.20287624 -3.30337998 -1.78558421
 -0.65811647 -0.62567141 -1.69440526 -3.16928882 -4.04483328 -3.57287082
 -1.69289005  0.9139343   3.17819066  4.2093363   3.76463968  2.36233517
  0.97781246  0.49182548  1.19642709  2.63209233  3.84612122  3.92925704
  2.53336603  0.08316415 -2.43623678 -4.         -4.08627951 -2.94262186
 -1.43026448 -0.53357383 -0.80661688 -2.06326389 -3.48229163 -4.06926912
 -3.21949793 -1.0701001   1.55443818  3.57454433  4.22830325  3.46973089
  1.97397427  0.74786022  0.56202899  1.51915827  2.99723048  3.99918999
  3.71541061  1.98745088 -0.58419432 -2.94917752 -4.16389475 -3.88957961
 -2.55817461 -1.11569637 -0.48604581 -1.05238969 -2.44318819 -3.74124001
 -4.         -2.78127629 -0.41626952  2.15549125  3.88172902  4.15520072
  3.12700305  1.60371507  0.5864511   0.70749206  1.87642938  3.33162698
  4.06847002  3.40730392  1.38643471 -1.23807405 -3.3870396  -4.23066451
 -3.62410482 -2.16690567 -0.85478467 -0.51725019 -1.35257151 -2.81747419
 -3.93255583 -3.83433439 -2.26820627  0.25107748  2.70135412  4.09412022
  3.99729134  2.75231018  1.26703076  0.5       ] r = 2

如果有人能提出一些关于如何解决这些问题的提示,那将非常感激!

1 个答案:

答案 0 :(得分:1)

您如何尝试保存图片?此

fig.savefig("/tmp/epicycloid.png")

应将数字fig保存到文件/tmp/epicycloid.png中。请参阅下面的示例。

图例问题是因为在奇数图例中你的x0和y0是数组。之所以发生这种情况,是因为您将变量xy都重用为循环变量,然后再绘制在图中。如果您更改要使用的代码,例如xx作为循环变量:

for xx in range(1,5):
    for yy in range(1,3):
        x0=.25*xx
        y0=.25*yy
        bigR=xx
        littleR=yy
        mpl.rcParams['legend.fontsize'] = 10
        label="parametric curve x0= " + str(x0) + " y0 = " + str(y0) + " R = " + str(bigR) + " r = " + str(littleR)


        fig = plt.figure()
        ax = fig.gca(projection='3d')
        t = np.linspace(-4 * np.pi, 4 * np.pi, 100)
        z = np.linspace(0, 0, 100)
        x = (x0 - (bigR-littleR))*np.cos(((bigR-littleR)*t)/littleR) + y0*np.sin(((bigR-littleR)*t)/littleR) + (bigR-littleR)*np.cos(t)
        y = -(x0 - (bigR-littleR))*np.sin(((bigR-littleR)*t)/littleR) + y0*np.cos(((bigR-littleR)*t)/littleR) + (bigR-littleR)*np.sin(t)

        ax.plot(x, y, z, label=label)
        ax.legend()

        fig.savefig("/tmp/epicycloid{0:d}-{1:d}.png".format(xx,yy))

现在文件/tmp/epicycloid3-2.png看起来像:

enter image description here