gpt4 book ai didi

matplotlib - Quiver 2D 颜色图

转载 作者:行者123 更新时间:2023-12-04 17:43:16 25 4
gpt4 key购买 nike

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

现在我设法生成了我想要的箭头,但是颜色沿着 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com