8

I'm trying to plot some points over a contour using Matplotlib.

I have scalar field from which I want to plot the contour. However, my ndarray has a dimension 0 x 20, but my real space varies from -4 to 4.

I can plot this contour using this piece of code:

x, y = numpy.mgrid[-4:4:20*1j, -4:4:20*1j]

# Draw the scalar field level curves
cs = plt.contour(scalar_field, extent=[-4, 4, -4, 4])
plt.clabel(cs, inline=1, fontsize=10)

The problem is 'cause I have to plot some points over this plot, and this points are obtained using the ndarray, i.e., I get points varying as this array dimension.

I tried to plot these points using this code:

def plot_singularities(x_dim, y_dim, steps, scalar_field, min_points, max_points, file_path):
    """
    :param x_dim : the x dimension of the scalar field
    :param y_dim : the y dimension of the scalar field
    :param steps : the discretization of the scalar field
    :param file_path : the path to save the data
    :param scalar_field : the scalar_field to be plot
    :param min_points : a set (x, y) of min points of the scalar field
    :param max_points : a set (x, y) of max points of the scalar field
    """
    min_points_x = min_points[0]
    min_points_y = min_points[1]
    max_points_x = max_points[0]
    max_points_y = max_points[1]

    plt.figure()

    x, y = numpy.mgrid[-x_dim:x_dim:steps*1j, -y_dim:y_dim:steps*1j]

    # Draw the scalar field level curves
    cs = plt.contour(scalar_field, extent=[-x_dim, x_dim, -y_dim, y_dim])
    plt.clabel(cs, inline=1, fontsize=10)

    # Draw the min points
    plt.plot(min_points_x, min_points_y, 'ro')

    # Draw the max points
    plt.plot(max_points_x, max_points_y, 'bo')

    plt.savefig(file_path + '.png', dpi=100)
    plt.close()

But I got this image:

enter image description here

Which is not correct.

If I change this line:

cs = plt.contour(scalar_field, extent=[-x_dim, x_dim, -y_dim, y_dim])

For that one:

cs = plt.contour(scalar_field)

enter image description here

I get the desired behavior, but the extents doesn't show my real data space, but the ndarray dimension.

At last, if I don't plot these points (comment the plot() lines), I can the extents that I want:

enter image description here

But I have to plot the points. Both data are in the same space. But the contour() function allows me to specify the grid. I could found a manner to do this when plotting the points.

How can I properly set the extents?

2
  • It would help to know what you were expecting. What should the "data space" be? Are both the points and contour incorrect, or just the contour? Commented Jul 11, 2014 at 23:34
  • If you look to the second image, it is correct (the plots). However is showing the extents as the ndarray dimensions (from 0 to 20). I would like to show it varying from an my data space (with the center in 0,0 and varying from -4 to 4). I call my "data space" as an analogy to the CG concepts to image space (ndarray) and world space (my real data space). Commented Jul 11, 2014 at 23:41

2 Answers 2

5

If you don't provide x and y data corresponding to the scalar field, contour uses integer values up to the size of the array. That is why the axes are displaying the dimension of the array. The parameters extent should give the minimum and maximum x and y values; I assume this is what you mean by "data space." So the call to contour would be:

contour(scalar_field,extent=[-4,4,-4,4])

This can be reproduced by specifying x and y data:

contour(numpy.linspace(-4,4,20),numpy.linspace(-4,4,20),scalar_field)

Then the contour looks exactly as in your first plot. I assume the reason this is incorrect because the min and max points are not in the right places. Based on the info you gave, this is because min_points and max_points which you pass to your function are indices to the array scalar_field, so they correspond to integers, not the actual x and y values. Try to use these indices to access the x and y points by defining:

x=numpy.linspace(-4,4,20)
y=numpy.linspace(-4,4,20)

For example, if you have a min point of (0,1), it would correspond to (x[0], y[1]). I think a similar thing can be done with the mgrid, but I've never used that myself.

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

1 Comment

Thank you! As I have just right now written for @Ajean, I know that these were indices and that this was the problem (sorry if I couldn't express myself in a good way). I was looking for a clever way to do this conversion in Matplolib/Python. Thank you very much!
0

You want to plot both the contour and the points in the real data space, yes? plt.contour will take x and y values associated with the 2d array you have, and will plot on axes correctly.

xvals = -x_dim:x_dim:step  # I'm not sure about these ... but you get the idea
yvals = -y_dim:y_dim_step
plt.contour(xvals, yvals, scalar_field)

6 Comments

If I just do this, I get the first image, which do not respect the space that I want when I plot the points.
Are you sure that the points you are plotting are in the right space, then? It looks like you're plotting actual values with one thing and indices with the other.
They are both in the same space. I change my extent as a parameter when plotting the contour, but I don't know how to do this when plotting the points.
I think there is a fundamental misunderstanding here ... when you plot points, they are (x,y) values, and plot will put them right where they are, no matter what. A 2D array to be displayed (via contour) has no inherent (x,y) positions associated with it, which is why you need to specify them (either by extent or by the xvals and yvals above). If your x-y points being plotted by plot are truly in the x_dim/y_dim space, (-4 to 4), this will work. It looks like they are indices.
Notice that the 4 max points are at 0-0,0-19,19-0,19-19, and your array is length 20.
|

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.