Python 用pyplot中的箭图函数绘制箭头时的对数长度

Python 用pyplot中的箭图函数绘制箭头时的对数长度,python,matplotlib,plot,logarithm,Python,Matplotlib,Plot,Logarithm,有没有一种简单的方法可以从PyPlot对数缩放箭袋函数绘制的箭头的长度 原因是,我画了陆地和海洋上的风箭头,而海洋上的风的大小大约是十倍 因此,要么陆地上的箭头太小,无法从绘图中获得任何信息,要么海洋上的箭头太大,以至于绘图在海洋上过于拥挤 我已经尝试使用symlog函数以对数方式缩放数据,但角度似乎会带来麻烦。为了强调这一点,下面是一个小图的代码: def main(): lat, lon, u, v = readmulticol2Dfile('test.txt', shape=(9,6)

有没有一种简单的方法可以从PyPlot对数缩放箭袋函数绘制的箭头的长度

原因是,我画了陆地和海洋上的风箭头,而海洋上的风的大小大约是十倍

因此,要么陆地上的箭头太小,无法从绘图中获得任何信息,要么海洋上的箭头太大,以至于绘图在海洋上过于拥挤

我已经尝试使用symlog函数以对数方式缩放数据,但角度似乎会带来麻烦。为了强调这一点,下面是一个小图的代码:

def main():
  lat, lon, u, v = readmulticol2Dfile('test.txt', shape=(9,6))
  # map without logarithmic scaling
  plt.subplot(1,2,1)
  mapproj = bm.Basemap(projection='cyl', llcrnrlon=lon.min(), urcrnrlon=lon.max(), llcrnrlat=lat.min(), urcrnrlat=lat.max())
  mapproj.drawcoastlines()
  mapproj.drawparallels(np.linspace(-20,20,5), labels=[1,0,0,0])
  mapproj.drawmeridians(np.linspace(-80,20,5), labels=[0,0,0,1])
  plt.title('linear')
  plt.quiver(lon, lat, u, v, color='k', units='x')
  # map with logarithmic scaling
  plt.subplot(1,2,2)
  mapproj = bm.Basemap(projection='cyl', llcrnrlon=lon.min(), urcrnrlon=lon.max(), llcrnrlat=lat.min(), urcrnrlat=lat.max())
  mapproj.drawcoastlines()
  mapproj.drawparallels(np.linspace(-20,20,5), labels=[1,0,0,0])
  mapproj.drawmeridians(np.linspace(-80,20,5), labels=[0,0,0,1])
  plt.quiver(lon, lat, symlog(u), symlog(v), color='k', units='x')
  plt.title('logarithmic')
  plt.show()

def readmulticol2Dfile(fname, header=True, delimiter='\t', shape=None):
  """reads a multicolumn txt-file and converts it to numpy arrays"""
  a=np.loadtxt(fname).T
  if shape is not None:
    b=[np.zeros(shape=shape)]*4
    for i in xrange(len(a)):    b[i]=np.reshape(a[i],shape)
    return b
  else: return a

def symlog(x):
  """ Returns the symmetric log10 value """
  return np.sign(x) * np.log10(np.abs(x))

if __name__=="__main__":
  main()
使用此数据,保存在
test.txt

# lattitude longitude   u   v
2.145047503739818495e+01    -8.062500000000000000e+01   -5.790064811706542969e+00   2.341136932373046875e-01
2.145047503739818495e+01    -7.312500000000000000e+01   -7.119166374206542969e+00   -1.223894119262695312e+00
2.145047503739818495e+01    -6.562500000000000000e+01   -6.140162467956542969e+00   -1.082292556762695312e+00
2.145047503739818495e+01    -5.812500000000000000e+01   -4.589381217956542969e+00   2.853832244873046875e-01
2.145047503739818495e+01    -5.062500000000000000e+01   -5.221705436706542969e+00   4.221019744873046875e-01
2.145047503739818495e+01    -4.312500000000000000e+01   -5.333521842956542969e+00   6.321525573730468750e-02
1.771996152644742750e+01    -2.812500000000000000e+01   -7.793482780456542969e+00   -3.714616775512695312e+00
1.771996152644742750e+01    -2.062500000000000000e+01   -6.195338249206542969e+00   -6.160417556762695312e+00
1.585470386969487322e+01    -7.687500000000000000e+01   -8.054713249206542969e+00   -1.638355255126953125e-01
1.585470386969487322e+01    -6.937500000000000000e+01   -7.378443717956542969e+00   -2.906990051269531250e-02
1.585470386969487322e+01    -6.187500000000000000e+01   -6.270533561706542969e+00   3.127269744873046875e-01
1.585470386969487322e+01    -5.437500000000000000e+01   -7.410181999206542969e+00   -1.028003692626953125e-01
1.212418712345576566e+01    -3.937500000000000000e+01   -8.221217155456542969e+00   -2.800065994262695312e+00
1.212418712345576566e+01    -3.187500000000000000e+01   -7.579127311706542969e+00   -3.560319900512695312e+00
1.212418712345576566e+01    -2.437500000000000000e+01   -5.761256217956542969e+00   -5.112565994262695312e+00
1.025892816800637597e+01    -8.062500000000000000e+01   -4.789576530456542969e+00   -4.363542556762695312e+00
1.025892816800637597e+01    -7.312500000000000000e+01   -1.818385124206542969e+00   -1.677995681762695312e+00
1.025892816800637597e+01    -6.562500000000000000e+01   -3.078639030456542969e+00   -1.554460525512695312e+00
6.528409401479990493e+00    -5.062500000000000000e+01   -7.822779655456542969e+00   -1.683855056762695312e+00
6.528409401479990493e+00    -4.312500000000000000e+01   -8.200709342956542969e+00   -2.835222244262695312e+00
6.528409401479990493e+00    -3.562500000000000000e+01   -7.456568717956542969e+00   -2.850358963012695312e+00
6.528409401479990493e+00    -2.812500000000000000e+01   -5.878443717956542969e+00   -2.700944900512695312e+00
6.528409401479990493e+00    -2.062500000000000000e+01   -2.720240592956542969e+00   -1.258562088012695312e+00
4.663149706177883935e+00    -7.687500000000000000e+01   1.257298469543457031e+00    -1.143815994262695312e+00
9.326299678379910141e-01    -6.187500000000000000e+01   -6.386976242065429688e-01   -2.507495880126953125e-01
9.326299678379910141e-01    -5.437500000000000000e+01   -2.149439811706542969e+00   -1.390886306762695312e+00
9.326299678379910141e-01    -4.687500000000000000e+01   -5.939478874206542969e+00   -1.460222244262695312e+00
9.326299678379910141e-01    -3.937500000000000000e+01   -6.882838249206542969e+00   -5.959644317626953125e-01
9.326299678379910141e-01    -3.187500000000000000e+01   -6.343287467956542969e+00   -1.565113067626953125e-01
9.326299678379910141e-01    -2.437500000000000000e+01   -5.749537467956542969e+00   3.444652557373046875e-01
-4.663149706177883935e+00   -7.312500000000000000e+01   -7.033824920654296875e-02   -3.654956817626953125e-01
-4.663149706177883935e+00   -6.562500000000000000e+01   -5.674085617065429688e-01   -3.176441192626953125e-01
-4.663149706177883935e+00   -5.812500000000000000e+01   -1.063014030456542969e+00   -1.213550567626953125e-01
-4.663149706177883935e+00   -5.062500000000000000e+01   -1.417994499206542969e+00   -2.028980255126953125e-01
-4.663149706177883935e+00   -4.312500000000000000e+01   -1.486842155456542969e+00   -8.557300567626953125e-01
-4.663149706177883935e+00   -3.562500000000000000e+01   -5.729517936706542969e+00   1.451887130737304688e+00
-8.393668907692383385e+00   -2.062500000000000000e+01   -6.579615592956542969e+00   2.450422286987304688e+00
-1.025892816800637597e+01   -7.687500000000000000e+01   4.018297195434570312e-01    -1.755542755126953125e-01
-1.025892816800637597e+01   -6.937500000000000000e+01   2.187242507934570312e-01    -5.783863067626953125e-01
-1.025892816800637597e+01   -6.187500000000000000e+01   -3.462171554565429688e-01   -8.737964630126953125e-01
-1.025892816800637597e+01   -5.437500000000000000e+01   -4.731702804565429688e-01   -3.200855255126953125e-01
-1.025892816800637597e+01   -4.687500000000000000e+01   -8.545179367065429688e-01   -4.890308380126953125e-01
-1.398944571235667311e+01   -3.187500000000000000e+01   -7.993678092956542969e+00   1.462230682373046875e-01
-1.398944571235667311e+01   -2.437500000000000000e+01   -8.063502311706542969e+00   1.512434005737304688e+00
-1.585470386969487322e+01   -8.062500000000000000e+01   -2.528346061706542969e+00   3.198469161987304688e+00
-1.585470386969487322e+01   -7.312500000000000000e+01   2.924547195434570312e-01    1.297590255737304688e+00
-1.585470386969487322e+01   -6.562500000000000000e+01   -3.598890304565429688e-01   -1.232194900512695312e+00
-1.585470386969487322e+01   -5.812500000000000000e+01   4.196643829345703125e-02    -1.524187088012695312e+00
-1.958521860882233057e+01   -4.312500000000000000e+01   -1.116724967956542969e+00   -3.195972442626953125e-01
-1.958521860882233057e+01   -3.562500000000000000e+01   -5.567896842956542969e+00   -3.761003494262695312e+00
-1.958521860882233057e+01   -2.812500000000000000e+01   -7.378443717956542969e+00   -1.043718338012695312e+00
-1.958521860882233057e+01   -2.062500000000000000e+01   -8.015162467956542969e+00   7.102775573730468750e-02
-2.145047503739818495e+01   -7.687500000000000000e+01   2.352025032043457031e+00    2.376691818237304688e+00
-2.145047503739818495e+01   -6.937500000000000000e+01   2.939915657043457031e+00    1.054914474487304688e+00
但不幸的是,结果看起来很奇怪。长度实际上是对数的,但角度在某些区域表现得很奇怪

非常感谢您事先的帮助=)


PS:我没有足够的声誉在这里添加一个数字或第二个超链接,所以我把它上传到ftp服务器上,两周后会自动删除。您只需在上面的数据链接中将“.txt”替换为“.png”即可访问该图。

根据我收集的信息,您只需在传入之前缩放箭头长度即可。这里有一个例子

import matplotlib.pyplot as plt
import numpy as np

def symlog(x):
    """ Returns the symmetric log10 value """
    return np.sign(x) * np.log10(np.abs(x))

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10,5))

# Generate fake data
N = 50
X,Y = np.meshgrid(np.linspace(0, 1, N), np.linspace(0, 1, N))
U = np.random.normal(0, 0.2, size=(50, 50))
V = np.random.normal(0, 0.2, size=(50, 50))

# Set RHS data to be larger than LHS
U[:, N/2:] *= 1000
V[:, N/2:] *= 1000
angles=np.arctan2(V,U)*180.0/np.pi # calculate angles manually

#Without scaling
HEAD_LENGTH = 4
Q = ax1.quiver( X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
             color='k', units='x', headaxislength=HEAD_LENGTH)

#With scaling
Q = ax2.quiver( X[::3, ::3], Y[::3, ::3], symlog(U[::3, ::3]), symlog(V[::3, ::3]),
             color='k', units='x', headaxislength=HEAD_LENGTH, angles=angles)

plt.show()


左侧显示了原始数据,而右侧我使用
symlog()
缩放了箭头长度。这将缩放值的大小,同时保留它们的符号,否则您将在记录负数时出错

当我遇到这个问题时,我使用了与Greg类似的方法,但正如您所注意到的,他对向量的调整的问题是,它们不保留箭头的方向。要做到这一点,您需要按如下相同的因数调整代码中的U和V:

def transform(u, v):
    arrow_lengths = np.sqrt(u*u + v*v)
    len_adjust_factor = np.log10(arrow_lengths + 1) / arrow_lengths
    return u*len_adjust_factor, v*len_adjust_factor

U2, V2 = transform(U, V)
此调整仅改变箭头的长度,以便新长度为旧长度的log10


注意:我使用log10(x+1)而不是log10(x),只是为了使函数不改变输出相对于输入的符号,并使函数从点(0,0)开始。

嘿,谢谢你的帮助!:)虽然它对你有用,但罗加里希姆似乎会给天使们带来麻烦。我用simlog函数编辑了我的文章(见上文)。你能用我的数据和脚本重现我的问题吗?我怀疑,因为我没有你的数据文件。请添加一个简短的完整工作示例,例如复制和粘贴生产图。这将有助于改善你得到的答案。好吧,我通过手动计算角度并使用箭袋图的关键字“角度”来实现。我在下面提供完整的代码。