Python 保存pylab figure plot时,无法更改figure_1.png的名称。还有标签问题
这里有我写的代码供参考Python 保存pylab figure plot时,无法更改figure_1.png的名称。还有标签问题,python,matplotlib,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 l
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
如果有人能就如何解决这些问题提供一些建议,我们将不胜感激 如何保存图像?这个
fig.savefig("/tmp/epicycloid.png")
应将图fig
保存到文件/tmp/epiccycloid.png
。请参见下面的示例
图例问题是因为在奇数图例的情况下,x0和y0是数组。这是因为重复使用变量x
和y
作为循环变量,然后在绘图中使用。如果将代码更改为使用,例如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
看起来像:
我的建议:将此问题分为两个不同的问题。非常感谢您的精心撰写和深思熟虑的回答。