2

I'm trying to start 2D contour plot for a flow net and I'm having trouble getting the initial grid to show up properly.

Given the number of columns and the number of rows, how can I write a function that will plot a grid so that all points in the given range appear?

I tried plotting for 4 columns and 3 rows of points by doing this:

r = 3

c = 4

x = [i for i in range(c)]

y = [i for i in range(r)]

plot(x,y,'ro')

grid()

show()

and get this error:

'ValueError: x and y must have same first dimension'

So I tried testing it on a 4x4 grid and got this and I get close to what I want, however it only plots points (0,0), (1,1), (2,2), and (3,3)

However, I also want the points (0,0), (1,0), (2,0), (3,0), (1,0), (1,1)...(3,2), (3,3) to appear, as I will later need to plot vectors from this point indicating the direction of flow for my flow net.

Sorry, I know my terminology isn't that great. Does anyone know how to do this and how to make it work for grids that aren't square?

Donald Miner
  • 38,889
  • 8
  • 95
  • 118
bang
  • 181
  • 2
  • 3
  • 5
  • you don't need to write `x = [i for i in range(c)]`, you can write `x = range(c)` – YXD Mar 29 '12 at 10:20
  • Thanks for pointing that out. I did a lot of working beforehand and I think I had c = 4.0 and r = 3.0 and the range() function didn't work for floats. If anyone knows how to solve this problem for floats that would be great too! – bang Mar 29 '12 at 10:27
  • Your plot tries to plot a line when X vector are x-axis values and Y vector are y-axis values. Sizes of the two vectors have to match obviously. And that probably is not what is desired output of your code. – Fenikso Mar 29 '12 at 10:32

3 Answers3

7

import numpy as np
import matplotlib.pyplot as plt
import itertools
r = 3
c = 4
x = np.linspace(0, c, c+1)
y = np.linspace(0, r, r+1)

pts = itertools.product(x, y)
plt.scatter(*zip(*pts), marker='o', s=30, color='red')

X, Y = np.meshgrid(x, y)
deg = np.arctan(Y**3 - 3*Y-X)
QP = plt.quiver(X, Y, np.cos(deg), np.sin(deg))
plt.grid()
plt.show()

enter image description here

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
3
r = 3
c = 4

x = [i % c for i in range(r*c)]
y = [i / c for i in range(r*c)]

print x
print y

Gives:

[0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]

When used to draw graph as you did it produces desired result.

Fenikso
  • 9,251
  • 5
  • 44
  • 72
2

The first two arguments specify your x and y components. So the number of points must match. I think what you want is something like:

from itertools import product
import matplotlib.pyplot as plt

points = np.array(list(product(range(3),range(4))))

plt.plot(points[:,0],points[:,1],'ro')
plt.show()
YXD
  • 31,741
  • 15
  • 75
  • 115