matplotlib - Quiver 2D 颜色图

标签 matplotlib

我正在绘制一个在原点处具有奇点的偶极子场。 因此,我想对箭头进行颜色编码以指示场的强度。

现在我设法生成了我想要的箭头,但是颜色沿着 theta 轴而不是沿着 r 轴:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.cm as cm
from matplotlib.colors import Normalize

fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')

n=30
m=8

thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3

m = np.meshgrid(thetas,radii)
#This is where one should define m such that it results in the color coding I want. Unfortunately, I am not completely sure how the color is decoded in the quiver function.

ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), m, pivot='mid')
plt.show()

enter image description here

我希望箭头在原点附近变暗,随着距原点的距离 r=sqrt (x^2+y^2) 的增加而变亮。

最佳答案

好的,感谢@ImportanceOfBeingEarnest 的评论,我可以回答如下问题:quiver 函数中的 C 参数可以只采用绘图坐标的函数。因此,只需在 quiver 函数中添加“r”,如下所示:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')

n=30
m=8

thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
#we leave out the 1/r**3 part because it would make our arrows infinitely long near the origin.
#Instead we use a colormap to indicate the strength of the field as follows

ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), r, pivot='mid', cmap='YlGnBu_r')
plt.show()

结果如下: enter image description here

cmap 命令根据cmap YlGnBu_r 使颜色编码出现。

这里给出更多颜色编码图: http://matplotlib.org/examples/color/colormaps_reference.html 和这里 http://matplotlib.org/users/colormaps.html .

关于matplotlib - Quiver 2D 颜色图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53450003/

相关文章:

python - Matplotlib : display array values with imshow

python条形图不居中

python - AttributeError:未知的属性密度

python - 如何在 Cartopy 投影中绘制圆的半径

python - 修改 Matplotlib 中绘制线的属性

python - 绘制 3D numpy 数组的第三轴

python - 如何知道 matplotlib 安装启用的功能?

python - 在 Windows 中安装 MatPlotLib

annotations - matplotlib的同一个注释中的不同字体大小?

python - 在 matplotlib 中绘制 unix 时间戳