2016-03-02 52 views
1

基於here的示例,我想繪製具有不同alpha值和不同顏色(我打算更新它們)的地圖上的點。然而,alpha值似乎沒有更新(它們似乎也被忽略了)。在mpl_toolkits.basemap.Basemap獲取工作的分散點的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) 

First figure

然後更新與新的RGBA值facecolors。

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() 

Second figure

顏色更新但Alpha值不正確打印。我也嘗試過使用pts.set_alpha,但這不允許列表作爲參數。

回答

0

所以,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) 

figure 1

而且這些α值可以通過使用set_facecolor,像這樣被更新:

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() 

figure 2

0

目前還不清楚您是否可以單獨爲每個點設置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) 

Map with different alpha values

+0

這並不爲我工作。 Alpha值保持不變。例如,嘗試將它們設置爲1: 'pts = map.scatter(x [0],y [0],c ='b',marker ='o',s = 80,alpha = 1)' 'pts = map.scatter(x [1],y [1],c ='g',marker ='o',s = 80,alpha = 1)' 'pts = map.scatter(x [2] ,y [2],c ='r',marker ='o',s = 80,alpha = 1)' –

+0

將它們設置爲1是什麼?使用alpha = 1,現在看起來像圖1。 – BChow

+0

謝謝。然而,在alpha = 1的情況下,你仍然可以看到下面的海岸線,所以它不是alpha = 1。我想要點也有不同的alpha值。 –