3

What is an effective way to plot a list of coordinates in the form [(x1,y1),(x2,y2)], where each pair of coordinates is connected by a line (as shown below). I'd like to avoid use of non-core packages (core being pandas, matplotlib, numpy, ...)

Example:

c = [[(0, 4), (1, 3)],
     [(0, 4), (1, 5)],
     [(1, 3), (2, 2)],
     [(1, 3), (2, 4)],
     [(1, 5), (2, 4)],
     [(1, 5), (2, 6)],
     [(2, 2), (3, 1)],
     [(2, 2), (3, 3)],
     [(2, 4), (3, 3)],
     [(2, 4), (3, 5)],
     [(2, 6), (3, 5)],
     [(2, 6), (3, 7)]]

to be plotted as (with labels):

enter image description here

7
  • You said: "where each pair of coordinates is connected by a line". What is the rule of connecting coordinates? For example, you did not connect (0, 4) and (2, 4). Commented Nov 15, 2015 at 13:47
  • I think this might be helpful https://github.com/erocarrera/pydot Commented Nov 15, 2015 at 14:47
  • @dopstar: good question. There is no pair [(0,4),(2,4)], so we do not connect it with a line :) However, there is a pair [(0, 4), (1, 3)], so we do connect it (as shown). Should be fairly straight forward, but let me know if further clarity is needed. Commented Nov 15, 2015 at 15:00
  • 1
    I believe you're looking for something like this: matplotlib.org/users/path_tutorial.html Commented Nov 15, 2015 at 15:13
  • 1
    @EmilyHill Ok I get you. By the way your data is wrong (1, 5) does not connect to (2, 4) in the data but it is in the plot. Commented Nov 15, 2015 at 15:23

1 Answer 1

2

In terms of the 'effective way' I'm not sure. What follows is the working way which I think can be tuned/tweaked to be 'more effective'?

import matplotlib.pyplot as plt

c = [[(0, 4), (1, 3)],
     [(0, 4), (1, 5)],
     [(1, 3), (2, 2)],
     [(1, 3), (2, 4)],
     [(1, 5), (2, 4)],  # I changed this to match your plot
     [(1, 5), (2, 6)],
     [(2, 2), (3, 1)],
     [(2, 2), (3, 3)],
     [(2, 4), (3, 3)],
     [(2, 4), (3, 5)],
     [(2, 6), (3, 5)],
     [(2, 6), (3, 7)]]

fig = plt.figure()
ax = fig.add_subplot(111)

annotated = set()
for l in c:
    d = [[p[0] for p in l], [p[1] for p in l]]
    ax.plot(d[0], d[1], 'k-*')
    for p in l:
        annotated.add(p)

for p in annotated:
    ax.annotate(str(p), xy=p)

plt.xlim([0, 3.5])
plt.ylim([0, 8])
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Nicely engineered! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.