Search code examples
pythonarrayslines

Connect points from two different arrays in Python


I'm trying to connect the points from two arrays with lines. I only want to connect points in the same position (the first from stapts with the first from endpts), not all points.

Can anyone tell me how to do that? Many thanks. Below I'm only plotting two scatter plots.

import numpy as np
import matplotlib.pyplot as plt

# First, import data file into an array
gb_data = np.genfromtxt('gb_boundaries.txt', skip_header=10)


# Now plot the starting points of the system
staptsx = [gb_data[:, 15]]
staptsy = [gb_data[:, 16]]

endptsx = [gb_data[:, 17]]
endptsy = [gb_data[:, 18]]

plt.scatter(staptsx, staptsy)
plt.show()

plt.scatter(endptsx, endptsy)
plt.show()


Solution

  • I believe the following should work, drawing a single line for each pair of points. I'm unsure if this is the most efficient way to do it, but if you don't have too many points, you should be alright.

    toPlot = zip(staptsx, staptsy, endptsx, endptsy)
    for tuple in toPlot:
      plt.plot([tuple[0], tuple[2]], [tuple[1], tuple[3]], marker='o')
    plt.show()