2

I'm looking for the correct data structure to use for the following. I want to plot a line between two points, for a multiple set of points.

For example, to plot a line between (-1, 1) and (12, 4) I do this:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1,1)

x1 = [-1, 12]
y1 = [1, 4]

ax.plot( x1, y1 )

plt.show()

If I want to plot another line connecting two different points (unrelated to the first set of points) I do this:

x2 = [1, 10]
y2 = [3, 2]

ax.plot( x1, y1, x2, y2 )

plot.show()

How do I extend this? That is, what data structure should I use to represent a growing array of such points [x1, y1, x2, y2, ...] generated by my input data?

I have tried the following,

points = [x1, y1, x2, y2]

ax.plot( points )

But the plot ends up connecting all of the individual lines with lines I do not want.

  • I've compared the efficiency of plotting many lines [in this answer](https://stackoverflow.com/questions/54492963/many-plots-in-less-time-python/54544965#54544965). – ImportanceOfBeingErnest Feb 28 '19 at 16:47

1 Answers1

1

You are close:

ax.plot(*points)

The asterisk operator * converts an iterable (the list in your case) into a sequence of function parameters.

DYZ
  • 55,249
  • 10
  • 64
  • 93