Matplotlib-在streamplot()中使用一维数组 [英] Matplotlib - Using 1-D arrays in streamplot()

查看:27
本文介绍了Matplotlib-在streamplot()中使用一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我发现这里以前有两个类似的问题:

<小时>

将 numpy 导入为 np导入matplotlib.pyplot作为plt导入scipy.interpolate作为插值# 小写变量是一维数组x = np.array([1,2,3,4,5])y = np.array([3,1,5,1,3])u = np.array([1,1,0,-1,-1])v = np.array([-0.5,1,-1,1,-0.5])# 大写的变量是二维数组xi = np.linspace(x.min(), x.max(), 100)yi = np.linspace(y.min(), y.max(), 100)X, Y = np.meshgrid(xi, yi)U = interpolate.griddata((x, y), u, (X, Y), method='nearest')V = interpolate.griddata((x,y),v,(X,Y),method ='nearest')plt.figure()plt.quiver(x, y, u, v, scale_units='xy',angle='xy', scale=1.5)plt.streamplot(X, Y, U, V, color=U**2+V**2, linewidth=2, cmap=plt.cm.autumn)plt.show()

收益

So I have found two previous similar questions asked here:

How to use streamplot function when 1D data of x-coordinate, y-coordinate, x-velocity and y-velocity are available?

How to plot Streamlines with Matplotlib given 1-D arrays of X cords, Y cords, U components and V components

The first question deals with arrays of different sizes (which isn't my case, X, Y, U and V will always be of the same length in my example) while the second does provide some more headway becomes incomprehnsible later on in the question and doesn't provide a solution.

Moving onto my problem I have 4, 1-D arrays, the X coordinates and Y coordinates of where each vector is and then the respective U and V values for each vector. I am trying to visualise the vector field (which I can visualise correctly in .quiver) as a streamline visualization using streamplot but I encounter the problem of making U and V 2D. I don't fully understand what the second dimension needs to contain for U and V so any clarification (and code ideally would be great).

The only code I could provide is my implementation of the second link but that doesn't work for me so would be obselete.

解决方案

Use griddata (see also scipy.interpolate.griddata) to interpolate 1D data to a 2D grid.

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interpolate

# lowercase variables are 1D arrays
x = np.linspace(0, 2 * np.pi, 10)
y = np.sin(x)
u = np.cos(x)
v = np.sin(x)

# capitalized variables are 2D arrays
xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)
X, Y = np.meshgrid(xi, yi)
U = interpolate.griddata((x, y), u, (X, Y), method='cubic')
V = interpolate.griddata((x, y), v, (X, Y), method='cubic')

plt.figure()
plt.quiver(x, y, u, v, scale_units='xy', angles='xy', scale=1.5)
plt.streamplot(X, Y, U, V, color=U**2+V**2, linewidth=2, cmap=plt.cm.autumn)
plt.show()


import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interpolate

# lowercase variables are 1D arrays
x = np.array([1,2,3,4,5])
y = np.array([3,1,5,1,3])
u = np.array([1,1,0,-1,-1])
v = np.array([-0.5,1,-1,1,-0.5])

# capitalized variables are 2D arrays
xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)
X, Y = np.meshgrid(xi, yi)
U = interpolate.griddata((x, y), u, (X, Y), method='nearest')
V = interpolate.griddata((x, y), v, (X, Y), method='nearest')

plt.figure()
plt.quiver(x, y, u, v, scale_units='xy', angles='xy', scale=1.5)
plt.streamplot(X, Y, U, V, color=U**2+V**2, linewidth=2, cmap=plt.cm.autumn)
plt.show()

yields

这篇关于Matplotlib-在streamplot()中使用一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆