1

Beginning python/numpy user here. I do an analysis of a 2D function in the XY plane. Using 2 loops through x and y I compute the function value and store it into an array for later plotting. I ran into a couple of problems.

  1. Lets say my XY range is -10 to 10. How do I accommodate that when storing computed value into my data array? (only positive numbers are allowed as indices) For now I just add to x and Y to make it positive.
  2. From my data I know that the extreme is a x=-3 and y=2. When I plot the computed array first of all the axes labels are wrong. I would like Y to go the mathematical way. (up)
  3. I would like the axes labels to run from -10 to 10. I tried 'extend' but that did not come out right.
  4. Again from my data I know that the extreme is at x=-3 and y=2. In the plot when I hover the mouse over the graphics, the max value is shown at x=12 and y=7. Seems x and y have been swapped. Though when I move the mouse the displayed x and y numbers run as follows. X grows larger when moving the mouse right etc. (OK) Y runs the wrong way, grows larger when moving DOWN.
  5. As side note it would be nice to have the function value shown in the plot window as well next to x and y.

Here is my code:

size = 10
q = np.zeros((2*size,2*size))

for xs in range(-size,+size):
    for ys in range(-size,+size):
        q[xs+size,ys+size] = my_function_of_x_and_y(x,y)

im = plt.imshow(q, cmap='rainbow', interpolation='none')
plt.show()

One more thing. I would like not to mess with the q array too badly as I later want to find the extreme spot in it.

idxmin = np.argmin(q)
xmin,ymin = np.unravel_index(idxmin, q.shape)
xmin= xmin-size
ymin= ymin-size

So that I get this:

>>> xmin,ymin
(-3, 2)
>>> 

Here is my plot: my plot
(source: dyndns.ws)

Here is the desired plot (made in photoshop) (axis lineswould be nice): my plot
(source: dyndns.ws)

1 Answer 1

2

Not too sure why setting extend did not work for you but this is how I have implemented it

q = np.random.randint(-10,10, size=(20, 20))
im = plt.imshow(q, cmap='rainbow', interpolation='none',extent=[-10,10,-10,10])
plt.vlines(0,10,-10)
plt.hlines(0,10,-10)
plt.show()

Use vlines and hlines methods to set the centering line

enter image description here

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

2 Comments

On a side note, axhline and axvline are easier ways to get horizontal/vertical lines that span the entire (or a specified ratio of) axes. e.g. plt.axvline(0) will give you a vertical line a x=0 that will always span the entire vertical range of the axes, no matter how you zoom.
What about the 'rotation' of the representation of the array? Can someone point to a tutorial that explains how a np array is indexed and how that transforms when the array is used for a plot?

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.