Issue
I have a NumPy matrix of the form u(x,y) which represents the velocity of the fluid in the x-direction. For now, let us consider that u, x, and y as follows:
nx,ny = 101,101 # no_of_points
x = np.linspace(0,20,nx) # X domain span
y = np.linspace(0,3,ny) # Y domain span
u = np.random.randn(nx,ny)
I wanted to plot this matrix u at different x-location, like plotting u[0,:] (which represents u(x,y) at x=0 and at all y), u(10,:) (which represents u(x,y) at x=2 and at all y), u(20,:) (which represents u(x,y) at x=4 and at all y) and so on like the one shown in the figure below.
How can I achieve this using matplotlib? For now, I was able to develop a contour plot using:
X, Y = np.meshgrid(x, y)
plt.contourf(X, Y, u, levels=50, cmap='jet')
plt.colorbar()
which looks something like the image below
But, I am not interested in contour plots (or the quiver or streamline plots), rather I am looking for the one I have described above (same as the figure I have attached)..
Solution
You dummy example is a bit confusing as it is fully random.
Let's use another one that looks a bit more similar to what you want to achieve:
y = np.linspace(0,3,20)
x = np.sin(y)
t = np.linspace(0,10,5).reshape(-1,1)
X = x+t
Now we can loop over the time to plot the series:
ax = plt.subplot()
for my_x in X:
ax.plot(my_x,y, marker='o')
Alternatively, you can take advantage of pandas by converting to DataFrame
:
df = pd.DataFrame(X.T, index=y)
df.index.name = 'y'
df.columns.name = 'time'
df.plot()
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.