python读取两份txt的所有坐标并按x坐标和y坐标分别绘制曲线图并导出为PDF
时间: 2024-03-27 21:37:40 浏览: 16
好的,根据您的需求,我提供以下代码供参考:
```python
import matplotlib.pyplot as plt
# 读取第一份txt文件的坐标
with open('file1.txt', 'r') as f1:
lines = f1.readlines()
x1 = []
y1 = []
for line in lines:
x, y = line.strip().split(',')
x1.append(float(x))
y1.append(float(y))
# 读取第二份txt文件的坐标
with open('file2.txt', 'r') as f2:
lines = f2.readlines()
x2 = []
y2 = []
for line in lines:
x, y = line.strip().split(',')
x2.append(float(x))
y2.append(float(y))
# 根据x坐标绘制曲线图
plt.plot(x1, y1, label='File 1')
plt.plot(x2, y2, label='File 2')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.savefig('x_curve.pdf')
plt.clf()
# 根据y坐标绘制曲线图
plt.plot(y1, x1, label='File 1')
plt.plot(y2, x2, label='File 2')
plt.xlabel('Y')
plt.ylabel('X')
plt.legend()
plt.savefig('y_curve.pdf')
plt.clf()
```
以上代码中,我们首先读取了两份txt文件的坐标,并将x坐标和y坐标分别存入不同的列表中。然后,我们使用Matplotlib库绘制了两张曲线图,一张是根据x坐标绘制的,另一张是根据y坐标绘制的。最后,我们使用savefig方法将绘制好的曲线图保存为PDF文件。
希望这个代码可以帮到您!