制作闭合曲线

问题描述 投票:0回答:1

我有一组数据,如下所示:

0.0,0.1,0.0
0.0,0.09987714877467492,0.005050556279843527

0.0,0.09980450443077922,0.010092681565644489

0.0,0.09975499879273926,0.015109690654306573

0.0,0.09971073675921141,0.02012768105094637

0.0,0.0996747741672319,0.025141279807181033

0.0,0.0996853457866208,0.09532893107285735

我想绘制第三列与第二列的图,连接每两个连续的点,并制作一条闭合曲线。我可以绘制第三列与第二列,但是我尝试连接它们的代码,结果不是我想要的。附上一张图作为参考。 我使用的代码是


# Function to read points from file, focusing on the second and third columns
def read_points(filename):
    points = []
    with open(filename, "r") as file:
        for line in file:
            _, x, y = line.strip().split(',')
            points.append((float(x), float(y)))
    return points

# Read points from the file
points = read_points("Entrance.dat")

# Plotting points and connecting them with lines
plt.scatter(*zip(*points), color='red')  # Plots the points
plt.plot(*zip(*points), color='blue')    # Connects the points with a line to form a curve

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Curve from Data Points')

# Showing the plot
plt.grid(True)
plt.show()

你能帮我找到正确的方向吗?有什么链接我可以查看吗?我看到了这个页面: 将闭合曲线拟合到一组点

但这对我没有帮助。

the curve 曲线的问题在于它将点彼此连接起来,而不考虑顺序。

python curve-fitting
1个回答
0
投票

就是这个了

import matplotlib.pyplot as plt

# Data provided
data = [
    (0.0, 0.09987714877467492, 0.005050556279843527),
    (0.0, 0.09980450443077922, 0.010092681565644489),
    (0.0, 0.09975499879273926, 0.015109690654306573),
    (0.0, 0.09971073675921141, 0.02012768105094637),
    (0.0, 0.0996747741672319, 0.025141279807181033),
    (0.0, 0.0996853457866208, 0.09532893107285735)
]

# Extracting the second and third column data
x_data = [x for _, x, _ in data]
y_data = [y for _, _, y in data]

# Closing the curve by appending the first point at the end
x_data.append(x_data[0])
y_data.append(y_data[0])

# Plotting the curve
plt.scatter(x_data, y_data, color='red')  # Plots the points
plt.plot(x_data, y_data, color='blue')    # Connects the points with a line to form a closed curve

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Closed Curve from Data Points')

# Showing the plot
plt.grid(True)
plt.show()



© www.soinside.com 2019 - 2024. All rights reserved.