- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a legend for a 3D bar in Matplotlib?
To create a legend for a 3D bar in matplotlib, we can plot 3D bars and place a legend using legend() method.
Steps
- Set the figure size and adjust the padding between and around the subplots.
- Create a new figure or activate an esxisting figure using figure() method.
- Add an axes to the figure as part of a subplot arrangement.
- Create a list of data x3, y3, z3, dx, dy and dz using numpy.
- Plot a 3D bar using bar3d() method.
- Create a rectangle axis for legend placement.
- Use legend() method to place the legend for bars.
- To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax1 = fig.add_subplot(111, projection='3d') 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) dx = np.ones(10) dy = np.ones(10) dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ax1.bar3d(x3, y3, z3, dx, dy, dz, color="green") b1 = plt.Rectangle((0, 0), 1, 1, fc="green") ax1.bar3d(y3, x3, z3, dx, dy, dz, color="red") b2 = plt.Rectangle((0, 0), 1, 1, fc="red") ax1.legend([b1, b2], ['type1', 'type2']) plt.show()
Output
- Related Articles
- How to create a draggable legend in Matplotlib?
- How do you create a legend for a contour plot in Matplotlib?
- Add a legend in a 3D scatterplot with scatter() in Matplotlib
- How to add a legend on Seaborn facetgrid bar plot using Matplotlib?
- How to Create a Diverging Stacked Bar Chart in Matplotlib?
- How to create a stacked bar chart for my DataFrame using Seaborn in Matplotlib?
- 3D scatterplots in Python Matplotlib with hue colormap and legend
- How to create a Matplotlib bar chart with a threshold line?
- How to plot a bar chart for a list in Python matplotlib?
- How to add a legend to a Matplotlib pie chart?
- How to create broken horizontal bar graphs in matplotlib?
- How to plot an emoji as a label for a bar in Matplotlib?
- How to plot a 3D continuous line in Matplotlib?
- How to plot a 3D patch collection in matplotlib?
- How to position and align a Matplotlib figure legend?

Advertisements