Home > Blockchain >  Using matplotlib to draw a path given cartesian coordinates
Using matplotlib to draw a path given cartesian coordinates

Time:02-08

I'm new to matplotlib and numpy and am trying to plot some array data to something like this:

plotting an array

but it currently looks like this:

enter image description here

My code is extremely basic but basically the image above is what I'd like to be able to do. I'm not too concerned yet with drawing gridlines but the plot should be proportional. I've looked at some examples on the mathplotlib website but can't find something similar enough. I wonder if pyplot is not the correct procedure or if my array is not formatted correctly?

import numpy as np
from matplotlib import pyplot as plt

data = [(2, 2), (3, 2), (4, 1), (4, 0), (3, 0), (3, 1), (4, 2),
        (4, 3), (4, 4), (3, 3), (3, 4), (2, 4), (2, 3), (1, 4),
        (0, 4), (0, 3), (1, 3), (0, 2), (1, 2), (0, 1), (0, 0),
        (1, 0), (1, 1), (2, 0), (2, 1)]

data = np.asarray(data)
print(data)
plt.ylim(0, 4)
plt.xlim(0, 4)
plt.axis([0, 4, 0, 4])
plt.plot(data)
plt.show()

CodePudding user response:

Try

plt.plot(*data.transpose())

instead of

plt.plot(data)

The plt.plot function takes 2 arrays, one with all the x-values and one with all the y-values. You cannot pass it a mixture of x and y values. The transpose method will reorder the values so that all x values are together and all y values are togheter and the star * operator will unpack those arrays into tow separate function arguments.

CodePudding user response:

You need to unpack the x and y components into arrays like this:

plt.plot([p[0] for p in data], [p[1] for p in data], 'o-')

Use format style "o-": "o" for a symbol marker and "-" for a solid line.

To also format the x/y ticks here is the full code:

from matplotlib import pyplot as plt

data = [(2, 2), (3, 2), (4, 1), (4, 0), (3, 0), (3, 1), (4, 2),
        (4, 3), (4, 4), (3, 3), (3, 4), (2, 4), (2, 3), (1, 4),
        (0, 4), (0, 3), (1, 3), (0, 2), (1, 2), (0, 1), (0, 0),
        (1, 0), (1, 1), (2, 0), (2, 1)]

x = [p[0] for p in data]
y = [p[1] for p in data]
_, ax = plt.subplots()
ax.set_xticks(range(5))
ax.set_yticks(range(5))
ax.plot(x, y, 'o-', color='blue')
plt.grid() # add grid lines
plt.show()

Output: matplotlib image

CodePudding user response:

You could try something like this:

import numpy as np
from matplotlib import pyplot as plt

data = [(2, 2), (3, 2), (4, 1), (4, 0), (3, 0), (3, 1), (4, 2),
        (4, 3), (4, 4), (3, 3), (3, 4), (2, 4), (2, 3), (1, 4),
        (0, 4), (0, 3), (1, 3), (0, 2), (1, 2), (0, 1), (0, 0),
        (1, 0), (1, 1), (2, 0), (2, 1)]

for i in range(len(data) -1): 
  x1 = data[i][0]
  y1 = data[i][1]
  x2 = data[i 1][0]
  y2 = data[i 1][1]
  plt.plot([x1, x2], [y1, y2], c='blue')
  plt.scatter(x1, y1, c='blue')
plt.show()

CodePudding user response:

To complete other answers, here is the closest I got in a reasonable amount of time to your desired output :

import numpy as np
from matplotlib import pyplot as plt

data = [(2, 2), (3, 2), (4, 1), (4, 0), (3, 0), (3, 1), (4, 2),
        (4, 3), (4, 4), (3, 3), (3, 4), (2, 4), (2, 3), (1, 4),
        (0, 4), (0, 3), (1, 3), (0, 2), (1, 2), (0, 1), (0, 0),
        (1, 0), (1, 1), (2, 0), (2, 1)]

data = np.asarray(data)
print(data)

# make axis equal and turn them off
plt.axis('equal')
plt.axis('off')

for i in range(5):
    # horizontal and vertical background lines
    plt.plot([i,i], [0,4], linewidth=0.5, color='black')
    plt.plot([0,4], [i,i], linewidth=0.5, color='black')
plt.plot(data[:,0], data[:,1], 'b') # line segments
plt.scatter(data[:,0], data[:,1], c='b') # markers

plt.show()

Output:

output


Edit

From plot with annotations

  •  Tags:  
  • Related