python - 如何使用相同的功能绘制和保存多个图表或图形?
问题描述
我正在尝试使用相同的功能保存两个图:
``` 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)
推荐阅读
- linux - 删除特定文件并保留一些
- ios - 无法从 TestFlight 向应用发送通知
- android - 为什么 gradle 构建过程会去掉我的 Application 和 Activity 类?
- powershell - 从数组中提供 Get-ChildItem 路径信息 - 非法字符
- c# - 有没有办法为 IIS 提供 MVC 解决方案所依赖的 DLL 的位置?
- javascript - 如何从 Storybook 中的样式组件渲染全局样式?
- google-cloud-platform - 需要说明如何使用 Terraform 管理 Google Cloud 项目
- events - Magento 2 - 事件'sales_order_place_after'的观察者不起作用
- python-3.x - 在 Python3 中同时进行 10 000 个连续 opencv 操作的最佳并发方式是什么?
- javascript - 按下联系表单中的按钮后,如何重新加载页面并清除输入值?