Removing Edge colors in Cartopy heatmap

Viewed 116

I am attempting to plot a heatmap showing the density of lightning using python's cartopy and matplotlib libraries.

I have roughly followed the code here Cartopy Heatmap over OpenStreetMap Background . However, my plot shown below contains solid lines around each transparent bin, which is my problem. The other plot is the same code with random numbers. An ideal solution would be to not display the lines at all, or for the lines to match the bin's face color with the correct transparency. I've done a fair amount of trial and error to remove them in addition to reading some matplotlib documentation. According to the 2d-histogram docs , I should be plotting a QuadMesh object. You should be able to set the linewidth to 0, or have the edgecolor set to none in the QuadMesh. In my code below, I tried doing that yet the lines still persist. I've also tried the pcolormesh as well with the same result. The Heatmap Second Dataset with Random Numbers

Here is my code.

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)
1 Answers

I can't reproduce the problematic results you showed us because of errors in your code. But once I correct the errors in the code and run. I get a good result as shown below.

The modified code:

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)

The output plot:

heatmap_on_streetmap

Related