python - 在创建计数图 ( ) 时为分类数据设置顺序会导致错误的图
问题描述
我的数据框中有一列针对不同年龄组,其中包含 174 行数据。示例如下: 在此处输入图像描述
我打印此列的 countplot() 的代码如下:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
sns.set(rc = {'figure.figsize':(15,8)})
sns.countplot(data = variable, x = 'Q2')
plt.title("Respondents as per age group")
结果输出为: 按年龄组划分的受访者人数
可以看出,x轴上的类别顺序不正确。
我使用以下代码设置顺序:
age_order = ['15-19','20-29','30-39','40-49','50-59','60 and above']
sns.countplot(x = 'Q2', data = variable, order = age_order)
plt.title("Respondents as per age group")
但结果是:
这显然是不正确的。
我不知道为什么会发生这种情况以及如何纠正这种情况。
解决方案
推荐阅读
- windows - Docker-Compose 在 Windows Server 2022 上不起作用
- ios - 在给定 PHContentEditingInput 的情况下获取资产的原始文件名
- reactjs - serve -s build 显示黑屏是我尝试更改以更改响应中的上下文路径
- node.js - 如何从邮递员向 mongodb 插入数据?
- python - 寻找将字典键(基于值)划分为列表列表的最快方法,以使每个列表不超过某个阈值
- linux - 使用列名而不是数字过滤条件
- reactjs - 子组件不反映对父组件状态所做的更改
- java - Netbeans 12.4 - 创建主/详细示例表单的选项在哪里?
- apache-spark - 结构化流的startingOffest和Checkpoint
- python - 如何使随机变量的输入部分?