在pyplot.contourf上隐藏轮廓线描边以仅获取填充 [英] Hide contour linestroke on pyplot.contourf to get only fills

查看:537
本文介绍了在pyplot.contourf上隐藏轮廓线描边以仅获取填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个宠物项目来创建地图图像,然后在地形高程图上绘制道路和其他内容.它旨在规划山地自行车路线(过去我手工制作了一些矢量绘图,它们对于可视化非常有用).

I have a pet project to create images of maps, where I draw the roads and other stuff over a contour plot of the terrain elevation. It is intended to plan mountain bike routes (I have made some vectorial drawings by hand, in the past, and they work great for visualization).

当前,我从此处下载GeoTIFF中的数字高程模型: http://www.ecologia.ufrgs.br /labgeo/arquivos/downloads/dados/SRTM/geotiff/rs.rar

Currently, I download Digital Elevation Model, in GeoTIFF, from here: http://www.ecologia.ufrgs.br/labgeo/arquivos/downloads/dados/SRTM/geotiff/rs.rar

,然后使用GDAL和Matplotlib contourf函数创建图:

and then create the plot with GDAL and Matplotlib contourf function:

from osgeo import gdal
import matplotlib
import matplotlib.pyplot as plt
from pylab import cm
import numpy

f = 'rs.tif'

elev = gdal.Open(f)

a = elev.GetRasterBand(1).ReadAsArray()

w = elev.RasterXSize
h = elev.RasterYSize
print w, h

altura  = (0.35, 0.42)
largura = (0.70, 0.82)

a = a[int(h*altura[0]):int(h*altura[1]),
      int(w*largura[0]):int(w*largura[1])]


cont = plt.contourf(a, origin='upper', cmap=cm.gist_earth, levels=numpy.arange(0,1000,20))
plt.title('Altitudes - max: %d m; min: %d m' % (numpy.amax(a), numpy.amin(a)))
plt.show()

哪个给:

问题在于轮廓线是白色的",并且会产生一些视觉污染,这是不希望的,因为我以后要绘制道路和河流.

The problem is that contour lines are "white", and generate some visual pollution, which is undesired since I want to plot roads and rivers later.

因此,我正在尝试通过参数设置或通过hack(更改源代码)来修改contourf创建这些较浅的行的方式,类似于此处建议的方法:

So, I am trying to modify the way contourf create these lighter lines, either via parameter setting, or via hack (changing source code), similar to the one proposed here:

如何从Matplotlib格式化轮廓线

此外,如果有人知道如何使用其他库以更优雅的方式生成此类地图,我将非常感谢该提示!

Also, if anyone knows how to generate such a map in a more elegant way, using other libraries, I would appreciate the tip very much!

感谢阅读.

推荐答案

我终于找到了解决这个长期存在问题的合适方法(当前在Matplotlib 2.0中),该问题不需要多次调用轮廓或栅格化图形.

I finally found a proper solution to this long-standing problem (currently in Matplotlib 2.0), which does not require multiple calls to contour or rasterizing the figure.

请注意,问题中显示的问题仅以保存的具有出版物质量的图形格式(如PDF)出现,而不以较低质量的栅格文件(如PNG)出现.

Note that the problem illustrated in the question appears only in saved publication-quality figures formats like PDF, not in lower-quality raster files like PNG.

我的解决方案的灵感来自答案,该答案与颜色条的类似问题有关.事实证明,类似的解决方案也可以解决等高线图,如下所示:

My solution was inspired by this answer, related to a similar problem with the colorbar. A similar solution turns out to solve the contour plot as well, as follows:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(123)
x, y = np.random.uniform(size=(100, 2)).T
z = np.exp(-x**2 - y**2)
levels = np.linspace(0, 1, 100)

cnt = plt.tricontourf(x, y, z, levels=levels, cmap="ocean")

# This is the fix for the white lines between contour levels
for c in cnt.collections:
    c.set_edgecolor("face")

plt.savefig("test.pdf")    

以下是修复之前的轮廓示例

Here below is an example of contours before the fix

下面是上述修复后的同一图

And here below is the same figure after the above fix

这篇关于在pyplot.contourf上隐藏轮廓线描边以仅获取填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆