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

时间:2016-03-02 22:36:17

标签: python matplotlib alpha matplotlib-basemap

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

首先,根据示例创建一个地图并绘制三个点:

List price: <input type="number" name="list" value="1000"><br>
Discount:   <input type="number" name="disc">%<br>
Sale price: <input type="number" name="sale">
<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>

First figure

然后使用新的rgba值更新facecolors。

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)

Second figure

颜色更新但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() ,但这不允许列表作为参数。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

这些alpha值可以使用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