在matplotlib中绘制具有不同颜色的箭头 [英] Plotting arrows with different color in matplotlib
问题描述
我有一个二维数组,其中包含5列和一定数量的行.不同的列具有以下条目x1 y1 x2 y2 z
我想绘制一个从(x1,y1)到(x2,y2)的箭头,箭头的颜色应该从与某些内置色图相对应的z列中获取.
我该怎么做matplotlib/python?
I have a two dimensional array with 5 columns and some number of rows. The different columns have the following entriesx1 y1 x2 y2 z
I want to plot an arrow from (x1,y1) to (x2,y2) and the color of the arrow should be taken from z column corresponding to some inbuilt colormap.
How can I do this matplotlib/python?
推荐答案
您可以执行以下操作:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cmx
DATA = np.random.rand(5,5)
cmap = plt.cm.jet
cNorm = colors.Normalize(vmin=np.min(DATA[:,4]), vmax=np.max(DATA[:,4]))
scalarMap = cmx.ScalarMappable(norm=cNorm,cmap=cmap)
for idx in range(0,len(DATA[:,1])):
colorVal = scalarMap.to_rgba(DATA[idx,4])
plt.arrow(DATA[idx,0], #x1
DATA[idx,1], # y1
DATA[idx,2]-DATA[idx,0], # x2 - x1
DATA[idx,3]-DATA[idx,1], # y2 - y1
color=colorVal)
plt.show()
您要使用scalarMap.to_rgba
将您的z
值转换为参数,以传递给arrow
命令的color
选项.
您的结果应如下所示:
You want to use scalarMap.to_rgba
to turn your z
value into an argument to pass to the color
option of the arrow
command.
Your result should look something like this:
编辑
如果您也想查看颜色条,则事情会有些棘手.这是一个更新的最小示例:
EDIT
If you want to see the colorbar, too, things are a little bit more tricky. Here's an updated minimal example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cmx
import matplotlib as mpl
DATA = np.random.rand(5,5)
cmap = plt.cm.jet
cNorm = colors.Normalize(vmin=np.min(DATA[:,4]), vmax=np.max(DATA[:,4]))
scalarMap = cmx.ScalarMappable(norm=cNorm,cmap=cmap)
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.7, 0.85]) # [left, bottom, width, height]
axc = fig.add_axes([0.85, 0.10, 0.05, 0.85])
for idx in range(0,len(DATA[:,1])):
colorVal = scalarMap.to_rgba(DATA[idx,4])
ax.arrow(DATA[idx,0], # x1
DATA[idx,1], # y1
DATA[idx,2]-DATA[idx,0], # x2 - x1
DATA[idx,3]-DATA[idx,1], # y2 - y1
color=colorVal)
cb1 = mpl.colorbar.ColorbarBase(axc, cmap=cmap,
norm=cNorm,orientation='vertical')
plt.show()
注意事项:
- 访问
ColorbarBase
的其他 - 现在,明确需要指定两个轴,一个用于箭头,一个用于颜色条.第二组轴的色条尺寸应合理.
add_axes
命令采用相对单位的[left, botton, width, height]
作为输入.因此,右侧由left + width
给出. - 在第一组轴
ax
上绘制箭头,这是您的初始图形. - 在第二组轴
axc
上绘制关节标尺.传递cmap
,规范化,cNorm
和方向作为参数.
import matplotlib as mpl
- The additional
import matplotlib as mpl
to have access to theColorbarBase
- Now, there is an explicit need to specify two axes, one for the arrows and one for the colorbar. This second set of axis should have a reasonable size for the colorbar.
Theadd_axes
command takes[left, botton, width, height]
in relative units as input. So the right side is given byleft + width
. - Plot the arrows on the first set of axis,
ax
, your initial figure. - Plot the colobar on the second set of axis,
axc
. Pass thecmap
, the normalization,cNorm
and an orientation as arguments.
您的身材应该看起来像这样:
Your figure should look something like this:
编辑2
如果箭头上希望有其他颜色的边缘,请将color
更改为facecolor
(或fc
),然后指定edgecolor
(ec
).此外,您现在可能需要控制箭头的宽度(默认= 0.001
)和头部的宽度(默认= 3x width
).
If you want a different colored edge on the arrows, change color
to facecolor
(or fc
) and specify an edgecolor
(ec
). Additionally, you may now want to control the width of the arrow (default = 0.001
) and the width of the head (default = 3x width
).
plt.arrow(DATA[idx,0], #x1
DATA[idx,1], # y1
DATA[idx,2]-DATA[idx,0], # x2 - x1
DATA[idx,3]-DATA[idx,1], # y2 - y1
facecolor=colorVal,
edgecolor='k',
width=0.005,
head_width=0.01)
这篇关于在matplotlib中绘制具有不同颜色的箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!