我正在尝试在python中的一个形状文件上做一个热图。我需要做很多这样的事情,所以我不想每次都在.shp中读取。
相反,我认为我可以创建地图边界的lineCollection实例并覆盖这两个图像。问题是-我似乎不能让这两个正确地排列在一起。
下面是代码,其中linecol是lineCollection对象。
fig = plt.figure()
ax = fig.add_subplot(111)
ax.contourf(xi,yi,zi)
ax.add_collection(linecol, autolim = False)
plt.show()有没有一种简单的方法来确定linecol的界限,使其与其他图的界限相匹配?我尝试过set_xlim和transforms.Bbox,但似乎无法驾驭它。
非常感谢您的帮助!
发布于 2012-02-07 13:03:10
变换是棘手的,因为涉及到各种坐标系。参见http://matplotlib.sourceforge.net/users/transforms_tutorial.html。
我设法将LineCollection缩放到合适的大小,如下所示。关键是要认识到,我需要在LineCollection上设置的新转换中添加+ ax.transData。(如果未在艺术家对象上设置任何变换,则默认设置为ax.transData。它将数据坐标转换为显示坐标。)
from matplotlib import cm
import matplotlib.pyplot as plt
import matplotlib.collections as mc
import matplotlib.transforms as tx
import numpy as np
fig = plt.figure()
# Heat map spans 1 x 1.
ax = fig.add_subplot(111)
xs = ys = np.arange(0, 1.01, 0.01)
zs = np.random.random((101,101))
ax.contourf(xs, ys, zs, cmap=cm.autumn)
lines = mc.LineCollection([[(5,1), (9,5), (5,9), (1,5), (5,1)]])
# Shape spans 10 x 10. Resize it to 1 x 1 before applying the transform from
# data coords to display coords.
trans = tx.Affine2D().scale(0.1) + ax.transData
lines.set_transform(trans)
ax.add_collection(lines)
plt.show()(此处输出:http://i.stack.imgur.com/hDNN8.png没有足够的声誉来内联发布。)

如果您需要在x和y上不相等地平移或缩放形状,则修改此选项应该很容易。
https://stackoverflow.com/questions/7904199
复制相似问题