【问题标题】:How to connect points to draw a line segment and also make a parallel line w.r.t. line segment?如何连接点以绘制线段并制作平行线 w.r.t.线段?
【发布时间】:2018-05-26 00:12:25
【问题描述】:

我想绘制一条穿过我的图形的所有端点(曲线的最大值)的线段,该线段是从 csv 文件中绘制的。对于这条线段,我还需要通过将一个点(已知)作为曲线上的参考来绘制一条平行于这条线段的线。

z,  x,  y
-40,0,0
-40,0.658,26.443
-40,1.316,47.128
-40,1.974,62.084
-40,2.632,73.336
-40,3.29,81.785
-40,3.948,87.501
-40,4.606,90.795
-40,5.264,92.491
-40,5.922,93.231
-40,6.58,93.41 - maximum value i.e end point of the curve
23,0,0
23,0.889,22.616
23,1.778,36.552
23,2.667,45.238
23,3.556,50.666
23,4.445,53.856
23,5.334,55.673
23,6.223,56.672
23,7.112,57.203
23,8.001,57.443
23,8.89,57.51- maximum value i.e end point of the curve
40,0,0
40,0.937,19.191
40,1.874,30.893
40,2.811,38.58
40,3.748,43.547
40,4.685,46.518
40,5.622,48.238
40,6.559,49.193
40,7.496,49.694
40,8.433,49.935
40,9.37,50.02- maximum value i.e end point of the curve

上面是我需要绘制的 CSV 文件,并提到了端点。我需要使用 Pandas 函数将所有端点与线 as in the image 连接起来,我尝试了以下代码来执行此操作。例如,平行线在任何曲线 w.r.t 上取一个点。指向要绘制的线,并且应该与第一条线平行。

import csv
from tkinter import filedialog
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import style
from mpldatacursor import datacursor

x=[]   # Initializing empty lists to store the 3 columns in csv
y=[]
z=[]
df = pd.DataFrame({'A' : []})

def readCSV(e):
        global df
        filename = filedialog.askopenfilename()
        df = pd.read_csv(filename, error_bad_lines=False)   #Reading CSV file using pandas
        read = csv.reader(df, delimiter = ",")
        fig = plt.figure()
        data_list = []
        ax= fig.add_subplot(111)
        df.set_index('x', inplace=True)  #Setting index
        df.groupby('z')['y'].plot(legend=True,ax=ax)   #grouping and plotting
        for line in ax.lines:
            xdata = line.get_xdata()
            ydata = line.get_ydata()
            s = line.append([6.58,8.89,9.37])
            r = line.append([93.41,57.51,50.02])
        ax.plot(s,r)
        ax.set_ylabel('y')
        ax.set_xlabel('x')
        ax.grid(True)
        plt.show()

【问题讨论】:

  • 开始你 - 你可以得到你绘制的每条线的最后坐标。 for line in ax.lines:line.get_xdata()。重复 y 数据并将这些数据的最后一个值附加到新列表中。绘制此图将为您提供连接现有线条末端的直线
  • 你能检查一下编辑过的程序吗?方法对吗?

标签: python-3.x pandas csv matplotlib


【解决方案1】:

要绘制一条连接图表端点的线,一种方法是获取每条线的最后一组坐标。这可以使用get_xdata()get_ydata() 来完成。这将返回所有值,但我们只需要 last 值。这可以使用切片符号[-1] 来完成:

my_list = [1,2,3,4,5]
print (my_list[-1])
# 5

所以你的代码会变成这样:

s = []
r = []

df = pd.read_csv("test.csv", error_bad_lines=False)   #Reading CSV file using pandas

fig = plt.figure()
data_list = []
ax= fig.add_subplot(111)
df.set_index('x', inplace=True)  #Setting index
df.groupby('z')['y'].plot(legend=True,ax=ax)   #grouping and plotting

for line in ax.lines:
    s.append(line.get_xdata()[-1])
    r.append(line.get_ydata()[-1])

ax.plot(s, r, color="black", linestyle=":")
ax.set_ylabel('y')
ax.set_xlabel('x')
ax.grid(True)

plt.show()

这给出了:

【讨论】:

  • 太棒了!如果我想在 '(m,n)' 的距离处制作一条平行线,这就是我正在尝试做的事情;最好的方法是偏移第一行还​​是找到两条线之间的距离并在该特定坐标处绘图
  • @Santo 如果它解决了您的问题,您可能会考虑接受它。 As described in here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多