用pyplot绘制带箭袋函数的箭头的对数长度

时间:2014-06-30 13:01:19

标签: python matplotlib plot logarithm

是否有一种简单的方法可以从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服务器上,它将在两周后自动删除。你可以通过在上面的数据链中用'.png'替换'.txt'来简单地访问这个数字。

2 个答案:

答案 0 :(得分:1)

从我收集的内容中,您可以在传入之前缩放箭头长度。这是一个示例

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()缩放了箭头长度。这会在保留其符号的同时缩放值的大小,否则您将记录负数的错误。

答案 1 :(得分:0)

遇到此问题时,我使用了与Greg类似的方法,但是正如您所注意到的,他对向量的调整存在问题,即它们没有保留箭头的方向。为此,您需要通过以下相同的因素来调整代码中的U和V:

 @override
  Widget build(BuildContext context) {
    return Container(
          margin: const EdgeInsets.only(bottom: 50),
          child: yourAdmobLayout(...)

此调整仅更改箭头的长度,以使新长度为旧长度的log10。

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