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

  • Home
  • SEARCH
  • 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 8104933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:01:23+00:00 2026-06-06T00:01:23+00:00

I have to create a 2D numpy array from the values x, y from

  • 0

I have to create a 2D numpy array from the values x, y from a function return to plot using contourf from matplotlib, and so far I’m using a “C” like structure, that it seems to be very inefficient in Python:

    dim_x = np.linspace(self.min_x, self.max_x, self.step)
    dim_y = np.linspace(self.min_y, self.max_y, self.step)
    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=complex)

    for i, y in enumerate(dim_y):
        for j, x in enumerate(dim_x):
            a[i][j] = aux_functions.final_potential(complex(x, y), element_list)

cs = plt.contourf(X, Y, (a.real), 100)

How can this be done in a more pythonic way?

Thanks!

  • 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-06T00:01:26+00:00Added an answer on June 6, 2026 at 12:01 am

    It would be ideal if you could rewrite final_potential as a vectorized function. A simple and, perhaps, too-obvious example:

    >>> dim_x = np.linspace(0, 2, 5)
    >>> dim_y = np.linspace(0, 2, 5)
    >>> X * Y
    array([[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
           [ 0.  ,  0.25,  0.5 ,  0.75,  1.  ],
           [ 0.  ,  0.5 ,  1.  ,  1.5 ,  2.  ],
           [ 0.  ,  0.75,  1.5 ,  2.25,  3.  ],
           [ 0.  ,  1.  ,  2.  ,  3.  ,  4.  ]])
    

    But if you really can’t do that, you could also vectorize:

    >>> np.vectorize(lambda x, y: x * y + 2)(X, Y)
    array([[ 2.  ,  2.  ,  2.  ,  2.  ,  2.  ],
           [ 2.  ,  2.25,  2.5 ,  2.75,  3.  ],
           [ 2.  ,  2.5 ,  3.  ,  3.5 ,  4.  ],
           [ 2.  ,  2.75,  3.5 ,  4.25,  5.  ],
           [ 2.  ,  3.  ,  4.  ,  5.  ,  6.  ]])
    

    In your case, it would probably look something like this:

    def wrapper(x, y): 
        return aux_functions.final_potential(complex(x, y), element_list)
    
    a = np.vectorize(wrapper)(X, Y)
    

    This will probably be a bit faster than nested for loops, although the overhead of a python function call will negate a lot of numpy’s efficiency. In tests I’ve done in the past, using vectorize provided a modest 5x speedup. (That’s compared to a 100x or 1000x speedup for pure numpy operations, as in the X * Y example.)

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

Sidebar

Related Questions

I would like to create a function that returns a numpy array if one
I have a numpy array like this: x = np.array([[1,2,3],[4,5,6],[7,8,9]]) I need to create
I have a NumPy array of values. I want to count how many of
I have a 2D numpy array. Is there a way to create a view
I have scipy and numpy, Python v3.1 I need to create a 1D array
I have an array of numbers and I'd like to create another array that
I have a Numpy array and a list of indices whose values I would
I have a numpy array containing: [1, 2, 3] I want to create an
I have a numpy array that I would like to share between a bunch
I'm using numpy to create a cube array with sides of length 100, thus

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.