【问题标题】:How to change the font size of the color bar of a GeoPandas choropleth plot如何更改 GeoPandas 等值线图颜色条的字体大小
【发布时间】:2022-03-26 07:21:58
【问题描述】:

当我尝试使用 legend_kwds 参数更改颜色条的字体大小时,我不断收到此错误

TypeError: init() 得到了一个意外的关键字参数 'fontsize'

ax = df.plot(figsize=(20,16), alpha=0.8, column='value', legend=True, cmap='OrRd', legend_kwds={'fontsize':20})
    
plt.show()

有谁知道如何使用 GeoPandas 增加颜色条的字体大小?我似乎找不到有效的关键字。我正在使用 GeoPandas 0.8.1 和 Matplotlib 3.3.1。

【问题讨论】:

    标签: python matplotlib geospatial geopandas


    【解决方案1】:

    您可以使用 matplotlib 解决方法,而不是在单个语句 geopandas 的 plot 函数中传递所有复杂的参数。

    import numpy as np
    import geopandas as gpd
    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    
    # for demo purposes, use the builtin data
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    africa = world[world.continent=='Africa']
    maxv, minv = max(africa.pop_est), min(africa.pop_est)
    
    fig, ax = plt.subplots(figsize=(7,6))
    divider = make_axes_locatable(ax)
    
    # create `cax` for the colorbar
    cax = divider.append_axes("right", size="5%", pad=0.1)
    
    # plot the geodataframe specifying the axes `ax` and `cax` 
    africa.plot(column="pop_est", cmap='magma', legend=True, \
                vmin=minv, vmax=maxv, ax=ax, cax=cax)
    
    # manipulate the colorbar `cax`
    cax.set_ylabel('pop_est', rotation=90)
    # set `fontsize` on the colorbar `cax`
    cax.set_yticklabels(np.linspace(minv, maxv, 10, dtype=np.dtype(np.uint64)), {'fontsize': 8})
    
    plt.show()
    

    输出图:

    【讨论】:

    • @JackSimpson 这是 matplotlib 的标准程序。 Geopandas 的绘图函数是一个粗略的包装函数,远非完美。
    【解决方案2】:

    我也遇到了同样的问题,发现答案不正确——图例上的标签其实是错误的。

    所以用同样的思路,可以调用

    cax.tick_params(labelsize='20')

    这将保留绘图创建的默认刻度,但您可以更改详细信息。

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 1970-01-01
      • 2017-04-03
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多