更改 Matplotlib Streamplot 箭头的 FaceColor 和 EdgeColor [英] Change FaceColor and EdgeColor of Matplotlib Streamplot Arrows

查看:44
本文介绍了更改 Matplotlib Streamplot 箭头的 FaceColor 和 EdgeColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some data in grid and I plot the streamlines with streamplot with color and width related to the speed. How can I change the color of the arrows, or just the edgecolors? My goal is to emphasis the stream direction. If someone have another way to do it..

我尝试使用 c.arrows,编辑 c.arrows.set_edgecolor,c.arrows.set_edgecolors,c.arrows.set_facecolorc.arrows.set_facecolors 什么也没发生,即使我运行 plt.draw()

I tried to do it using c.arrows, editing c.arrows.set_edgecolor,c.arrows.set_edgecolors,c.arrows.set_facecolor and c.arrows.set_facecolors and nothing happened, even when I run plt.draw()

图:

代码:

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)

推荐答案

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

(Beware, the analysis below may not be completely correct, I only took a cursory look into the source.)

看起来 streamplot 在创建箭头时做了两件事:

It seems that streamplot does two things when it creates the arrows:

  • 将箭头色块(类型为 FancyArrowPatch )添加到轴上
  • 将相同的箭头补丁添加到 PatchCollection (c.arrows)
  • adds arrow patches (type FancyArrowPatch) to the axes
  • adds the same arrow patches to the PatchCollection (c.arrows)

出于某种原因(我猜这背后是获得正确的缩放比例)该集合似乎没有被使用,也没有添加到轴上.因此,如果您更改颜色图或集合的颜色,则它对图形没有影响.

For some reason (I guess getting the correct scaling is behind this) the collection does not seem to be used and is not added to the axes. So, if you change the color map or color of the collection, it has no effect on the drawing.

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

There might be more beautiful ways of doing this, but if you want, e.g., black arrows into your plot, you may do this:

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)

这将创建:

然后是通常的警告:这是丑陋且脆弱的.

And then the usual warnings: This is ugly and fragile.

这篇关于更改 Matplotlib Streamplot 箭头的 FaceColor 和 EdgeColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆