I am trying to iterate through matplotlib subplots based on index, and it works fine for fig, axs = plt.subplots(4, 1)
, but it throws and error AttributeError: 'numpy.ndarray' object has no attribute 'set_aspect'
for fig, axs = plt.subplots(2, 2)
. Using Matplotlib version 3.2.1.
Here is an example setup. I have a dataframe of coordinates that represent polygons and they have categories like A,B,C, etc. I want to plot the polygons in various combinations (i.e., plot A and B on one plot, A and C on another plot, etc.) so I have these combinations defined in a dictionary. Then, I set up my subplots equal to the number of dictionary items, loop through the dictionary, and use the dictionary index number as the subplot index number.
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
from shapely import geometry
%matplotlib inline
df = pd.DataFrame(
{
"x1": [8.6,13.5,18.7,50.8,45.2,41.8,12.8,13.8,19.7,],
"y1": [81.4,76.7,68.8,54.5,50.8,44.1,19.8,15.3,13.3,],
"x2": [15.6,21.0,22.4,53.4,50.6,49.5,16.9,17.2,20.3,],
"y2": [82.2,76.2,74.0,53.0,49.5,43.1,21.1,18.4,20.3,],
"cat": ["A","A","A","B","B","B","C","C","C",],
}
)
display(df)
dict_combinations = {
"var1": ["A",],
"var2": ["A","B",],
"var3": ["B","C",],
"var4": ["A","C",],
}
This plotting works, using fig, axs = plt.subplots(4, 1)
:
fig, axs = plt.subplots(4, 1)
for index, (key, value) in enumerate(dict_combinations.items()):
df2 = df[df['cat'].isin(value)].copy()
for name, group in df2.groupby('cat'):
group = group.copy()
top = group[["x1", "y1"]].copy().rename(columns={"x1": "x", "y1": "y"})
bot = group[["x2", "y2"]].copy().rename(columns={"x2": "x", "y2": "y"})
bot = bot[::-1]
df3 = pd.concat([top, bot])
poly = geometry.Polygon(df3.values.tolist())
poly = gpd.GeoDataFrame([poly], columns={"geometry"}, crs="EPSG:4326")
poly.plot(ax=axs[index])
Changing fig, axs = plt.subplots(4, 1)
to fig, axs = plt.subplots(2, 2)
throws an error (AttributeError: 'numpy.ndarray' object has no attribute 'set_aspect'
). How can I fix this?