Python Forum
How to create meshgrid with non-integer stepsize of list elements?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create meshgrid with non-integer stepsize of list elements?
#1
I have 2 lists of x and y coordinate that are independently generated, with a/h amount of points between 0 and a.
    x = np.linspace(0, a, a/h)
    y = np.linspace(0, d, d/h)
when a/h is such that 0 increases to a in steps of integers i.e. [0,1,2,..,a]. It's nice because then the number of elements within the list can be used as indices. And as a result I can usually create a meshgrid such that a third list V1 can be associated with it.
    X, Y = plt.meshgrid(x, y)
    def potential(V1):
        return V1[X, Y]
where potential(V1) is now V1 corresponding to the meshgrid [x, y]. However I'm doing an assignment where I'm required to investigate how step-sizes affect my problem. As a result if I was to have a step-size of non-integers from 0 to a i.e. [0, 0.5, 1,...,a] Now I can't do what I did above since the indices are now non-integers. Raising the error
Error:
IndexError: arrays used as indices must be of integer (or boolean) type
How can I fix this so that I don't rely on the value of the element itself as the index of the elements, so that if there was a step-size of 0.25 between 0 to a for a list X say i.e.
    X = [0, 0.25, 0.75. 1.0] or x = np.linspace(0,1,4)
such that I can have
    x[0] = 0 corresponds to V[0]
    x[1] = 0.25 corresponds to V[1]
    x[2] = 0.75 corresponds to V[2]
    x[3] = 1 corresponds to V[3]
?
Reply
#2
Couldn't you kick it up a notch, so that all the values are integers? For example, x = np.linspace(0, a * h, h). It would require dividing by h elsewhere in your code, but it is doable.

Or you could do what your last example implies: x = [0, 0.25, 0.5, 0.75, 1.0], and then use the integers 0 through 4 for your indexes. You then translate the indexes using x.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 371 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 426 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  Checking if a string contains all or any elements of a list k1llcod3 1 1,023 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,428 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  How to change the datatype of list elements? mHosseinDS86 9 1,896 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,012 May-17-2022, 11:38 AM
Last Post: Larz60+
  how to easily create a list of already existing item CompleteNewb 15 3,377 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Change a list to integer so I can use IF statement buckssg 3 2,194 Sep-21-2021, 02:58 AM
Last Post: bowlofred
  Why am I getting list elements < 0 ? Mark17 8 3,026 Aug-26-2021, 09:31 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020