When trying to create a plot with multiple figures, I get this error
AttributeError: 'numpy.ndarray' object has no attribute 'xaxis'
My code is:
plt.figure()
n_cols = 6
n_rows = int(len(df.foo.unique()) / n_cols) # 4
fn, axis = plt.subplots(nrows=n_rows, ncols=n_cols, figsize=(30, 25))
for i in range(len(df.foo.unique())):
sb.scatterplot(x='var1', y='var2', hue='foo', data=df[df.foo == i], ax=axis[i])
And foo
is a variable from value 0
to 23
, so it has length = 24
.
But it works when I hyperparametrize nrows
and ncols
ONLY assigning the following values. By doing the following, I get success:
plt.figure()
fn, axis = plt.subplots(nrows=1, ncols=12, figsize=(30, 25))
for i in range(len(df.foo.unique())):
sb.scatterplot(x='var1', y='var2', hue='foo', data=df[df.foo == i], ax=axis[i])
Why is this happening?
A deeper view of the error:
File c:\Users\user\anaconda3\envs\ml\lib\site-packages\seaborn\_decorators.py:46, in _deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
36 warnings.warn(
37 "Pass the following variable{} as {}keyword arg{}: {}. "
38 "From version 0.12, the only valid positional argument "
(...)
43 FutureWarning
44 )
45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46 return f(**kwargs)
File c:\Users\user\anaconda3\envs\ml\lib\site-packages\seaborn\relational.py:825, in scatterplot(x, y, hue, style, size, data, palette, hue_order, hue_norm, sizes, size_order, size_norm, markers, style_order, x_bins, y_bins, units, estimator, ci, n_boot, alpha, x_jitter, y_jitter, legend, ax, **kwargs)
822 if not p.has_xy_data:
823 return ax
--> 825 p._attach(ax)
827 p.plot(ax, kwargs)
829 return ax
File c:\Users\user\anaconda3\envs\ml\lib\site-packages\seaborn\_core.py:1134, in VectorPlotter._attach(self, obj, allowed_types, log_scale)
...
-> 1134 axis = getattr(ax, f"{var}axis")
1135 seed_data = self.plot_data[var]
1136 if var_type == "categorical":
AttributeError: 'numpy.ndarray' object has no attribute 'xaxis'