缩小颜色条图例

问题描述 投票:0回答:2

有没有办法让这个颜色条图例变得更小?比如当前大小的 1/5 或 1/10,这样可以更好地融合?

我不知道您需要查看多少代码,所以这就是全部内容:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
import geopandas as gpd
from descartes import PolygonPatch
import pandas as pd
import math
import numpy as np


world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.loc[world['name'] == 'France', 'iso_a3'] = 'FRA'
world.loc[world['name'] == 'Norway', 'iso_a3'] = 'NOR'
world.loc[world['name'] == 'Somaliland', 'iso_a3'] = 'SOM'
world.loc[world['name'] == 'Kosovo', 'iso_a3'] = 'RKS'

world = world[(world.pop_est>0) & (world.name!="Antarctica")]
world['val'] = 0
fig, ax = plt.subplots(1, 1)


df=pd.read_csv('data.csv', usecols=['SpatialDimValueCode','Location','Period','Dim1','FactValueNumeric'])

def lerp(val, _max, _min):
    return math.pow((val - _min)/(_max - _min), 1/4)

min_ = min(df[df['Dim1'] == 'Total']['FactValueNumeric'].tolist())
max_ = max(df[df['Dim1'] == 'Total']['FactValueNumeric'].tolist())


for index, country in df[(df['Period'] == 2016) & (df['Dim1'] == 'Total')].iterrows():
    if(country['SpatialDimValueCode'] in world.iso_a3.tolist()):
        world.loc[world['iso_a3'] == country['SpatialDimValueCode'], 'val'] = lerp(country['FactValueNumeric'], max_, min_)

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad="0.01%")
world.plot(column='val', cmap='Greens', ax=ax, legend=True, cax=cax)

ax.axis('off')
plt.savefig('data.jpg', dpi=300, format='jpg',bbox_inches='tight', pad_inches=0)

python pandas matplotlib geopandas colorbar
2个回答
0
投票

您可以尝试

plt.rc('legend', fontsize=10)
,然后将字体大小调整为您想要的。

希望有帮助


0
投票

您可以使用图例关键字的

shrink
属性。

world.plot(column='val', cmap='Greens', ax=ax, legend=True, cax=cax, legend_kwds={'shrink':0.5})
© www.soinside.com 2019 - 2024. All rights reserved.