如何解决AttributeError:在绘制子图时,"numpy.ndarray"对象没有属性"get_figure" [英] How to resolve AttributeError: 'numpy.ndarray' object has no attribute 'get_figure' when plotting subplots
本文介绍了如何解决AttributeError:在绘制子图时,"numpy.ndarray"对象没有属性"get_figure"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试运行代码单元时遇到问题.我正在尝试为数据框中的每个变量绘制散点图,但遇到了一个我不太确定的错误.你能帮忙吗?
我的代码:
fig, axes = plt.subplots(nrows=3, ncols=7, figsize=(12,10))对于 xcol, ax in zip(df[df.columns], axes):df.plot(kind='scatter', x=xcol, y='price', ax=ax, alpha=0.5, color='r')
返回错误:AttributeError: 'numpy.ndarray' 对象没有属性 'get_figure'
解决方案
-
fig,axes = plt.subplots(nrows = 3,ncols = 7,figsize =(12,10))
创建3组,每组7个AxesSubplot
对象立>
array([[< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:>;, <AxesSubplot:>],[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>]],dtype = object)
- 通过使用
zip(df [df.columns],axes)
进行压缩,您将得到如下所示的内容:- 这是错误的来源;如您所见,循环中的
ax
是一个array
,而不是AxesSubplot
.
- 这是错误的来源;如您所见,循环中的
<代码> [('col1',array([< AxesSubplot:> ;,< AxesSubplot:> ;,< AxesSubplot:> ;,< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:>],dtype = object)),('col2',array([< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:>,< AxesSubplot:> ;、<AxesSubplot:>], dtype=object)),('col3', array([<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>,< AxesSubplot:>],dtype = object))]]
- 您想要的是,将一列压缩到一个子图,这可以通过使用列表推导解压缩所有轴子图或使用
axes.ravel()
来完成,然后压缩他们到列名.I'm an encountering an issue when I try to run a cell of code. I'm attempting to make a scatter plot for each of the variables in my dataframe, but am encountering an error i'm not too sure about. Can you help?
My code:
fig, axes = plt.subplots(nrows=3, ncols=7, figsize=(12,10)) for xcol, ax in zip(df[df.columns], axes): df.plot(kind='scatter', x=xcol, y='price', ax=ax, alpha=0.5, color='r')
Returned error: AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'
解决方案fig, axes = plt.subplots(nrows=3, ncols=7, figsize=(12,10))
creates 3 groups of 7AxesSubplot
objects
array([[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>], [<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>], [<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>]], dtype=object)
- By zipping with
zip(df[df.columns], axes)
, you're getting something like the following:- This is the sources of the error; as you can see,
ax
from the loop, is anarray
, not anAxesSubplot
.
- This is the sources of the error; as you can see,
[('col1', array([<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>], dtype=object)), ('col2', array([<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>], dtype=object)), ('col3', array([<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>], dtype=object))]
- What you want is, to zip one column to one subplot, which can be done by unpacking all the axes subplots, with a list comprehension, or using
axes.ravel()
, and then zipping them to the column names.numpy.ravel
returns a flattened array.
- Use
df.columns
, notdf[df.columns]
, to get the column names.
# the list comprehension unpacks all the axes zip(df.columns, [x for v in axes for x in v]) # which results in one column name per subplot [('col1', <AxesSubplot:>), ('col2', <AxesSubplot:>), ('col3', <AxesSubplot:>), ('col4', <AxesSubplot:>), ('col5', <AxesSubplot:>), ('col6', <AxesSubplot:>), ('col7', <AxesSubplot:>), ('col8', <AxesSubplot:>), ('col9', <AxesSubplot:>), ('col10', <AxesSubplot:>), ('col11', <AxesSubplot:>), ('col12', <AxesSubplot:>), ('col13', <AxesSubplot:>), ('col14', <AxesSubplot:>), ('col15', <AxesSubplot:>), ('col16', <AxesSubplot:>), ('col17', <AxesSubplot:>), ('col18', <AxesSubplot:>), ('col19', <AxesSubplot:>), ('col20', <AxesSubplot:>), ('col21', <AxesSubplot:>)]
Example
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt # load sample data df = sns.load_dataset('car_crashes') # setup figure fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(12, 10)) # iterate and plot subplots for xcol, ax in zip(df.columns[1:-1], [x for v in axes for x in v]): df.plot.scatter(x=xcol, y='speeding', ax=ax, alpha=0.5, color='r')
这篇关于如何解决AttributeError:在绘制子图时,"numpy.ndarray"对象没有属性"get_figure"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文