无法绘制GeoDataFrame来显示同一区域随时间的变化。

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

我有一个geopandas.geodataframe.GeoDataFrame,有3列:the_geom,财产犯罪率,年份。注意,这里的the_geom只是弗吉尼亚州的一个多边形。我想显示这个犯罪率随时间的变化。这是我目前掌握的情况。

sns.set_style("white")
fig, axs = plt.subplots(nrows=3, ncols=4, sharex=True, sharey=True, figsize=(10,5))
fig.tight_layout(pad=3.0)

i = 0
for year in range(1994, 2015, 2):
    subdf = c7[c7['Year']==year]

    divider = make_axes_locatable(axs[i//4, i%4])
    cax = divider.append_axes("right", size="5%", pad=0)
    axs[i//4, i%4].set_title(str(year))

    subdf.plot(ax=axs[i//4, i%4], 
           column='Property crime rate',
           cmap='RdBu_r',
           cax=cax,
           legend=True
          )
    axs[i//4, i%4].axis('off')
    i+=1

axs[i//4, i%4].axis('off')

enter image description here

问题是所有的州都是一样的颜色 因为右边的图例使用的是不同的比例尺 我想让它们共享同一个比例尺,这样你就可以看到颜色随时间的变化。像sns.FacetGrid()这样的东西似乎可以工作,但我无法让它与GeoDataFrames一起工作。当我使用下面的plt.plot时,它不显示多边形。

g = sns.FacetGrid(c7, col="Year", hue="Property crime rate")
g = (g.map(plt.plot, "Property crime rate").add_legend()) 

enter image description here

当我试着用gpd.GeoDataFrame.plot替换plt.plot时,我得到以下错误。

g = sns.FacetGrid(c7, col="Year", hue="Property crime rate")
g = (g.map(gpd.GeoDataFrame.plot, "Property crime rate").add_legend())

AttributeError: 'Series' object has no attribute 'geometry'

谁能帮帮我?

python matplotlib data-visualization seaborn geopandas
1个回答
1
投票

你是将每一年的数据独立地绘制在其他的数据上,所以函数只是每次给一个值分配一个颜色。为了在子图之间共享彩色图,你最简单的选择是在子图中指定整体的最小值和最大值。subdf.plot. 我这里只是猜测最佳值)。

subdf.plot(ax=axs[i//4, i%4], 
           column='Property crime rate',
           cmap='RdBu_r',
           cax=cax,
           legend=True,
           vmin=1700,
           vmax=4100
          )

0
投票

好吧,经过一番修整,我找到了解决方案。我没有尝试@martinfleis的vmin和vmax,所以那也可能有用。我放弃了plt.Legend(),改用plt.colorbar(),似乎效果很好。

import matplotlib
import matplotlib.cm as cm #color mapping function
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import seaborn as sns
%matplotlib inline

sns.set_style("white")

# c7 is my geodataframe
va = c7[c7.State=='Virginia'].sort_values(by='Year').copy(deep=True)
va.reset_index(drop=True, inplace=True)

minima = min(va["Property crime rate"])
maxima = max(va["Property crime rate"])
norm = matplotlib.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.cool)

fig, ax = plt.subplots(1, 1, figsize=(15,8))
ax.axis('off')

axinset1 = inset_axes(ax, loc='center right', borderpad=-7, # negative value borderpad pushes it out
                      width="2%", height="40%") # width/height = X% of parent_bbox width/height
cb = plt.colorbar(cax=axinset1, mappable=mapper)
cb.set_label("Property Crime Rate", labelpad=20)
fig.suptitle('Virginia\'s Property Crime Rate Over Time', y=1, fontsize=20) # y<1 brings down, y>1 brings up

for idx, row in va.iterrows():
    ax = fig.add_subplot(3, 4, idx+1)
    ax.set_title("{}".format(row.Year))
    va[va.Year == row.Year].plot(ax=ax, color=mapper.to_rgba(row["Property crime rate"]))
    ax.axis('off')
ax.axis('off')
plt.show()

产生了。enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.