基于Python的Excel多级分类数据清晰可视化方案咨询
Hey there! Totally get why multi-label bar charts in Seaborn can feel cluttered for hierarchical Excel data—those overlapping labels and cramped bars don’t do your structured data justice. Let’s walk through some way clearer alternatives that’ll make your data hierarchy pop and help others spot insights easily:
Treemaps nest subcategories within parent boxes, where the size of each box maps to a numerical value (like count, sum, or total). It’s perfect for highlighting both the hierarchy and relative importance of each segment without cluttering axes.
Here’s a quick Python implementation using the squarify library:
import squarify import matplotlib.pyplot as plt # Replace with your Excel data (labels = hierarchy levels, sizes = corresponding values) labels = ["Category A", "A1", "A2", "Category B", "B1", "B2", "B3"] sizes = [100, 30, 70, 150, 50, 60, 40] colors = ["#f94144", "#f3722c", "#f8961e", "#f9c74f", "#90be6d", "#43aa8b", "#577590"] plt.figure(figsize=(10, 6)) squarify.plot(sizes=sizes, label=labels, color=colors, alpha=0.8) plt.title("Hierarchical Data Treemap") plt.axis('off') # Remove axes for cleaner look plt.show()
This makes it instantly obvious how subcategories roll up into parent groups, and the box size gives immediate context to their relative values.
Sunburst charts are like nested pie charts, with each concentric layer representing a level in your hierarchy. Each segment’s angle corresponds to its value, and interactive versions let users hover to get exact details—great for deep, complex hierarchies.
Try this with Plotly for interactivity:
import plotly.express as px import pandas as pd # Match this DataFrame structure to your Excel data df = pd.DataFrame({ "Level 1": ["A", "A", "B", "B", "B"], "Level 2": ["A1", "A2", "B1", "B2", "B3"], "Value": [30, 70, 50, 60, 40] }) fig = px.sunburst(df, path=['Level 1', 'Level 2'], values='Value', title='Hierarchical Sunburst Chart') fig.show()
Even static sunbursts make the nested structure clear at a glance, while interactive ones let users zoom into specific segments for deeper dives.
If you still prefer bar charts but want to avoid clutter, split your parent categories into separate subplots (facets). This keeps each subplot focused on a single parent’s subcategories, with no overlapping labels.
Here’s how to do it with Seaborn:
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd df = pd.DataFrame({ "Parent Category": ["A", "A", "B", "B", "B"], "Subcategory": ["A1", "A2", "B1", "B2", "B3"], "Value": [30, 70, 50, 60, 40] }) # Create facets for each parent category g = sns.FacetGrid(df, col="Parent Category", height=4, aspect=0.8) g.map(sns.barplot, "Subcategory", "Value", palette="viridis") g.set_axis_labels("Subcategory", "Value") g.set_titles("Category {col_name}") plt.show()
Each facet acts as a clean mini bar chart, making it easy to compare subcategories within a parent and still spot trends across parent groups.
This approach uses horizontal bars with indentation to visually represent hierarchy—parent categories are left-aligned, subcategories are indented under them. It’s ideal for showing exact values and a clear linear hierarchy without extra visual noise.
Simple implementation with Matplotlib:
import matplotlib.pyplot as plt # List tuples: (hierarchy label with indentation, corresponding value) hierarchical_data = [ ("Category A", 100), (" A1", 30), (" A2", 70), ("Category B", 150), (" B1", 50), (" B2", 60), (" B3", 40) ] labels = [item[0] for item in hierarchical_data] values = [item[1] for item in hierarchical_data] plt.figure(figsize=(8, 6)) plt.barh(labels, values, color="#43aa8b") plt.xlabel("Value") plt.title("Indented Hierarchical Bar Chart") plt.tight_layout() # Ensure labels fit plt.show()
The indentation makes the hierarchy immediately obvious, and horizontal bars give plenty of space for readable labels.
Quick Decision Guide
- Pick treemaps if you want to emphasize relative segment sizes across the hierarchy.
- Go for sunburst charts for interactive exploration of deep, complex hierarchies.
- Use faceted bar charts if you still need bar-based comparisons but want to reduce clutter.
- Choose indented bar charts if you need to show exact values and a clear linear hierarchy.
内容的提问来源于stack exchange,提问作者Monta

