This example shows the steps to subplot with every individual plot occupies same area in figure.
- Shapes of US states are generated from Free Blank United States Map in SVG - Resources | Simplemaps.com
- Population data is accessed from Historical Population Change Data (1910-2020) (census.gov)
Example
>>> load_example_data("geodataframe", ["us_population", "us_states_shapes"])
>>> from teradataml import subplots
>>> fig, axis = subplots(2, 2)
>>> fig.height = 1200
>>> fig.heading = "Change in population density in US across four decades."
>>> axis
[AxesSubplot(position=(1, 1), span=(1, 1)), AxesSubplot(position=(1, 2), span=(1, 1)), AxesSubplot(position=(2, 1), span=(1, 1)), AxesSubplot(position=(2, 2), span=(1, 1))]
- Prepare the Data for subplotting.
>>> us_population = DataFrame("us_population")
>>> us_population
location_type population_year population state_name Georgia State 1930 2908506.0 Georgia State 1950 3444578.0 Georgia State 1960 3943116.0 Georgia State 1970 4589575.0 Georgia State 1990 6478216.0 Georgia State 2000 8186453.0 Georgia State 1980 5463105.0 Georgia State 1940 3123723.0 Georgia State 1920 2895832.0 Georgia State 1910 2609121.0
>>> us_states_shapes = GeoDataFrame("us_states_shapes")
>>> us_states_shapes
state_name state_shape id NM New Mexico POLYGON ((472.45213 324.75551, VA Virginia POLYGON ((908.75086 270.98255, ND North Dakota POLYGON ((556.50879 73.847349, OK Oklahoma POLYGON ((609.50526 322.91131, WI Wisconsin POLYGON ((705.79187 134.80299, RI Rhode Island POLYGON ((946.50841 152.08022, HI Hawaii POLYGON ((416.34965 514.99923, KY Kentucky POLYGON ((693.17367 317.18459, WV West Virginia POLYGON ((836.73002 223.71281, NJ New Jersey POLYGON ((916.80709 207.30914,
- Join shapes with population and filter only 1990 data.
>>> population_data = us_states_shapes.join(us_population, on=us_population.state_name == us_states_shapes.state_name, lsuffix="us", rsuffix="t2")
>>> population_data = population_data.select(["us_state_name", "state_shape", "population_year", "population"])
- Find out the minimum and maximum population. This helps in coloring the plot.
>>> population_data.assign(min_population=population_data.population.min(), max_population=population_data.population.max(), drop_columns=True)
max_population min_population 0 39538223.0 55036.0
>>> population_data_2020 = population_data[population_data.population_year == 2020]
>>> population_data_2010 = population_data[population_data.population_year == 2010]
>>> population_data_2000 = population_data[population_data.population_year == 2000]
>>> population_data_1990 = population_data[population_data.population_year == 1990]
- Generate subplot.
- Plot population_data_1990 on first axis.
>>> plot_1990 = population_data_1990.plot(y=(population_data_1990.population, population_data_1990.state_shape), cmap='rainbow', figure=fig, ax=axis[0], reverse_yaxis=True, vmin=55036.0, vmax=39538223.0, title="US 1990 Population", xlabel="", ylabel="")
- Plot population_data_2000 on second axis.
>>> plot_2000 = population_data_2000.plot(y=(population_data_2000.population, population_data_2000.state_shape), cmap='rainbow', figure=fig, ax=axis[1], reverse_yaxis=True, vmin=55036.0, vmax=39538223.0, title="US 2000 Population", xlabel="", ylabel="")
- Plot population_data_2010 on third axis.
>>> plot_2010 = population_data_2010.plot(x=population_data_2010.population_year, y=(population_data_2010.population, population_data_2010.state_shape), cmap='rainbow', figure=fig, ax=axis[2], reverse_yaxis=True, vmin=55036.0, vmax=39538223.0, title="US 2010 Population", xlabel="", ylabel="", xtick_values_format="")
- Plot population_data_2020 on fourth axis.
>>> plot = population_data_2020.plot(x=population_data_2020.population_year, y=(population_data_2020.population, population_data_2020.state_shape), cmap='rainbow', figure=fig, ax=axis[3], reverse_yaxis=True, vmin=55036.0, vmax=39538223.0, title="US 2020 Population", xlabel="", ylabel="", xtick_values_format="")
- Plot population_data_1990 on first axis.
>>> plot.show()