Advanced Chart Types
12.1 Explanation with Definition
Advanced visualizations include box plots, pie charts, heatmaps, and subplots. These charts
reveal complex patterns, distributions, and relationships in data. Box plots show quartiles and
outliers, pie charts display proportions, and heatmaps visualize matrix data with colors.
12.2 Package and Method Explanations
[Link](): Creates box-and-whisker plot showing quartiles and outliers
[Link](): Creates pie chart with percentages, useful for proportions
[Link](): Displays matrix data as image (heatmap)
[Link](): Adds color scale reference to heatmap
[Link](nrows, ncols): Creates grid of subplots
[Link](): Plots data with error bars for uncertainty visualization
autopct: Parameter for pie charts to show percentages
explode: Tuple to separate pie slices from center
patch_artist=True: Allows filling box plot boxes with colors
12.3 Syntax
import [Link] as plt
import numpy as np
# Box plot
[Link](data_list)
[Link]([data1, data2, data3], labels=['Group1', 'Group2', 'Group3'])
# Pie chart
[Link](sizes, labels=labels, autopct='%1.1f%%', startangle=90)
# Heatmap
[Link](matrix, cmap='viridis', aspect='auto')
[Link]()
# Subplots with different chart types
fig, axes = [Link](2, 2, figsize=(12, 10))
axes[0, 0].plot(x, y)
axes[0, 1].scatter(x, y)
axes[1, 0].bar(categories, values)
axes[1, 1].hist(data, bins=20)
# Violin plot (requires seaborn for full functionality)
# But can approximate with matplotlib
# Error bars
[Link](x, y, yerr=errors, fmt='o-', capsize=5)
# Filled area
plt.fill_between(x, y1, y2, alpha=0.3)
# Stacked bar chart
[Link](x, data1, label='Series 1')
[Link](x, data2, bottom=data1, label='Series 2')
12.4 Example - Agriculture Application
import [Link] as plt
import numpy as np
# Agricultural data for advanced visualizations
crops = ['Wheat', 'Rice', 'Corn', 'Soybean']
production_2021 = [180, 228, 416, 145]
production_2022 = [195, 245, 430, 160]
production_2023 = [210, 260, 445, 175]
regions = ['North', 'South', 'East', 'West', 'Central']
market_share = [18, 24, 32, 15, 11]
# Yield data for multiple farms (for box plot)
north_yields = [165, 170, 180, 175, 182, 168, 195, 260]
south_yields = [210, 220, 228, 215, 235, 240, 225, 218]
east_yields = [400, 416, 410, 430, 420, 408, 425, 415]
west_yields = [155, 164, 160, 170, 158, 162, 168, 165]
# Example 1: Box Plot - Yield Distribution by Region
[Link](figsize=(10, 6))
box_data = [north_yields, south_yields, east_yields, west_yields]
bp = [Link](box_data, labels=['North', 'South', 'East', 'West'],
patch_artist=True, showmeans=True)
# Customize colors
colors = ['lightblue', 'lightgreen', 'lightyellow', 'lightcoral']
for patch, color in zip(bp['boxes'], colors):
patch.set_facecolor(color)
[Link]('Yield (tons)', fontsize=12)
[Link]('Yield Distribution by Region', fontsize=14, fontweight='bold')
[Link](axis='y', alpha=0.3)
print('Figure 1: Box plot - showing median, quartiles, and outliers')
# Example 2: Pie Chart - Market Share by Region
[Link](figsize=(10, 8))
explode = (0, 0, 0.1, 0, 0) # Emphasize East region
colors_pie = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#ff99cc']
[Link](market_share, labels=regions, autopct='%1.1f%%', startangle=90,
explode=explode, colors=colors_pie, shadow=True)
[Link]('Agricultural Market Share by Region', fontsize=14,
fontweight='bold')
print('Figure 2: Pie chart - market share percentages')
# Example 3: Grouped Bar Chart - Production Comparison
x = [Link](len(crops))
width = 0.25
fig, ax = [Link](figsize=(12, 6))
bars1 = [Link](x - width, production_2021, width, label='2021',
color='skyblue')
bars2 = [Link](x, production_2022, width, label='2022', color='lightgreen')
bars3 = [Link](x + width, production_2023, width, label='2023',
color='salmon')
ax.set_xlabel('Crop Type', fontsize=12)
ax.set_ylabel('Production (tons)', fontsize=12)
ax.set_title('Crop Production Trends (2021-2023)', fontsize=14,
fontweight='bold')
ax.set_xticks(x)
ax.set_xticklabels(crops)
[Link]()
[Link](axis='y', alpha=0.3)
print('Figure 3: Grouped bar chart - three-year comparison')
# Example 4: Heatmap - Correlation Matrix
# Create correlation data
yield_data = [Link]([north_yields, south_yields, east_yields,
west_yields])
correlation_matrix = [Link](yield_data)
fig, ax = [Link](figsize=(8, 6))
im = [Link](correlation_matrix, cmap='coolwarm', aspect='auto', vmin=-1,
vmax=1)
# Set ticks and labels
ax.set_xticks([Link](4))
ax.set_yticks([Link](4))
ax.set_xticklabels(['North', 'South', 'East', 'West'])
ax.set_yticklabels(['North', 'South', 'East', 'West'])
# Add colorbar
cbar = [Link](im, ax=ax)
cbar.set_label('Correlation Coefficient', fontsize=10)
# Add correlation values as text
for i in range(4):
for j in range(4):
text = [Link](j, i, f'{correlation_matrix[i, j]:.2f}',
ha='center', va='center', color='black', fontsize=10)
ax.set_title('Yield Correlation Between Regions', fontsize=14,
fontweight='bold')
print('Figure 4: Heatmap - correlation matrix with values')
# Example 5: Subplots - Comprehensive Dashboard
fig, axes = [Link](2, 2, figsize=(14, 10))
# Subplot 1: Line plot
axes[0, 0].plot([2021, 2022, 2023],
[sum(production_2021), sum(production_2022),
sum(production_2023)],
marker='o', linewidth=2, color='green')
axes[0, 0].set_title('Total Production Trend')
axes[0, 0].set_ylabel('Total Production (tons)')
axes[0, 0].grid(True, alpha=0.3)
# Subplot 2: Bar chart
axes[0, 1].bar(crops, production_2023, color=['wheat', 'gold', 'orange',
'brown'])
axes[0, 1].set_title('2023 Production by Crop')
axes[0, 1].set_ylabel('Production (tons)')
# Subplot 3: Scatter plot
all_yields = north_yields + south_yields + east_yields + west_yields
yield_indices = list(range(len(all_yields)))
axes[1, 0].scatter(yield_indices, all_yields, alpha=0.6)
axes[1, 0].set_title('All Farm Yields')
axes[1, 0].set_xlabel('Farm Index')
axes[1, 0].set_ylabel('Yield (tons)')
# Subplot 4: Box plot
axes[1, 1].boxplot(box_data, labels=['N', 'S', 'E', 'W'])
axes[1, 1].set_title('Regional Yield Distribution')
axes[1, 1].set_ylabel('Yield (tons)')
plt.tight_layout()
print('Figure 5: Dashboard with 4 subplots showing multiple perspectives')
print('\nAll advanced visualizations created successfully!')
12.5 Output
Figure 1: Box plot - showing median, quartiles, and outliers
Figure 2: Pie chart - market share percentages
Figure 3: Grouped bar chart - three-year comparison
Figure 4: Heatmap - correlation matrix with values
Figure 5: Dashboard with 4 subplots showing multiple perspectives
All advanced visualizations created successfully!
[Visual descriptions:
Box Plot: Shows East region with highest median (~415), widest variation
North has lowest median (~172), tight distribution
Pie Chart: East dominates with 32%, followed by South (24%)
West and Central smaller shares (15%, 11%)
Grouped Bar: All crops show increasing trend 2021→2023
Corn consistently highest production (400+ tons)
Heatmap: Strong positive correlations (0.8-1.0) shown in red
Diagonal is perfect correlation (1.00)
Dashboard: Shows upward total production trend
2023 corn production highest at 445 tons
Yields range from ~155 to ~445 tons]