获取 mpl_toolkits.basemap.Basemap 中散点的工作 alpha 值

Get working alpha value of scatter points in mpl_toolkits.basemap.Basemap

基于 here 中的示例,我想在地图上绘制具有不同 alpha 值和不同面色的点(我计划更新它们)。但是,alpha 值似乎没有更新(它们似乎也一开始就被忽略了)。

首先,按照示例创建地图并绘制三个点:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

map = Basemap(projection='merc', lat_0 = 57, lon_0 = -135,
    resolution = 'h', area_thresh = 0.1,
    llcrnrlon=-136.25, llcrnrlat=56.0,
    urcrnrlon=-134.25, urcrnrlat=57.75)

map.drawcoastlines()
map.drawcountries()
map.fillcontinents(color = 'lightgray', zorder = 0)
map.drawmapboundary()

lons = [-135.3318, -134.8331, -134.6572]
lats = [57.0799, 57.0894, 56.2399]
x,y = map(lons, lats)
pts = map.scatter(x, y, c ='r', marker = 'o', s = 80, alpha = 1.0)

然后用新的 rgba 值更新面部颜色。

pts.set_facecolor([(0.7, 0.3, 0.3, 1.0), (1, 0.0, 1, 0.5), (1.0, 1.0, 0.2, 0.2)])
fig.canvas.draw()

颜色更新,但 alpha 值绘制不正确。我也尝试过使用 pts.set_alpha 但这不允许将列表作为参数。

不清楚您是否可以分别为每个点设置 alpha 值,但如果您使用多个 plot 命令,它会起作用。

pts = bmap.scatter(x[0], y[0], c ='b', marker = 'o', s = 80, alpha = 0.7)
pts = bmap.scatter(x[1], y[1], c ='g', marker = 'o', s = 80, alpha = 0.3)  
pts = bmap.scatter(x[2], y[2], c ='r', marker = 'o', s = 80, alpha = 0.5)

因此,after posting this as an issue on github,如果您不首先设置 alpha 值,似乎可以更新 alpha 值。

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.interactive(True)

fig, ax = plt.subplots()

map = Basemap(projection='merc', lat_0 = 57, lon_0 = -135,
    resolution = 'i', area_thresh = 0.1,
    llcrnrlon=-136.25, llcrnrlat=56.0,
    urcrnrlon=-134.25, urcrnrlat=57.75)

map.drawcoastlines(zorder = 0)
map.drawcountries(zorder = 0)
map.fillcontinents(color = 'lightgray', zorder = 0)
map.drawmapboundary(zorder = 0)

lons = [-135.3318, -134.8331, -134.6572]
lats = [57.0799, 57.0894, 56.2399]
x,y = map(lons, lats)
pts = map.scatter(x, y, c ='r', marker = 'o', s = 600)

并且可以使用 set_facecolor 更新这些 alpha 值,如下所示:

pts.set_facecolor([(0.7, 0.3, 0.3, 1.0), (1, 0.0, 1, 0.5), (1.0, 1.0, 0.2, 0.2)])
fig.canvas.draw()