1

我处理地理信息并使用 matplotlib 呈现结果。所有输入都是纬度/经度[度]。我转换为 x/y [米] 进行计算。我以纬度/经度呈现我的结果。问题是要让图形纵横比正确:所有图形都太宽了。是否有设置正确纵横比的标准程序,以便我可以简单地
使用纬度/经度绘制散点图和其他图表,结果具有正确的形状?在屏幕上和纸上 (png)?

[稍后添加这部分]这是我的问题的一个简单的剥离版本。我需要围绕轴的实际纬度/经度值和准确的形状(正方形)。现在它看起来很宽(2x)。

import math
import matplotlib.pyplot as plt
import numpy as np
from pylab import *

w=1/math.cos(math.radians(60.0))
plt_area=[0,w,59.5,60.5] #60deg North, adjacent to the prime meridian

a=np.zeros(shape=(300,300))

matshow(a, extent=plt_area)

plt.grid(False)
plt.axis(plt_area)
fig   = plt.gcf()
fig.set_size_inches(8,8)
fig.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9)

plt.show()
4

3 回答 3

2

看来我找到了解决方案。我在这里找到了它:如何在 matplotlib 中设置纵横比?

import math
import matplotlib.pyplot as plt
import numpy as np

w=1/math.cos(math.radians(60.0))
plt_area=[0,w,59.5,60.5] #square area

a=np.zeros(shape=(300,300))

fig = plt.figure()
ax = fig.add_subplot(111)

ax.imshow(a)

plt.grid(False)
ax.axis(plt_area)
fig   = plt.gcf()
fig.set_size_inches(8,8)
ax.set_aspect(w)
fig.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9)

plt.show()
于 2013-09-20T13:47:10.960 回答
0

在 matplotlib 中,我通常像这样更改图形大小:

import matplotlib.pyplot as plt

plt.clf()
fig     = plt.figure()
fig_p   = plt.gcf()
fig_p.set_size_inches(8, 8)    # x, y

但是,这会设置图形外部尺寸的尺寸,而不是绘图区域。您可以相对于图形大小更改绘图区域,分别以 x 和 y 的总图形大小长度的比率给出:

fig.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9)

只要相对比率保持对称,绘图的纵横比就应该相同。

示例 1:

plt.clf()
fig     = plt.figure()
fig_p   = plt.gcf()
fig_p.set_size_inches(5, 5)    # x, y for figure canvas

# Relative distance ratio between origin of the figure and max extend of canvas
fig.subplots_adjust(left=0.2, right=0.8, bottom=0.2, top=0.8)

ax1   = fig.add_subplot(111)
xdata = [rand()*10 for i in xrange(100)]
ydata = [rand()*1  for i in xrange(100)]
ax1.plot(xdata, ydata, '.b', )
ax1.set_xlabel('Very Large X-Label', size=30)
plt.savefig('squareplot.png', dpi=96)

$.subplot_adjust$ 覆盖图形大小和绘图扩展之间的默认空间

示例 2:

fig.subplots_adjust(left=0.0, right=1.0, bottom=0.0, top=1.0)

绘图区域完全填充图形大小:

图形大小和绘图区域之间没有间距

于 2013-09-18T15:51:33.197 回答
0

不要试图通过摆弄fig.set_size_inches()fig.subplots_adjust()更改数据来解决此问题;而是使用墨卡托投影。

您可以通过使用数据平均纬度的余弦的倒数的纵横比来获得快速而肮脏的墨卡托投影。这对于包含在大约 1 度纬度(大约 100 公里)中的数据来说是“相当不错的”。(对于您的应用程序,您必须决定这是否“足够好”。如果不是,您真的必须考虑一些严肃的地理投影库......)

例子:

from math import cos, radians
import matplotlib.pyplot as plt
import numpy as np

# Helsinki 60.1708 N, 24.9375 E
# Helsinki (lng, lat)
hels = [24.9375, 60.1708]
# a point 100 km directly north of Helsinki
pt_N = [24.9375, 61.0701]
# a point 100 km east of Helsinki along its parallel
pt_E = [26.7455, 60.1708]

coords = np.array([pt_N, hels, pt_E])

plt.figure()
plt.plot(coords[:,0], coords[:,1])

# either of these will estimate the "central latitude" of your data
# 1) do the plot, then average the limits of the y-axis    
central_latitude = sum(plt.axes().get_ylim())/2.
# 2) actually average the latitudes in your data
central_latitude = np.average(coords, 0)[1]

# calculate the aspect ratio that will approximate a 
# Mercator projection at this central latitude 
mercator_aspect_ratio = 1/cos(radians(central_latitude))

# set the aspect ratio of the axes to that
plt.axes().set_aspect(mercator_aspect_ratio)

plt.show()

我选择赫尔辛基作为示例,因为在那个纬度,纵横比几乎是 2……因为两度经度与一度纬度的距离大致相同。

要真正看到这项工作:a)运行上述,b)调整窗口大小。然后注释掉调用set_aspect()并做同样的事情。在第一种情况下,保持正确的纵横比,在后一种情况下,您会得到无意义的拉伸。

赫尔辛基以北和以东 100 公里的点是由 EXCELLENT 页面计算/确认Movable Type Scripts 的lat/lng 点之间的距离

于 2016-01-08T13:51:36.960 回答