Skip to content Skip to sidebar Skip to footer

Connect All 2d Points From Numpy 2d Arrays As A Triangular Meshgrid

I am pretty new to Python and I am trying to plot a triangular grid like this: import matplotlib.pyplot as plt import numpy as np r = 0.25 d = 2*r s = 0 l1 = np.array([[s,0

Solution 1:

triplot is made for that purpose: plotting triangles. You can either pass only x and y coordinates (in this case a Delaunay triangulation will be computed), or a full Triangulation object to which you can specify your own triangles.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.tri as mtri

r = 0.25
d = 2*r
s = 0defmeshgrid_triangles(n, m):
    """ Returns triangles to mesh a np.meshgrid of n x m points """
    tri = []
    for i inrange(n-1):
        for j inrange(m-1):
            a = i + j*(n)
            b = (i+1) + j*n
            d = i + (j+1)*n
            c = (i+1) + (j+1)*n
            if j%2 == 1:
                tri += [[a, b, d], [b, c, d]]
            else:
                tri += [[a, b, c], [a, c, d]]
    return np.array(tri, dtype=np.int32)


x0 = np.arange(4) * d
y0 = np.arange(5) * d
x, y = np.meshgrid(x0, y0)
x[1::2] -= r
triangles = meshgrid_triangles(4, 5)
triangulation = mtri.Triangulation(x.ravel(), y.ravel(), triangles)
plt.scatter(x, y, color='red')
plt.triplot(triangulation, 'g-h')

plt.show()

enter image description here

Solution 2:

Using the code the way you have (otherwise look at triplot_demo depending on what you want, as mentioned by @GBy), you can extract or rotate each array so that you just plot the line downwards:

import matplotlib.pyplot as plt
import numpy as np

r   = 0.25
d   = 2*r
s   = 0

l1 = np.array([[s,0], [s+d,0], [s+2*d,0], [s+3*d,0]])
l2 = np.array([[s-r,d], [s+r,d], [s+r+d,d], [s+r+2*d,d]])
l3 = np.array([[s,2*d], [s+d,2*d], [s+2*d,2*d], [s+3*d,2*d]])
l4 = np.array([[s-r,3*d], [s+r,3*d], [s+r+d,3*d], [s+r+2*d,3*d]])
l5 = np.array([[s,4*d], [s+d,4*d], [s+2*d,4*d], [s+3*d,4*d]])

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

larr = [l1,l2,l3,l4,l5]

# Plot horizontally
for l in larr:

  # same as your *zip(*l1), but you can select on a column-wise basis
  ax.errorbar(l[:,0], l[:,1], fmt="o", ls="-", color="black")

# Plot zig-zag-horizontally
for i in range(len(larr[0])):

  lxtmp = np.array([x[:,0][i] for x in larr])
  lytmp = np.array([x[:,1][i] for x in larr])

  ax.errorbar(lxtmp, lytmp, fmt="o", ls="-", color="black")

ax.set_ylim([-0.1,2.1])
ax.set_xlim([-0.6,1.6])

plt.show()

plot image

EDIT:

lxtmp = np.array([x[:,0][i] for x in larr])

So, x[:,0] means take all of the rows ":" but only the first column "0". For l1 it will return:

l1[:,0]array([ 0. ,  0.5,  1. ,  1.5])

which are the x-values for l1. Doing l1[:,1] will return all of the rows from column "1", the y-values. To draw the vertical lines, you want to take all the x and y values from each i-th array, and hence you loop over all the arrays, taking out the i-th element. For example, the 3rd vertical zig-zag line, would be:

lxtmp = [l1[:,0][2], l2[:,0][2], l3[:,0][2], l4[:,0][2], l5[:,0][2]]
lytmp = [l1[:,1][2], l2[:,1][2], l3[:,1][2], l4[:,1][2], l5[:,1][2]]

To simplify and run over each element, I created 'larr' to loop over and 'build' then in a normal python fashion, e.g.,

[i for i in range(1,10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Post a Comment for "Connect All 2d Points From Numpy 2d Arrays As A Triangular Meshgrid"