删除Cartopy Heatmap中的边缘颜色
我正在尝试使用Python的作品和Matplotlib库来绘制一个热图,以显示闪电的密度。
我在此处大致遵循了代码 cartopy heatmap apenstreetmap背景。但是,下面显示的我的图包含每个透明箱周围的实线,这是我的问题。另一个图是带有随机数的代码。理想的解决方案是完全不显示线条,或者使线与正确的透明度匹配垃圾箱的脸部颜色。除了阅读一些matplotlib文档外,我已经进行了相当多的反复试验以删除它们。根据2d-histragr docs ,我应该请绘制a quadmesh quadmesh 对象。您应该能够将线宽设置为0,或者将EdgeColor设置为QuadMesh中的NON。在下面的代码中,我尝试执行此操作,但线路仍然存在。我还尝试了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)
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.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的代码错误,我无法重现您向我们展示的问题结果。但是,一旦我纠正了代码中的错误并运行。如下所示,我的结果很好。
修改后的代码:
输出图:
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:
The output plot:
正如拉斐尔(Raphael)所建议的那样,我的问题似乎是在渲染。我最终在Conda中安装了所需的库,并且问题似乎已经解决。
My problem seemed to be rendering as raphael suggested. I ended up installing the required libraries in conda and the problem seems to have been resolved.