Last active
October 3, 2025 00:27
-
-
Save bretttolbert/f05db0b58d44fa5e1bbc652ee8aa45f8 to your computer and use it in GitHub Desktop.
US states plot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| from matplotlib.patches import Rectangle | |
| import geopandas as gpd | |
| import fiona | |
| fiona.drvsupport.supported_drivers["KML"] = "rw" | |
| # Load detailed US states data from the U.S. Census Bureau | |
| # Source: https://www2.census.gov/geo/tiger/GENZ2022/shp/cb_2022_us_state_20m.zip | |
| states = gpd.read_file( | |
| "./data/cb_2022_us_state_20m/cb_2022_us_state_20m.kml", driver="KML" | |
| ) | |
| states_df = gpd.GeoDataFrame(states) | |
| for i in range(len(states_df)): | |
| states_df.loc[i, "Name"] = ( | |
| states_df.iloc[i]["Name"] | |
| .replace("<at><openparen>", "") | |
| .replace("<closeparen>", "") | |
| ) | |
| highlight_states = states[states["Name"].isin(["Alabama", "Florida"])] | |
| highlight_color = "orange" | |
| title = "States where you can be sentenced to death without a unanimous jury verdict" | |
| # Plotting | |
| fig, ax = plt.subplots(figsize=(10, 8)) | |
| states.plot(ax=ax, color="lightgrey", edgecolor="white") | |
| highlight_states.plot(ax=ax, color=highlight_color, edgecolor="black") | |
| """ | |
| # Annotate the states | |
| for idx, row in highlight_states.iterrows(): | |
| plt.annotate( | |
| text=row["Name"], | |
| xy=(row.geometry.centroid.x, row.geometry.centroid.y), | |
| horizontalalignment="center", | |
| fontsize=12, | |
| fontweight="bold", | |
| ) | |
| """ | |
| # ax.set_title(title, fontsize=15) | |
| ax.axis("off") | |
| plt.tight_layout() | |
| # Set the legend | |
| proxy_artist = Rectangle((0, 0), 1, 1, fc=highlight_color) | |
| ax.legend( | |
| handles=[proxy_artist], | |
| labels=[title], | |
| ) | |
| plt.show() |
bretttolbert
commented
Oct 3, 2025
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment