繁体   English   中英

如何使用 matplotlib 动画向 geopandas 图中添加颜色条

[英]How to add a colorbar to geopandas plot with matplotlib animation

我正在尝试为不使用 FuncAnimation 的 geopandas 绘图设置动画。 基本上 eu_private_map 是一个 geopandas.geodataframe.GeoDataFrame 和其他字段,它包含几个列,其中包含连续 10 年不同欧盟国家的参数 x 的值。 每列是 x 一年的值。

from matplotlib.animation import FuncAnimation
from IPython.display import HTML
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig = plt.figure() 
ax1 = fig.add_subplot(1,1,1)

def animate(year):
        ax1.clear()
        my_map = eu_private_map.plot(ax=ax1, column=year, vmin=0, vmax=30, legend=False)
        divider = make_axes_locatable(ax1)
        cax = divider.append_axes("right", size="5%", pad=0.05)
        plt.colorbar(my_map, cax=cax)

# the FuncAnimation function iterates through our animate function using the steps array
years = eu_private.columns.drop('country').tolist()
ani = FuncAnimation(fig, animate, years, interval=1000, blit=False)

HTML(ani.to_jshtml())

颜色条让我很难受。 一方面,如果我设置legend = True,我不喜欢它的大小(见图)与图形不同的事实,它会产生一种奇怪的效果(见图)。 我试过了,但出现错误:AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None'。 但是我无法发现代码中的问题,是否有助于同时解决这两个问题? 非常感谢

在此处输入图片说明 在此处输入图片说明

在尝试了许多对 geopandas 和 animate 不起作用的方法之后,我最终找到了一个解决方案,使其工作成为将颜色条从 animate 函数中分离到绘图的 init 函数的主要思想。

from matplotlib.animation import FuncAnimation
from IPython.display import HTML
from mpl_toolkits.axes_grid1 import make_axes_locatable


fig = plt.figure(figsize=(20,10)) 
ax1 = fig.add_subplot(1,1,1)

def animate(year):
        ax1.clear()
        eu_private_map.plot(color='gainsboro', ax=ax1, linewidth=0.6, edgecolor='1')
        sm = eu_private_map.dropna().plot(ax=ax1, column=year, vmin=0, vmax=30, cmap = 'viridis',
                                              legend=False, linewidth=0.6, edgecolor='1')
        ax1.set_title(year, fontdict={'fontsize': '23'})
        ax1.axis('off')

def init():
    # Create colorbar as a legend
    sm = plt.cm.ScalarMappable(cmap='viridis', norm=plt.Normalize(vmin=0, vmax=30))
    # empty array for the data range
    sm._A = []
    # add the colorbar to the figure

    divider = make_axes_locatable(ax1)
    cax = divider.append_axes("right", size="5%", pad=0.5)


    cbar = fig.colorbar(sm, cax=cax)
    cbar.ax.tick_params(labelsize=14) 


# the FuncAnimation function iterates through our animate function using the steps array
years = eu_private.columns.drop('country').tolist()[::-1]
ani = FuncAnimation(fig, animate, years, interval=1000, blit=False, init_func=init)

HTML(ani.to_jshtml())

嘿,找到了一种制作对数标度颜色图norm=matplotlib.colors.LogNorm()可以解决问题:

matplotlib 散点图中的对数颜色条

你也可以像这样添加 vmin 和 vmax: norm=colors.LogNorm(vmin=vmin, vmax=vmax)

虽然它仅适用于 vmin 和 vmax 正值,但请记住。 例如norm=colors.LogNorm(vmin=0.1, vmax=1000000)

对于init_func此问题的新答案寻求者,其想法是将init_func参数用于FuncAnimation函数。 这是回答here

init_func将呈现init_func ,而animate函数不会呈现颜色条。

限制:我假设颜色条及其刻度不会改变,这个答案将在更新图表轴时保持旧的颜色条完好无损。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM