You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Seaborn绘制奥克兰Top10区域柱状图X轴标签异常求助

解决Seaborn柱状图X轴显示所有区域的问题

问题描述

尝试绘制柱状图突出奥克兰地区赌博支出排名前10的区域,已筛选出目标数据,但Seaborn生成的图表X轴仍显示奥克兰所有区域的标签,导致拥挤混乱,仅需显示前10个区域的标签。

数据集快照

Date,AU2017_code,crime,n,Pop,AU_GMP_PER_CAPITA,Dep_Index,AU2017_name,TA2018_name,TALB
2018-02-01,500100.0,Abduction,0.0,401.0,28.890063,10.0,Awanui,Far North District,Far North District
2018-03-01,500100.0,Abduction,0.0,402.0,28.890063,10.0,Awanui,Far North District,Far North District
2018-04-01,500100.0,Abduction,0.0,408.0,28.890063,10.0,Awanui,Far North District,Far North District
2018-05-01,500100.0,Abduction,0.0,409.0,28.890063,10.0,Awanui,Far North District,Far North District
2018-06-01,500100.0,Abduction,0.0,410.0,28.890063,10.0,Awanui,Far North District,Far North District

原代码

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns


# Extract the year from the Date column and create a new 'Year' column
merged_data['Year'] = merged_data.index.year

# Filter data for areas that come under Auckland in the TA2018_name column
auckland_data = merged_data[merged_data['TA2018_name'] == 'Auckland']

# Calculate the average AU_GMP_PER_CAPITA for each area within Auckland
avg_gmp_per_area = auckland_data.groupby('AU2017_name')['AU_GMP_PER_CAPITA'].mean()

# Select the top 10 areas by AU_GMP_PER_CAPITA within Auckland
top_10_areas = avg_gmp_per_area.nlargest(10).index

# Further filter the auckland_data to include only the top 10 areas
filtered_data = auckland_data[auckland_data['AU2017_name'].isin(top_10_areas)]

# Use seaborn to create the barplot
sns.barplot(x='AU2017_name', y='AU_GMP_PER_CAPITA', hue='Year', data=filtered_data)

plt.title('The top 10 areas for gambling spend in Auckland')
plt.xticks(rotation=60)
plt.legend(title='Year', loc='upper right')
plt.figure(figsize = (20, 10))
plt.show()

问题根源

  1. Seaborn默认保留原数据中AU2017_name的所有类别标签,即便过滤了数据,类别元信息未更新
  2. plt.figure(figsize=(20,10))放在绘图之后,导致实际图表未应用指定尺寸,加剧标签拥挤

修正后的代码

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

# 提取年份列
merged_data['Year'] = merged_data.index.year

# 筛选奥克兰地区数据
auckland_data = merged_data[merged_data['TA2018_name'] == 'Auckland']

# 计算各区域人均GMP平均值
avg_gmp_per_area = auckland_data.groupby('AU2017_name')['AU_GMP_PER_CAPITA'].mean()

# 获取前10区域
top_10_areas = avg_gmp_per_area.nlargest(10).index

# 过滤出前10区域数据
filtered_data = auckland_data[auckland_data['AU2017_name'].isin(top_10_areas)]

# 将区域列转为有序分类,仅保留前10个区域的类别
filtered_data['AU2017_name'] = pd.Categorical(filtered_data['AU2017_name'], categories=top_10_areas, ordered=True)

# 先创建绘图对象并设置尺寸
plt.figure(figsize=(20, 10))

# 绘制柱状图
sns.barplot(x='AU2017_name', y='AU_GMP_PER_CAPITA', hue='Year', data=filtered_data)

plt.title('奥克兰地区赌博支出Top10区域')
plt.xticks(rotation=60)
plt.legend(title='年份', loc='upper right')
plt.tight_layout()  # 自动调整布局,避免标签被截断
plt.show()

关键修正点

  • 将AU2017_name转为有序分类类型,明确指定仅包含前10个区域的类别,让Seaborn只绘制目标标签
  • 将图表尺寸设置移到绘图前,确保生效
  • 添加plt.tight_layout()自动调整布局,防止X轴标签被截断

内容的提问来源于stack exchange,提问作者YShastri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 04:11:07