# How to plot an array of lines?

I have the data stored in the textfile like this:

0.5 0.5 -0.7 -0.8

0.51 0.51 -0.75 -0.85

0.6 0.1 0.1 1.00

and so on

4 numbers in each row. Two first numbers means coordinates (x0,y0), two last - (x1,y1). This determines the coordinates of a line. So, the first row tells, that I have a line starting from (0.5, 0.5) and finishing in (-0.7, -0.8). The aim is to plot all of these lines. How can I do this? Explain it for beginner, please.

• Well you’ll have to import the data first. What format is it in? Then presuming you get it in some form like {{{x1,y1},{x2,y2}},....} you can just use Line in a Graphics. – CA Trevillian Dec 12 '20 at 22:29

You can use "Table" to keep the structure of your data. And then you define a function Line[{{#1, #2}, {#3, #4}}]& to draw the Line by two points {#1,#2} and {#3,#4}.

0.5 0.5 -0.7 -0.8
0.51 0.51 -0.75 -0.85
0.6 0.1 0.1 1.00

data = Import["data.txt", "Table"]

(* {{0.5, 0.5, -0.7, -0.8}, {0.51, 0.51, -0.75, -0.85}, {0.6, 0.1, 0.1,
1.}} *)

Graphics[Line[{{#1, #2}, {#3, #4}}] & @@@ data] • Thank you for your guidance! This is a good way to solve this problem indeed! – Михаил Кочубей Dec 12 '20 at 23:40
data = Import["/Users/roberthanlon/Downloads/lines.txt", "Data"]

(* {{0.5, 0.5, -0.7, -0.8}, {0.51, 0.51, -0.75, -0.85}, {0.6, 0.1, 0.1, 1.}} *)

Graphics[Line /@ (Partition[#, 2] & /@ data), Axes -> True] Highlight any function or operator and press F1 for help.

• Thank you a million for you prompt reply! You really helped me out! – Михаил Кочубей Dec 12 '20 at 23:37

This is another way to do it:

data = ImportString[
"0.5 0.5 -0.7 -0.8
0.51 0.51 -0.75 -0.85
0.6 0.1 0.1 1.00", "Table"]

Graphics[
Table[
Line[Partition[row, 2]],
{row, data}
]
]


Try each piece of the code separately to see what it does (this applies to all situations when you are trying to understand some code). For example, try these and observe the output:

Partition[{0.5, 0.5, -0.7, -0.8}, 2]

Table[
f[row],
{row, data}
]

Table[
Line[Partition[row, 2]],
{row, data}
]