python - 使用 pyplot 中的箭袋函数绘制箭头的对数长度

标签 python matplotlib plot logarithm

是否有一种简单的方法可以对 PyPlot 中的 quiver 函数绘制的箭头长度进行对数缩放?

原因是,我在陆地和海洋上绘制了风箭头,而海洋上的风的强度大约高出十倍。

因此,要么陆地上的箭头太小,无法通过绘制它们来获取任何信息,要么海洋上的箭头太大,导致海洋上的绘图过于拥挤。

我已经尝试使用符号日志函数对数据进行对数缩放,但似乎角度会带来麻烦。为了强调这一点,这里是一个小图的代码:

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()

enter image description here

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

关于python - 使用 pyplot 中的箭袋函数绘制箭头的对数长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24490753/

相关文章:

python - 匹配区域指示符字符类的 python 正则表达式

python - 如何在字典列表中绘制值?

python - 如何删除 Seaborn facetgrid 中重复的轴标签?

python - Pyplot 逐行颜色图

MATLAB 子图标题和轴标签

python - Pandas 在 DatetimeIndex 中指定时间范围

python - `python file.py` 和 `python -m file` 之间的区别

python - python 程序中的 Prolog 查询未返回正确答案

r - 定义 grid.arrange() 所以第三个图在中间?

r - 右转