Python Forum

Full Version: How to create meshgrid with non-integer stepsize of list elements?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]
?
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.