Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:04:55+00:00 2026-06-09T21:04:55+00:00

I’m using numpy 1.6 and matplotlib 1.1.1, trying to generate a velocity field from

  • 0

I’m using numpy 1.6 and matplotlib 1.1.1, trying to generate a velocity field from a scalar field that I have. So far, I’m generating my scalar data as such:

    num_samples = 50
    dim_x = np.linspace(self.min_x, self.max_x,num_samples)
    dim_y = np.linspace(self.min_y, self.max_y,num_samples)
    X, Y = np.meshgrid(dim_x, dim_y)

    len_x = len(dim_x)
    len_y = len(dim_y)

    a = np.zeros([len_x, len_y], dtype=float)
    for i, y in enumerate(dim_y):
        for j, x in enumerate(dim_x):
            a[i][j] = x*y # not exactly my function, just an example

Then I get the gradient:

   (velx,vely) =  np.gradient(a)

From the numpy documentation, velx is the x component and vely is the y component of the vector field. Checking the docs for matplotlib, I use quiver to plot a vector field using arrows. It states that velx and vely are the x component and y component of the vector field:

    fig0 = plt.figure()
    ax = fig0.add_subplot(111)
    Q = ax.quiver(X,Y, velx, vely )
    plt.show()

This gives the wrong result for the velocity field:

Wrong result for the scalar field

The only way of the graph looks ok is if I invert the components on quiver:

    Q = ax.quiver(X,Y, vely, velx )#WHY???

Right result with the "wrong" expression

I’m suspecting that is something like row-or-column ordering, but I can’t figure it out if the output of np.gradient is inverted, or if quiver is inverted. All one dimensional problems are working as expected. Thanks!

EDIT: just to make even more clear how this is inverted, change the function

a[i][j] = x*y

to

a[i][j] = x*x

The gradient should be in the x direction, increasing with increasing x. The results are still wrong: if I use

Q = ax.quiver(X,Y, velx, vely )

I get

Still wrong

and if I invert it

Q = ax.quiver(X,Y, vely, velx )

I get

Right

Maybe there’s a more pythonic (and correct!) way to do it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:04:56+00:00Added an answer on June 9, 2026 at 9:04 pm

    I think you’re right, (this is an array ordering issue). a is built as a[yidx,xidx] but when you take the gradient, you do: velx, vely = np.gradient(a) when you should be doing vely, velx = np.gradient(a). Since the gradient along the 0th axis should give you vely (presumably d/dy(a) = vely)? — unless I’m missing something (in which case I’ll happily delete this answer).

    Also note that I think you can build “a” without the nested lists:

    a = X*Y
    

    which should work for more complicated functions as well…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.