gpt4 book ai didi

python - 更改 Matplotlib 流图箭头的 FaceColor 和 EdgeColor

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:48 25 4
gpt4 key购买 nike

我在网格中有一些数据,我用 streamplot 绘制流线图,颜色和宽度与速度相关。如何更改箭头的颜色或仅更改边缘颜色?我的目标是强调流向。如果有人有其他方法可以做到这一点..

我尝试使用c.arrows,编辑c.arrows.set_edgecolor,c.arrows.set_edgecolors, c.arrows.set_facecolorc.arrows.set_facecolors 没有任何反应,即使我运行 plt.draw()

图: The Result of the code

代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-10,-1, 50)
y = np.linspace(-30, -28, 50)

xi, yi = np.meshgrid(x,y)

u = 3*np.cos(xi)*((-3)*np.sin(yi))**3
v = 2*np.sin(xi)*3*np.cos(yi)
speed = np.sqrt((u**2)+(v**2))

lw = 4*speed/speed.max()

plt.ion()
plt.figure()
plt.plot(xi,yi,'-k',alpha=0.1)
plt.plot(xi.T,yi.T,'-k',alpha=0.1)
c = plt.streamplot(xi,yi,u,v,linewidth=lw,color=speed)

最佳答案

(请注意,下面的分析可能不完全正确,我只是粗略地查看了源代码。)

似乎 streamplot 在创建箭头时做了两件事:

  • 向轴添加箭头补丁(类型FancyArrowPatch)
  • 将相同的箭头补丁添加到 PatchCollection (c.arrows)

出于某种原因(我想得到正确的缩放是这背后的原因)集合似乎没有被使用并且没有被添加到轴上。所以,如果你改变颜色图或集合的颜色,它对绘图没有影响。

可能有更漂亮的方法来做到这一点,但如果你想要,例如,黑色箭头进入你的情节,你可以这样做:

import matplotlib.patches

# get the axes (note that you should actually capture this when creating the subplot)
ax = plt.gca()

# iterate through the children of ax
for art in ax.get_children():
# we are only interested in FancyArrowPatches
if not isinstance(art, matplotlib.patches.FancyArrowPatch):
continue
# remove the edge, fill with black
art.set_edgecolor([0, 0, 0, 0])
art.set_facecolor([0, 0, 0, 1])
# make it bigger
art.set_mutation_scale(30)
# move the arrow head to the front
art.set_zorder(10)

这会创建:

enter image description here

然后是通常的警告:这是丑陋和脆弱的。

关于python - 更改 Matplotlib 流图箭头的 FaceColor 和 EdgeColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25349693/

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