Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create a legend for a 3D bar in Matplotlib?
Creating a legend for a 3D bar chart in Matplotlib requires plotting the bars and using Rectangle patches as legend handles since 3D bars don't automatically generate legend entries.
Steps to Create 3D Bar Legend
- Set up the figure and 3D subplot
- Create data arrays for bar positions and dimensions
- Plot 3D bars using
bar3d()method - Create
Rectanglepatches matching bar colors - Use
legend()method with custom handles and labels - Display the figure
Example
import numpy as np
import matplotlib.pyplot as plt
# Set up the figure
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
# Create data for two series of 3D bars
x_pos = np.arange(5)
y_pos1 = np.ones(5) * 1 # First series at y=1
y_pos2 = np.ones(5) * 2 # Second series at y=2
z_pos = np.zeros(5)
# Bar dimensions
dx = np.ones(5) * 0.8
dy = np.ones(5) * 0.8
dz1 = [2, 4, 6, 8, 5] # Heights for first series
dz2 = [3, 6, 4, 7, 9] # Heights for second series
# Plot the 3D bars
ax.bar3d(x_pos, y_pos1, z_pos, dx, dy, dz1, color='skyblue', alpha=0.8)
ax.bar3d(x_pos, y_pos2, z_pos, dx, dy, dz2, color='orange', alpha=0.8)
# Create rectangle patches for legend
legend_patch1 = plt.Rectangle((0, 0), 1, 1, fc='skyblue')
legend_patch2 = plt.Rectangle((0, 0), 1, 1, fc='orange')
# Add legend
ax.legend([legend_patch1, legend_patch2], ['Series 1', 'Series 2'])
# Labels and title
ax.set_xlabel('X Position')
ax.set_ylabel('Y Position')
ax.set_zlabel('Height')
ax.set_title('3D Bar Chart with Legend')
plt.show()
Key Points
- Rectangle patches: Used as legend handles since 3D bars don't support direct legend creation
- Color matching: Rectangle colors must match the bar colors exactly
- Alpha transparency: Can be applied to bars for better visualization
- Custom labels: Provide meaningful names for each data series
Advanced Example with Multiple Colors
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
# Data for multiple categories
categories = ['A', 'B', 'C', 'D']
colors = ['red', 'green', 'blue', 'purple']
legend_handles = []
for i, (category, color) in enumerate(zip(categories, colors)):
x_pos = np.arange(3)
y_pos = np.ones(3) * i
z_pos = np.zeros(3)
dx = dy = np.ones(3) * 0.6
dz = np.random.randint(1, 10, 3)
ax.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color=color, alpha=0.7)
# Create legend handle
legend_handles.append(plt.Rectangle((0, 0), 1, 1, fc=color))
# Add comprehensive legend
ax.legend(legend_handles, categories, loc='upper right')
ax.set_xlabel('X Axis')
ax.set_ylabel('Categories')
ax.set_zlabel('Values')
ax.set_title('Multi-Category 3D Bar Chart')
plt.tight_layout()
plt.show()
Conclusion
Creating legends for 3D bar charts requires using Rectangle patches as custom legend handles. Match the patch colors to your bar colors and provide descriptive labels for clear data visualization.
Advertisements
