pandas 缩小颜色条图例

mmvthczy  于 2023-11-15  发布在  其他
关注(0)|答案(2)|浏览(126)

有没有办法让这个颜色条图例变小?比如现在的1/5或1/10,这样它就能更好地融合在一起?
我不知道你需要看多少代码,所以这里是一切:

  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
  3. import geopandas as gpd
  4. from descartes import PolygonPatch
  5. import pandas as pd
  6. import math
  7. import numpy as np
  8. world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
  9. world.loc[world['name'] == 'France', 'iso_a3'] = 'FRA'
  10. world.loc[world['name'] == 'Norway', 'iso_a3'] = 'NOR'
  11. world.loc[world['name'] == 'Somaliland', 'iso_a3'] = 'SOM'
  12. world.loc[world['name'] == 'Kosovo', 'iso_a3'] = 'RKS'
  13. world = world[(world.pop_est>0) & (world.name!="Antarctica")]
  14. world['val'] = 0
  15. fig, ax = plt.subplots(1, 1)
  16. df=pd.read_csv('data.csv', usecols=['SpatialDimValueCode','Location','Period','Dim1','FactValueNumeric'])
  17. def lerp(val, _max, _min):
  18. return math.pow((val - _min)/(_max - _min), 1/4)
  19. min_ = min(df[df['Dim1'] == 'Total']['FactValueNumeric'].tolist())
  20. max_ = max(df[df['Dim1'] == 'Total']['FactValueNumeric'].tolist())
  21. for index, country in df[(df['Period'] == 2016) & (df['Dim1'] == 'Total')].iterrows():
  22. if(country['SpatialDimValueCode'] in world.iso_a3.tolist()):
  23. world.loc[world['iso_a3'] == country['SpatialDimValueCode'], 'val'] = lerp(country['FactValueNumeric'], max_, min_)
  24. divider = make_axes_locatable(ax)
  25. cax = divider.append_axes("right", size="5%", pad="0.01%")
  26. world.plot(column='val', cmap='Greens', ax=ax, legend=True, cax=cax)
  27. ax.axis('off')
  28. plt.savefig('data.jpg', dpi=300, format='jpg',bbox_inches='tight', pad_inches=0)

字符串
x1c 0d1x的数据

bgtovc5b

bgtovc5b1#

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

js81xvg6

js81xvg62#

您可以使用图例关键字的shrink属性。

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

字符串

相关问题