更改Matplotlib Streamplot箭头的FaceColor和EdgeColor

2024-05-15 08:40:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我在网格中有一些数据,我用streamplot绘制流线,用颜色和宽度与速度相关。我怎样才能改变箭头的颜色,或者只改变边缘颜色? 我的目标是强调流向。如果有人有别的办法。。

我试着用c.arrows,编辑c.arrows.set_edgecolorc.arrows.set_edgecolorsc.arrows.set_facecolor和{}进行编辑,但是没有任何结果,即使我运行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)

Tags: import编辑颜色asnppltsincos
1条回答
网友
1楼 · 发布于 2024-05-15 08:40:05

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

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

  • 将箭头面片(类型FancyArrowPatch)添加到轴
  • PatchCollectionc.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

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

相关问题 更多 >