如何使用相同的功能绘制和保存多个图表或图形?
how to plot and save multiple chart or figure using the same function?
我正在尝试使用相同的函数保存两个图:
``` plot_xy(x,y,x1,y1)```
``` plot_xy(x=x,y=y,x1=x2,y1=y2)```
但是,第二个输出覆盖了第一个输出,我怎样才能改变它来给我两个 save.fig 的数字?
示例代码如下:
import numpy as np
import matplotlib.pyplot as plt
def plot_xy(x,y,x1,y1,*args, **kwargs):
A = np.amax(x1)
fig, ax = plt.subplots()
ax.plot(x, y, 'r-', label='a' )
ax.plot(x1, y1, linestyle='none', marker='^', label = 'b')
ax.set(xlabel='cm', ylabel='(d/t)',
title='ABC')
ax.grid()
ax.set_ylim(ymin=0)
ax.set_xlim(xmin=0)
ax.set_ylim(ymax=1)
ax.set_xlim(xmax=A)
plt.legend(loc = 'best')
plt.show()
fig.savefig(r'C:\...location....\test.png')
return fig
if __name__ == "__main__":
"""passing a rough estimated data"""
print (0)
x = np.linspace(0,500, num=20 )
y = np.linspace(0,0.85, num=20 )
x1 = np.linspace(0,1000, num=20 )
y1 = np.linspace(0,1000, num=20 )
x2 = np.linspace(0,2000, num=20 )
y2 = np.linspace(0,1, num=20 )
plot_xy(x,y,x1,y1)
plot_xy(x=x,y=y,x1=x2,y1=y2)
谢谢,感谢您的努力。
这是因为 file name
是相同的。所以以前的文件被覆盖了。您可以通过创建新文件名来处理此问题。您可以使用 current seconds
来确保它是不同的。或者您可以使用任何其他类型,以便每次生成的文件名都不同。
import numpy as np
import matplotlib.pyplot as plt
import datetime
def plot_xy(x,y,x1,y1,*args, **kwargs):
A = np.amax(x1)
fig, ax = plt.subplots()
ax.plot(x, y, 'r-', label='a' )
ax.plot(x1, y1, linestyle='none', marker='^', label = 'b')
ax.set(xlabel='cm', ylabel='(d/t)',
title='ABC')
ax.grid()
ax.set_ylim(ymin=0)
ax.set_xlim(xmin=0)
ax.set_ylim(ymax=1)
ax.set_xlim(xmax=A)
plt.legend(loc = 'best')
plt.show()
fig.savefig(r'C:\...location....\test_' + str(datetime.datetime.now().strftime('%f'))+'.png')
return fig
if __name__ == "__main__":
"""passing a rough estimated data"""
print (0)
x = np.linspace(0,500, num=20 )
y = np.linspace(0,0.85, num=20 )
x1 = np.linspace(0,1000, num=20 )
y1 = np.linspace(0,1000, num=20 )
x2 = np.linspace(0,2000, num=20 )
y2 = np.linspace(0,1, num=20 )
plot_xy(x,y,x1,y1)
plot_xy(x=x,y=y,x1=x2,y1=y2)
这是因为您对函数的每次调用都使用了相同的图形保存位置。您可以使用另一个参数来确定图形的保存位置。像这样:
import numpy as np
import matplotlib.pyplot as plt
def plot_xy(filename,x,y,x1,y1,*args, **kwargs):
A = np.amax(x1)
fig, ax = plt.subplots()
ax.plot(x, y, 'r-', label='a' )
ax.plot(x1, y1, linestyle='none', marker='^', label = 'b')
ax.set(xlabel='cm', ylabel='(d/t)',
title='ABC')
ax.grid()
ax.set_ylim(ymin=0)
ax.set_xlim(xmin=0)
ax.set_ylim(ymax=1)
ax.set_xlim(xmax=A)
plt.legend(loc = 'best')
plt.show()
fig.savefig(f"{filename}.png")
if __name__ == "__main__":
"""passing a rough estimated data"""
print (0)
x = np.linspace(0,500, num=20 )
y = np.linspace(0,0.85, num=20 )
x1 = np.linspace(0,1000, num=20 )
y1 = np.linspace(0,1000, num=20 )
x2 = np.linspace(0,2000, num=20 )
y2 = np.linspace(0,1, num=20 )
plot_xy("image1",x,y,x1,y1)
plot_xy("image2",x=x,y=y,x1=x2,y1=y2)
我正在尝试使用相同的函数保存两个图:
``` plot_xy(x,y,x1,y1)```
``` plot_xy(x=x,y=y,x1=x2,y1=y2)```
但是,第二个输出覆盖了第一个输出,我怎样才能改变它来给我两个 save.fig 的数字?
示例代码如下:
import numpy as np
import matplotlib.pyplot as plt
def plot_xy(x,y,x1,y1,*args, **kwargs):
A = np.amax(x1)
fig, ax = plt.subplots()
ax.plot(x, y, 'r-', label='a' )
ax.plot(x1, y1, linestyle='none', marker='^', label = 'b')
ax.set(xlabel='cm', ylabel='(d/t)',
title='ABC')
ax.grid()
ax.set_ylim(ymin=0)
ax.set_xlim(xmin=0)
ax.set_ylim(ymax=1)
ax.set_xlim(xmax=A)
plt.legend(loc = 'best')
plt.show()
fig.savefig(r'C:\...location....\test.png')
return fig
if __name__ == "__main__":
"""passing a rough estimated data"""
print (0)
x = np.linspace(0,500, num=20 )
y = np.linspace(0,0.85, num=20 )
x1 = np.linspace(0,1000, num=20 )
y1 = np.linspace(0,1000, num=20 )
x2 = np.linspace(0,2000, num=20 )
y2 = np.linspace(0,1, num=20 )
plot_xy(x,y,x1,y1)
plot_xy(x=x,y=y,x1=x2,y1=y2)
谢谢,感谢您的努力。
这是因为 file name
是相同的。所以以前的文件被覆盖了。您可以通过创建新文件名来处理此问题。您可以使用 current seconds
来确保它是不同的。或者您可以使用任何其他类型,以便每次生成的文件名都不同。
import numpy as np
import matplotlib.pyplot as plt
import datetime
def plot_xy(x,y,x1,y1,*args, **kwargs):
A = np.amax(x1)
fig, ax = plt.subplots()
ax.plot(x, y, 'r-', label='a' )
ax.plot(x1, y1, linestyle='none', marker='^', label = 'b')
ax.set(xlabel='cm', ylabel='(d/t)',
title='ABC')
ax.grid()
ax.set_ylim(ymin=0)
ax.set_xlim(xmin=0)
ax.set_ylim(ymax=1)
ax.set_xlim(xmax=A)
plt.legend(loc = 'best')
plt.show()
fig.savefig(r'C:\...location....\test_' + str(datetime.datetime.now().strftime('%f'))+'.png')
return fig
if __name__ == "__main__":
"""passing a rough estimated data"""
print (0)
x = np.linspace(0,500, num=20 )
y = np.linspace(0,0.85, num=20 )
x1 = np.linspace(0,1000, num=20 )
y1 = np.linspace(0,1000, num=20 )
x2 = np.linspace(0,2000, num=20 )
y2 = np.linspace(0,1, num=20 )
plot_xy(x,y,x1,y1)
plot_xy(x=x,y=y,x1=x2,y1=y2)
这是因为您对函数的每次调用都使用了相同的图形保存位置。您可以使用另一个参数来确定图形的保存位置。像这样:
import numpy as np
import matplotlib.pyplot as plt
def plot_xy(filename,x,y,x1,y1,*args, **kwargs):
A = np.amax(x1)
fig, ax = plt.subplots()
ax.plot(x, y, 'r-', label='a' )
ax.plot(x1, y1, linestyle='none', marker='^', label = 'b')
ax.set(xlabel='cm', ylabel='(d/t)',
title='ABC')
ax.grid()
ax.set_ylim(ymin=0)
ax.set_xlim(xmin=0)
ax.set_ylim(ymax=1)
ax.set_xlim(xmax=A)
plt.legend(loc = 'best')
plt.show()
fig.savefig(f"{filename}.png")
if __name__ == "__main__":
"""passing a rough estimated data"""
print (0)
x = np.linspace(0,500, num=20 )
y = np.linspace(0,0.85, num=20 )
x1 = np.linspace(0,1000, num=20 )
y1 = np.linspace(0,1000, num=20 )
x2 = np.linspace(0,2000, num=20 )
y2 = np.linspace(0,1, num=20 )
plot_xy("image1",x,y,x1,y1)
plot_xy("image2",x=x,y=y,x1=x2,y1=y2)