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
Plot 3D bars without axes in Matplotlib
To plot 3D bars without axes in Matplotlib, we can use the bar3d() method with 3D subplot projection and hide the axes using axis('off').
Steps to Create 3D Bars Without Axes
- Set the figure size and adjust the padding between and around the subplots
- Create a new figure using
figure()method - Add a 3D subplot using
add_subplot()withprojection='3d' - Create coordinate data points (x, y, z) and dimension data (dx, dy, dz) using NumPy
- Use
bar3d()method to plot 3D bars - Hide the axes using
axis('off') - Display the figure using
show()method
Example
Here's how to create 3D bars and remove the axes ?
import numpy as np
from matplotlib import pyplot as plt
# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create figure and 3D subplot
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
# Define bar positions and dimensions
x3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y3 = [5, 6, 7, 8, 2, 5, 6, 3, 7, 2]
z3 = np.zeros(10) # Base positions (all at z=0)
# Bar dimensions
dx = np.ones(10) # Width in x direction
dy = np.ones(10) # Depth in y direction
dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Height of each bar
# Create 3D bars
ax1.bar3d(y3, x3, z3, dx, dy, dz, color="red")
# Hide all axes
ax1.axis('off')
# Display the plot
plt.show()
Understanding the Parameters
The bar3d() method requires six main parameters ?
- x, y, z ? Position coordinates for the bottom-left corner of each bar
- dx, dy, dz ? Dimensions of each bar in x, y, and z directions respectively
- color ? Color of the bars
Customizing Bar Colors
You can customize individual bar colors by passing a list of colors ?
import numpy as np
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
# Bar data
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 2]
z = np.zeros(5)
dx = dy = np.ones(5)
dz = [2, 5, 3, 8, 4]
# Different colors for each bar
colors = ['red', 'blue', 'green', 'orange', 'purple']
ax.bar3d(x, y, z, dx, dy, dz, color=colors)
ax.axis('off')
plt.show()
Output
Conclusion
Use bar3d() with projection='3d' to create 3D bar charts. Hide axes with axis('off') for a cleaner visualization. Customize colors and dimensions to enhance your data presentation.
Advertisements
