How to create meshgrid with non-integer stepsize of list elements? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to create meshgrid with non-integer stepsize of list elements? (/thread-16682.html) |
How to create meshgrid with non-integer stepsize of list elements? - dazzabaijan - Mar-10-2019 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 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]? RE: How to create meshgrid with non-integer stepsize of list elements? - ichabod801 - Mar-11-2019 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.
|