简体   繁体   English

如何在没有将轴存储在 numpy.ndarray 中的情况下创建具有多列的计数图?

[英]How do I create a count plot with multiple columns without the axes being stored in a numpy.ndarray?

I'm new to coding and this is my first post.我是编码新手,这是我的第一篇文章。 Sorry if it could be worded better!对不起,如果它的措辞更好!

I'm taking a free online course, and for one of the projects I have to make a count plot with 2 subplot columns.我正在参加免费的在线课程,对于其中一个项目,我必须制作一个带有 2 个子图列的计数图。

I've managed to make a count plot with multiple subplots using the code below, and all of the values are correct.我已经设法使用下面的代码制作了一个包含多个子图的计数图,并且所有值都是正确的。

fig = sns.catplot(x = 'variable', hue = 'value', order = ['active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke'], col='cardio', data = df_cat, kind = 'count')

But because of the way I've done it, the fig.axes is stored in a 2 dimensional array.但是由于我这样做的方式, fig.axes 存储在二维数组中。 The only difference between both rows of the array is the title (cardio = 0 or cardio = 1).数组两行之间的唯一区别是标题(有氧运动 = 0 或有氧运动 = 1)。 I'm assuming this is because of the col='cardio'.我假设这是因为 col='cardio'。 Does the col argument always cause the fig.axes to be stored in a 2D array? col 参数是否总是导致 fig.axes 存储在二维数组中? Is there a way around this or do I have to completely change how I'm making my graph?有没有办法解决这个问题,还是我必须完全改变我制作图表的方式?

I'm sure it's not usually a problem, but because of this, when I run my program through the test module, it fails since some of the functions in the test module don't work on numpy.ndarrays.我确信这通常不是问题,但正因为如此,当我通过测试模块运行我的程序时,它会失败,因为测试模块中的某些函数不适用于 numpy.ndarrays。

I pass the test if I change the reference from fig.axes[0] to fig.axes[0,0], but obviously I cant just change the test module to pass.如果我将引用从 fig.axes[0] 更改为 fig.axes[0,0],我通过了测试,但显然我不能只更改测试模块以通过。

I found something.我发现了一些东西。 This is just an implementation detail, so it would be nuts to rely on it.这只是一个实现细节,因此依赖它会很疯狂。 If you set col_wrap, then you get an axes ndarray of a different shape.如果你设置 col_wrap,那么你会得到一个不同形状的轴 ndarray。

Reproduced like this:转载如下:

import seaborn as sns 

# I don't have your data but I have this example
tips = sns.load_dataset("tips")

fig = sns.catplot(x='day', hue='sex', col='time', data=tips, kind='count', col_wrap=2)
fig.axes.shape

And it has shape (2,) ie it's 1D.它具有形状(2,),即它是一维的。 seaborn==0.11.2.海生==0.11.2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个行为类似于numpy.ndarray的类而又不继承numpy.ndarray的类? - How do I write a class that behaves like a numpy.ndarray without subclassing numpy.ndarray? 如何在没有ambuigity的情况下使用numpy.ndarray合并特定的轴 - How to merge specific axes without ambuigity with numpy.ndarray 如何在DataFrame的列中存储numpy.ndarray - How to store numpy.ndarray in the columns of DataFrame numpy.ndarray中的值计数 - Count of values in numpy.ndarray Numpy.ndarray:按数组的特定轴进行迭代 - Numpy.ndarray: iteration by specific axes of an array 'numpy.ndarray' 对象如何不是 'numpy.ndarray' 对象? - How do 'numpy.ndarray' object do not 'numpy.ndarray' object? 使用轴进行子图获取 AttributeError 的问题:'numpy.ndarray' 对象没有属性 'plot' - Prob using axes for subplot an get AttributeError: 'numpy.ndarray' object has no attribute 'plot' plt.subplots() 中的轴是“numpy.ndarray”对象,没有属性“plot” - Axes from plt.subplots() is a “numpy.ndarray” object and has no attribute “plot” 我如何轻松地将numpy.ndarray转换为numpy.array列表? - How do I easily convert a numpy.ndarray to a list of numpy.array? 如何从numpy.ndarray数据中排除行/列 - How to exclude rows/columns from numpy.ndarray data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM