本文介绍matplotlib中quiver用于绘制箭头图的用法。
quiver的英文释义是:a long, thin containers for carrying arrows。如果要同时表示向量的大小和方向,起点和终点,那么就需要绘制箭头图。
在matplotlib中使用quiver绘制箭头图非常简单。如上图所示,这里放上一段绘制三维箭头图的代码:
from glob import glob
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
fps = glob("*-position*/*Detailed.csv")
# 批量读取Imaris导出的荧光小球marker在不同通道的spot的position数据表
ch1 = pd.read_csv(fps[0], header=2)
ch2 = pd.read_csv(fps[1], header=2)
res = showChannelTranslation(ch1, ch2)
# 使用之前写好的函数计算每个spot的偏移向量
dat = res['dat']
dist = res['dist']
norm = Normalize()
colors = plt.cm.viridis(norm(dist))
# 使用偏移向量的长度归一化后映射为颜色调色板
ax= plt.figure(figsize=(10,10)).add_subplot(projection='3d')
ax.quiver(0,
0,
0,
dat[:,0],
dat[:,1],
dat[:,2],
c=dist,
color=colors,
arrow_length_ratio=0.1,
pivot='tail',
)
# 注意 ax.quiver() 中头6个参数,x,y,z 是箭头的终点,而 u,v,w 是具体的向量数值。
# 此处希望所有箭头从原点出发,所以pivot默认是tail,对应xyz坐标。
ax.set_aspect('equal')
# ax.view_init(elev=90, azim=0)
cbar = plt.colorbar(plt.cm.ScalarMappable(norm=norm,cmap=plt.cm.viridis),
ax=ax, shrink=0.3)
cbar.set_label('Distance (μm)')
# 给三维图表添加scale bar,注意这里可以通过shrink调整大小
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()
其中自定义函数 showChannelTranslation()
详见之前的博客文章👇
此处评论已关闭