用线连接点打印

2024-03-28 22:28:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个程序来确定某个高度的最小、最大和百分位风速。文件分为5列。到目前为止,我的代码如下所示:

import matplotlib.pyplot as plt
f = open('wind.txt', 'r')
for line in f.readlines():
    line = line.strip()
    columns = line.split()
    z = columns[0]
    u_min = columns[1]
    u_10 = columns[2]
    u_max = columns[4]
    u_90 = columns[3]

    plt.plot(u_min,z,'-o')
    plt.plot(u_max,z,'-o')

This shows the max and mins of the wind节目()

正如你所看到的,它用一个点绘制了特定高度上的每一个最小值和最大值。我如何调整它,使之成为一条线。你知道吗


Tags: columns文件代码import程序高度plotmatplotlib
2条回答

因评论而编辑的答案

要创建连接所有最小值的线,请执行以下操作:

  1. 将所有最小值存储在列表中(使用append)
  2. 绘制列表

代码:

import matplotlib.pyplot as plt
f = open('wind.txt', 'r')
min_vector = []
max_vector = []
z_vector = []
for line in f.readlines():
    line = line.strip()
    columns = line.split()
    z = columns[0]
    u_min = columns[1]
    u_10 = columns[2]
    u_max = columns[4]
    u_90 = columns[3]

    min_vector.append(u_min)
    max_vector.append(u_max)
    z_vector.append(z)

plt.plot(min_vector, z_vector, '-o')
plt.plot(max_vector, z_vector, '-o')

如果只需要从左散点图的顶部到底部的一行,可以这样做:

plot([u_min[0], u_min[-1], [z[0], z[-1], color='k', linestyle='-', linewidth=2)

相关问题 更多 >