缩小颜色条图例

发布于 2025-01-13 12:37:52 字数 1730 浏览 0 评论 0原文

有没有办法让这个颜色条图例变得更小?比如当前大小的 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)

输出

Is there a way to make this colorbar legend way smaller ? Like 1/5th or 1/10th of its current size so it blends in better ?

I don't know how much of the code you need to see so here is everything:

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)

output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有你 2025-01-20 12:37:52

您可以尝试 plt.rc('legend', fontsize=10),并将字体大小调整为您想要的值。

希望有帮助

You could try plt.rc('legend', fontsize=10), and adjust the fontsize to what you want it to be.

Hope it helps

染柒℉ 2025-01-20 12:37:52

您可以使用图例关键字的 shr​​ink 属性。

world.plot(column='val', cmap='Greens', ax=ax, legend=True, cax=cax, legend_kwds={'shrink':0.5})

You can use the shrink attribute of legend keywords.

world.plot(column='val', cmap='Greens', ax=ax, legend=True, cax=cax, legend_kwds={'shrink':0.5})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文