底图mpld3工具提示隐藏/显示节点标签

时间:2018-08-12 02:01:30

标签: python matplotlib-basemap mpld3

我不熟悉编程,因此尝试将Tooltips mpld3插件应用于此地图以显示选中时每个节点的标签,但是由于我使用的是for循环,因此无法将正确的参数传递给PointLabelTooltip(https://mpld3.github.io/examples/scatter_tooltip.html)  有没有一种方法可以将标签和点的坐标分组,以便仅通过提到的插件显示和隐藏它们?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from colour import Color
from mpl_toolkits.basemap import Basemap
import mpld3

lats = [27.14309625, 29.425095000000002, 28.340101399999998, 25.4504203, 26.8284772, 26.79433, 26.98394715, 27.3054948, 25.7759608, 26.925553999999998, 25.379094100000003, 28.308865500000003, 27.790111199999995, 29.006420000000002, 27.65778955, 27.0514177, 25.529286499999998, 26.899998800000002, 28.33717065, 28.34926665, 27.027876199999998, 28.4217309, 27.3150222, 25.45089587, 28.701960200000002, 27.37352345, 25.540786999999998, 27.94246405, 27.0053461, 25.4192971, 27.059801500000003, 28.064626699999998, 26.124927300000003, 27.2810136, 25.542698, 25.343033300000002, 28.096385700000003, 28.49280954]
lons = [-101.3273818, -101.9353718, -100.8514785, -100.85237629999999, -100.749837, -101.42870400000001, -102.061353, -101.30062070000001, -103.2722493, -101.45229259999999, -101.4761115, -100.3787998, -99.8752627, -100.8829136, -100.6880688, -101.7949359, -103.2319968, -101.4171173, -101.1183428, -101.6740921, -101.5923026, -100.7674167, -102.3967266, -102.1793823, -100.5219222, -100.8685395, -100.9466688, -101.0727813, -101.7263078, -100.9945068, -101.5466831, -101.3903538, -102.7745628, -103.7706311, -103.4105122, -102.8050434, -100.79781259999999, -100.922123]
num_cds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38]
nom_mun = ['Abasolo', 'Acuña', 'Allende', 'Arteaga', 'Candela', 'Castaños', 'Cuatro Ciénegas', 'Escobedo', 'Francisco I. Madero', 'Frontera', 'General Cepeda', 'Guerrero', 'Hidalgo', 'Jiménez', 'Juárez', 'Lamadrid', 'Matamoros', 'Monclova', 'Morelos', 'Múzquiz', 'Nadadores', 'Nava', 'Ocampo', 'Parras', 'Piedras Negras', 'Progreso', 'Ramos Arizpe', 'Sabinas', 'Sacramento', 'Saltillo', 'San Buenaventura', 'San Juan de Sabinas', 'San Pedro', 'Sierra Mojada', 'Torreón', 'Viesca', 'Villa Unión', 'Zaragoza']
circuitos = [[6,18,10,31,21,29,16,7,23,34,33,9,35,17,36,24,11,30,4,27],[15,13,12,25,14,2,20,32,19,38,22,3,37,28,8,1,30,5,26]]

fig, ax = plt.subplots()
marco_mapa = 0.5
m = Basemap(projection='merc', 
    lat_0 = max(lats), lon_0 = min(lons),
    resolution = 'i',
    llcrnrlon=min(lons)-marco_mapa, llcrnrlat=min(lats)-marco_mapa,
    urcrnrlon=max(lons)+marco_mapa, urcrnrlat=max(lats)+marco_mapa, epsg=4326)
m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 200, verbose= False)
m.drawstates(color='lightgray',linewidth=0.5)
m.drawcountries(color='lightgray',linewidth=0.5)

colors = cm.rainbow(np.linspace(0, 1, len(circuitos)))
for ind_r in range(len(circuitos)):
    lons_sort = list()
    lats_sort = list()
    for lat_ruta in circuitos[ind_r]:
        lats_sort.append(lats[lat_ruta-1])
    for lon_ruta in circuitos[ind_r]:
        lons_sort.append(lons[lon_ruta-1])

    lats_sort.append(lats[circuitos[ind_r][0]-1])
    lons_sort.append(lons[circuitos[ind_r][0]-1])

    x, y = m(lons_sort, lats_sort)
    lineas = ax.plot(x, y, '-', color=colors[ind_r], markersize=1, linewidth=1,\
           label='Ruta {}'.format(ind_r+1))

    points = ax.plot(x, y, 'o', color=colors[ind_r],
                 ms=5, alpha=.6)

handles, labels = ax.get_legend_handles_labels() # return lines and labels
interactive_legend = mpld3.plugins.InteractiveLegendPlugin(handles,
                                                     labels,
                                                     alpha_unsel=0.5,
                                                     alpha_over=1.5, 
                                                     start_visible=True)

plt.subplots_adjust(left=0, bottom=0, top=1, wspace=None, hspace=None)    

mpld3.plugins.connect(fig, interactive_legend)

mpld3.show()

0 个答案:

没有答案