Геопанды уменьшают размер легенды (и удаляют пустое пространство под картой)

Я разработал неэлегантное решение. Предположим, что у нас есть ось X, а также список меток для каждой точки в X.

Пример:
import matplotlib.pyplot as plt

x = [0,1,2,3,4,5]
y = [10,20,15,18,7,19]
xlabels = ['jan','feb','mar','apr','may','jun']
Предположим, что я хочу показывать метки метки только для «feb» и «jun»
xlabelsnew = []
for i in xlabels:
    if i not in ['feb','jun']:
        i = ' '
        xlabelsnew.append(i)
    else:
        xlabelsnew.append(i)
Good , теперь у нас есть поддельный список ярлыков. Сначала мы построили оригинальную версию.
plt.plot(x,y)
plt.xticks(range(0,len(x)),xlabels,rotation=45)
plt.show()
Теперь изменилась версия.
plt.plot(x,y)
plt.xticks(range(0,len(x)),xlabelsnew,rotation=45)
plt.show()
0
задан Arnaud Geotribu 17 January 2019 в 12:33
поделиться

1 ответ

Чтобы показать, как получить правильный размер легенды цветовой шкалы, сопровождающей карту, созданную методом geopandas plot (), я использую встроенный набор данных naturalearth_lowres . Рабочий код выглядит следующим образом.

import matplotlib.pyplot as plt
import geopandas as gpd

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world[(world.name != "Antarctica") & (world.name != "Fr. S. Antarctic Lands")]  # exclude 2 no-man lands

# plot as usual, grab the axes 'ax' returned by the plot
colormap = "copper_r"   # add _r to reverse the colormap
ax = world.plot(column='pop_est', cmap=colormap, \
                figsize=[12,9], \
                vmin=min(world.pop_est), vmax=max(world.pop_est))

# map marginal/face deco
ax.set_title('World Population')
ax.grid() 

# colorbar will be created by ...
fig = ax.get_figure()
# add colorbar axes to the figure
# here, need trial-and-error to get [l,b,w,h] right
# l:left, b:bottom, w:width, h:height; in normalized unit (0-1)
cbax = fig.add_axes([0.95, 0.3, 0.03, 0.39])   
cbax.set_title('Population')

sm = plt.cm.ScalarMappable(cmap=colormap, \
                norm=plt.Normalize(vmin=min(world.pop_est), vmax=max(world.pop_est)))
# at this stage, 
# 'cbax' is just a blank axes, with un needed labels on x and y axes

# blank-out the array of the scalar mappable 'sm'
sm._A = []
# draw colorbar into 'cbax'
fig.colorbar(sm, cax=cbax, format="%d")

# dont use: plt.tight_layout()
plt.show()

Прочитайте комментарии в коде для полезной информации.

Результирующий сюжет: enter image description here

0
ответ дан swatchai 17 January 2019 в 12:33
поделиться
Другие вопросы по тегам:

Похожие вопросы: