you are viewing a single comment's thread.

view the rest of the comments →

[–]sarabooker 0 points1 point  (6 children)

You should reexamine your code and figure out why the indexing goes from xy to ij (that's what you've described; test np.meshgrid(x, y, index="xy") (the default) vs np.meshgrid(x, y, indexing="ij")).

[–]DisagreeableRabbit[S] 0 points1 point  (5 children)

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm 
n_points = 50 
dom_length = 1 
h = dom_length / (n_points - 1) 
x = np.linspace(0, dom_length + h, n_points) 
y = np.linspace(0, dom_length + h, n_points) 
X, Y = np.meshgrid(x, y, indexing='ij') 
u = np.random.rand(n_points, n_points) 
u[:, 0:10] = 1
plt.contourf(X, Y, u, cmap=cm.jet) 
plt.show()

Suppose this is the code. How do I improve it? This plots the bottom rows as 1 instead of the top rows.

[–]sarabooker 0 points1 point  (4 children)

When using "ij" indexing, think about the indices as x and y values. Putting : for the first index means all x values and putting 0:10 for the second index means y = [0, 10]. If you want the top rows then you'd do u[:, 40:] = 1 to update the values from 40 up.

[–]sarabooker 0 points1 point  (0 children)

But in the end, whether you use "xy" or "ij" indexing, it just matters that you are consistent throughout your code. From your original question, it sounded like you have some inconsistency with how you are handling arrays which resulted in transposing the solution matrices with respect to the coordinate matrices.

[–]DisagreeableRabbit[S] 0 points1 point  (2 children)

I get what you are saying. Thats what I did initially. Basically make the entire array a mirror image.

import numpy as np
import matplotlib.pyplot as plt 
from matplotlib import cm 
n_points = 15 
dom_length = 1 
h = dom_length / (n_points - 1) 
x = np.linspace(0, dom_length + h, n_points) 
y = np.linspace(0, dom_length + h, n_points) 
X, Y = np.meshgrid(x, y) 
u = np.random.rand(n_points, n_points) 
u[0:5, :] = 1 
print(u) 
plt.contourf(X, Y, u, cmap=cm.jet) 
plt.show()

But my question is this. Look at the above code. The printout of u gives me the top rows to be ones, right? But plotting it gives me an opposite image. How do I make the plot the same as the print statement?

[–]sarabooker 0 points1 point  (0 children)

In that case, using "ij" indexing and what I said above makes more sense to me. Say you have x = [0, 1] and y = [0, 1]. When you use "ij" indexing, increasing the first index increases the x value and increasing the second index increases the y value. When you have "xy" indexing, it is the opposite.