箭袋2D色彩图

时间:2018-11-23 16:19:28

标签: 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)增大而变亮。

1 个答案:

答案 0 :(得分:0)

好吧,多亏@ImportanceOfBeingEarnest的评论,我可以回答如下问题:颤动函数中的C参数可以仅接受绘图坐标的函数。因此,只需在颤音函数中添加“ 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

相关问题