Matplotlib 箭袋二维彩色地图

Matplotlib 箭袋二维彩色地图,matplotlib,Matplotlib,我正在绘制一个偶极子场,它在原点有一个奇点。 因此,我想对箭头进行颜色编码,以指示磁场的强度 现在我设法制作出我想要的箭头,但颜色沿着θ轴而不是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

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

现在我设法制作出我想要的箭头,但颜色沿着θ轴而不是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()


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

好的,多亏@ImportanceOfBeingEarnest的评论,我可以回答如下问题:quiver函数中的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()
结果如下:

cmap命令根据cmap YlGnBu_r显示颜色编码

此处提供了更多颜色编码图: 这里呢
.

我不知道为什么在这里使用
m
也行,但我想你应该用
r
来代替它。哈哈,哇,谢谢@ImportanceOfBeingErnest-我不知道这么简单。您知道如何将颜色编码更改为如cm.copper之类的地图吗?添加
cmap=“copper”
?哦…凯,谢谢!很抱歉,我感到困惑,因为我在另一个示例中看到cmap和color一起定义,然后在color参数中作为“color=colormap(norm(colors))”提供给plot函数,所以我认为在这里也必须这样做,但它不起作用。再次感谢。如果你把你的评论写成答案,我会接受的。你需要像
color=colormap(norm(colors))
这样的东西,以防你使用
color
参数。但是在这里您使用了
C
参数,因此对于任何ScalarMappable(如scatter、imshow等)来说,一切都可以正常工作。也许你可以自己写下来作为答案?