子批次中的分类数据

2024-06-09 10:16:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pandas数据框,包含20列,包含数字和分类数据的混合。我要绘制一个5x4矩阵的数据图。使用matplotlib和subplot,我现在可以绘制所有数值数据的图形,但是对于我来说,我不知道如何包括分类数据。在

我想要一些

df['RBC'].value_counts().plot(kind='bar')

但在一个小插曲里。在

下面是一些代码(为了简洁起见,我省略了一些重复部分)。在

^{pr2}$

Tags: 数据图形pandasdfplotmatplotlibvalue绘制
1条回答
网友
1楼 · 发布于 2024-06-09 10:16:32

你真的应该发布你尝试过的代码和一些示例数据。否则不可能知道最好的方法。但是,我认为您可能需要尝试以下方法,即使用matplotlibAPI而不是pandas,这样可以更好地控制每个绘图中的内容:

from matplotlib import pyplot as plt
fig, axes = plt.subplots(5, 4)   # axes is a numpy array of pyplot Axes
axes = iter(axes.ravel())   # set up an iterator for the set of axes. 

categoricals = df.columns[df.dtypes == 'category']
numeric = df.columns[df.dtypes != 'category']

for col in categoricals: 
    ax = df[col].value_counts().plot(kind='bar', ax=axes.next())
    # do other stuff with ax, formatting etc.  
    # the plot method returns the axis used for the plot for further manipulation

for col in numeric: 
     ax = df[col].plot(ax=axes.next())
     # etc. 

这只是给你一些想法,因为我不知道你的数据的细节,你想如何绘制每一列,你有什么样的数据类型等等

相关问题 更多 >