FRED Maps¶
This documentation serves as a quick start guide to FredMaps. The FRED Maps API enables developers to pull regional data or shape files.
Setup¶
In [ ]:
Copied!
import geopandas as gpd
import matplotlib.pyplot as plt
from rich import print
import pyfredapi as pf
import geopandas as gpd
import matplotlib.pyplot as plt
from rich import print
import pyfredapi as pf
Get geo-series info¶
In [ ]:
Copied!
info = pf.get_geoseries_info(series_id="WIPCPI")
print(info)
info = pf.get_geoseries_info(series_id="WIPCPI")
print(info)
Get Regional Data¶
get_geoseries returns a cross section of data that are published by region (e.g. state, county, MSA). Similar to the get_series function in FredSeries, get_geoseries returns a GeoseriesData object that contains the metadata for the series and the requested data.
In this example, we pull the U.S. state per capital personal income between 2010-2021.
In [ ]:
Copied!
per_capita_personal_income_by_state = pf.get_geoseries(
series_id="WIPCPI",
start_date="2010-01-01",
end_date="2021-01-01",
)
per_capita_personal_income_by_state = pf.get_geoseries(
series_id="WIPCPI",
start_date="2010-01-01",
end_date="2021-01-01",
)
In [ ]:
Copied!
per_capita_personal_income_by_state_df = per_capita_personal_income_by_state.data
per_capita_personal_income_by_state_df = per_capita_personal_income_by_state.data
In [ ]:
Copied!
per_capita_personal_income_by_state_df = per_capita_personal_income_by_state_df[
per_capita_personal_income_by_state_df.date
== per_capita_personal_income_by_state_df.date.max()
]
per_capita_personal_income_by_state_df = per_capita_personal_income_by_state_df[
per_capita_personal_income_by_state_df.date
== per_capita_personal_income_by_state_df.date.max()
]
In [ ]:
Copied!
per_capita_personal_income_by_state_df
per_capita_personal_income_by_state_df
Get Shape Files¶
get_shape_files returns the shape files as json in in Well-known text (WKT) format.
In [ ]:
Copied!
state_shapes_files = pf.get_shape_files(shape="state")
state_shapes_files = pf.get_shape_files(shape="state")
In [ ]:
Copied!
pf.get_shape_files(shape="state")
pf.get_shape_files(shape="state")
In [ ]:
Copied!
state_shapes_files
state_shapes_files
In [ ]:
Copied!
state_gdf = gpd.GeoDataFrame.from_features(state_shapes_files["features"])
state_gdf = gpd.GeoDataFrame.from_features(state_shapes_files["features"])
In [ ]:
Copied!
joined_df = state_gdf[["geometry", "state_fips"]].merge(
per_capita_personal_income_by_state_df,
left_on=["state_fips"],
right_on=["code"],
)
joined_df = state_gdf[["geometry", "state_fips"]].merge(
per_capita_personal_income_by_state_df,
left_on=["state_fips"],
right_on=["code"],
)
In [ ]:
Copied!
joined_df.date.max()
joined_df.date.max()
In [ ]:
Copied!
fig, ax = plt.subplots(figsize=(10, 10))
fig, ax = plt.subplots(figsize=(10, 10))
In [ ]:
Copied!
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.axes_grid1 import make_axes_locatable
In [ ]:
Copied!
fig, ax = plt.subplots(1, 1)
fig, ax = plt.subplots(1, 1)
In [ ]:
Copied!
divider = make_axes_locatable(ax)
divider = make_axes_locatable(ax)
In [ ]:
Copied!
cax = divider.append_axes("bottom", size="5%", pad=0.1)
cax = divider.append_axes("bottom", size="5%", pad=0.1)
In [ ]:
Copied!
joined_df[["geometry", "value"]].plot(
column="value",
ax=ax,
legend=True,
cax=cax,
legend_kwds={
"label": "Per Capita Personal Income 2022",
"orientation": "horizontal",
},
)
joined_df[["geometry", "value"]].plot(
column="value",
ax=ax,
legend=True,
cax=cax,
legend_kwds={
"label": "Per Capita Personal Income 2022",
"orientation": "horizontal",
},
)
In [ ]:
Copied!
fig
fig
In [ ]:
Copied!
# Create a figure and axis
fig, ax = plt.subplots(figsize=(10, 10))
# Plot the GeoDataFrame
# Use the 'bea_regi_1' column to assign colors
joined_df.plot(ax=ax, column="value", legend=True, edgecolor="black", cmap="Set3")
# Optional: Customize the legend
ax.get_legend().set_bbox_to_anchor((1, 0.5)) # Adjust position
ax.get_legend().set_title("BEA Region")
# Create a figure and axis
fig, ax = plt.subplots(figsize=(10, 10))
# Plot the GeoDataFrame
# Use the 'bea_regi_1' column to assign colors
joined_df.plot(ax=ax, column="value", legend=True, edgecolor="black", cmap="Set3")
# Optional: Customize the legend
ax.get_legend().set_bbox_to_anchor((1, 0.5)) # Adjust position
ax.get_legend().set_title("BEA Region")
In [ ]:
Copied!
# Optional: Customize the legend
ax.get_legend().set_bbox_to_anchor((1, 0.5)) # Adjust position
ax.get_legend().set_title("U.S. Per Capita Personal Income")
# Optional: Customize the legend
ax.get_legend().set_bbox_to_anchor((1, 0.5)) # Adjust position
ax.get_legend().set_title("U.S. Per Capita Personal Income")
In [ ]:
Copied!
fig
fig
In [ ]:
Copied!
fig.show()
fig.show()
In [ ]:
Copied!
joined_df.plot(ax=ax, column="value", legend=True, edgecolor="black", cmap="Set1")
joined_df.plot(ax=ax, column="value", legend=True, edgecolor="black", cmap="Set1")
In [ ]:
Copied!
plt.show()
plt.show()
In [ ]:
Copied!
import matplotlib
print(matplotlib.get_backend())
matplotlib.use("agg")
import matplotlib
print(matplotlib.get_backend())
matplotlib.use("agg")
In [ ]:
Copied!