matplotlib 删除Cartopy热图中的边缘颜色

abithluo  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(131)

我正尝试使用python的cartopy和matplotlib库绘制一张显示 lightning 密度的热图。
我大致遵循了这里的代码Cartopy Heatmap over OpenStreetMap Background。但是,我的图显示如下包含实线周围的每个透明箱,这是我的问题。另一个图是带有随机数的相同代码。理想的解决方案是根本不显示这些线,或者使这些线与具有正确透明度的箱子表面颜色相匹配。除了阅读一些matplotlib文档之外,我还做了大量的尝试和错误来删除它们。根据2d直方图docs,我应该绘制一个QuadMesh对象。您应该能够将“线宽”设置为0,或者在“四边形网格”中将“边颜色”设置为“无”。在我下面的代码中,我尝试这样做,但行仍然存在。我也试过pcolormesh,结果也是一样的。

这是我的代码。

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import cartopy.feature as cfeature
import cartopy.io.img_tiles as cimgt
import numpy as np
import random

#xsize and ysize are integers and lons, lats are 1d numpy arrays of longitude and latitude.
def testDensity(xsize, ysize, lons, lats):
    #Some code below follows example 
    #https://stackoverflow.com/questions/50611018/cartopy-heatmap-over-openstreetmap-background

    request = cimgt.OSM()
    fig, ax = plt.subplots(figsize=(xsize,ysize),subplot_kw=dict(projection=request.crs), dpi=200)
    extent = [-126,-118,41,44]
    ax.set_extent(extent)
    ax.add_image(request,8)

    xynps = ax.projection.transform_points(ccrs.Geodetic(), lons, lats)#
    print(xynps, type(xynps))

    #Create 2-d histogram
    histogram = ax.hist2d( xynps[:,0] , xynps[:,1] ,cmap='jet', bins=100, zorder=1,alpha=0.5,edgecolors="none",linewidth=0 )
    print(histogram[3], dir(histogram[3]) )
    histogram[3].set_linewidth(0.0)
    histogram[3].set_edgecolor("none")
    #histogram:(frequency, xedges, yedges, image QuadMesh)

    #ax.pcolormesh(histogram[1], histogram[2], histogram[0], cmap = 'jet', alpha=0.5,edgecolors="none")
    cbar = plt.colorbar(mappable=histogram[3], ax=ax , shrink=0.5, format='%.1f1' )
    cbar.solids.set_rasterized("True")#Removes lines from colorbar
    cbar.solids.set_edgecolor("face")
    plt.savefig("densityTest.png", bbox_inches="tight")


#Generate random dataset
for i in range(0,800):
    lon = random.randrange(41,44) + random.random()
    lat = random.randrange(-126,-118) + random.random()
    lons.append(lon)
    lats.append(lat)
lons = np.array(lons)
lats = np.array(lats)

testDensity(9,34, lons, lats)
yws3nbqq

yws3nbqq1#

由于代码中的错误,我无法重现您向我们展示的问题结果。但是一旦我纠正了代码中的错误并运行。我得到了一个很好的结果,如下图所示。
修改后的代码:

def testDensity(xsize, ysize, lons, lats):
    # Some code below follows example 
    # https://stackoverflow.com/questions/50611018/cartopy-heatmap-over-openstreetmap-background (That's my another answer)

    request = cimgt.OSM()
    fig, ax = plt.subplots(figsize=(xsize,ysize),subplot_kw=dict(projection=request.crs), dpi=200)
    extent = [-126, -118, 41, 44]
    ax.set_extent(extent)
    ax.add_image(request, 8)

    xynps = ax.projection.transform_points(ccrs.Geodetic(), lons, lats)

    #Create 2-d histogram
    # histogram = ax.hist2d(xynps[:,0],xynps[:,1],cmap='jet',bins=100,zorder=1,alpha=0.5,edgecolors="none",linewidth=0)
    #This produces the same result, but shorter.
    histogram = ax.hist2d( xynps[:,0], xynps[:,1], cmap='jet', bins=100, zorder=1, alpha=0.5)

    # (Why use these code?)
    #histogram[3].set_linewidth(0.0)
    #histogram[3].set_edgecolor("none")
    #ax.pcolormesh(histogram[1], histogram[2], histogram[0], cmap = 'jet', alpha=0.5,edgecolors="none")
    # cbar = plt.colorbar(mappable=histogram[3], ax=ax , shrink=0.5, format='%.1f' )
    # cbar.solids.set_rasterized("True")#Removes lines from colorbar
    # cbar.solids.set_edgecolor("face")

    # ... when this produces good result.
    cbar = plt.colorbar(histogram[3], ax=ax, pad=0.03, aspect=28, shrink=0.26, format='%.1f')  # h[3]: image
    plt.savefig("densityTest.png", bbox_inches="tight")
    plt.show()

#Generate random dataset
lons = []
lats = []

for i in range(0,800):
    lat = random.randrange(41,44) + random.random()
    lon = random.randrange(-126,-118) + random.random()
    lons.append(lon)
    lats.append(lat)

lons = np.array(lons)
lats = np.array(lats)

#testDensity(9,34, lons, lats)
testDensity(10,16, lons, lats)

输出图:

tjrkku2a

tjrkku2a2#

我的问题似乎是渲染拉斐尔建议。我最终在conda中安装了所需的库,问题似乎已经解决了。

相关问题