I want to plot some Census Data on a US map to achieve something like this: https://censusreporter.org/data/map/?table=B23025&geo_ids=01000US,040|01000US&primary_geo_id=01000US
But unlike the above link, I want to create an animation where I update the plot in a loop for different years. This is what I have till now:
def StatesPlot(df, data, start_year, end_year, cmap):
for year in range(start_year, end_year+1):
f,ax = plt.subplots(1,1, figsize=(10, 5.4), sharex=True, sharey=True, dpi=300)
f.tight_layout()
plt.title('United States Map - Variable: ' + data + ' Year: ' +str(year), fontsize=12)
ax.set_axis_off()
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="3%", pad=0, alpha=0.1)
clear_output(wait=True)
tmp = df[df['YEAR'] == year]
tmp.plot(data, ax=ax, alpha=0.5, cmap=cmap, edgecolor='k', legend=True, cax=cax, linewidth=0.1)
plt.show()
time.sleep(1)
StatesPlot(us_cont, 'EMPLOYMENT_STATUS', 2015, 2019, 'viridis')
This works fine. The issues is that I wanted to create this animation to compare census data for different years, but as of now, this also updated the scale (cax
) everytime, so I can't compare the data. How can I fix the scale outside of the loop and just update the plot?
I think you just want to set the max and min values for the colorbar in your
tmp.plot
line using thevmin
andvmax
arguments. Try adding something like this before your loop:And then changing your plot line to reflect those limits: