

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
3D scatterplots in Python Matplotlib with hue colormap and legend
To plot 3D scatter plots in Python with hue colormap and legend, we can take the following steps−
- Set the figure size and adjust the padding between and around the subplots
- Create x, y and z data points using numpy.
- Create a new figure or activate an existing figure using figure() method.
- Get the current axes, creating one if necessary.
- Get the hue colormap, defining a palette.
- Plot x, y and z data points using scatter() method.
- Place a legend on the plot.
- To display the figure, use show() method.
Example
import numpy as np import seaborn as sns from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.rand(100) y = np.random.rand(100) z = np.random.rand(100) fig = plt.figure() ax = fig.gca(projection='3d') cmap = ListedColormap(sns.color_palette("husl", 256).as_hex()) sc = ax.scatter(x, y, z, s=40, c=x, marker='o', cmap=cmap, alpha=1) plt.legend(*sc.legend_elements(), bbox_to_anchor=(1.05, 1), loc=2) plt.show()
Output
- Related Questions & Answers
- Add a legend in a 3D scatterplot with scatter() in Matplotlib
- How to create a legend for a 3D bar in Matplotlib?
- Legend with vertical line in matplotlib
- Matplotlib Plot Lines with Colors through Colormap
- Matplotlib histogram with multiple legend entries
- Manually add legend Items Python Matplotlib
- Extract Matplotlib colormap in hex-format
- Show only certain items in legend Python Matplotlib
- Plot a histogram with colors taken from colormap in Matplotlib
- How to plot a 3D density map in Python with Matplotlib?
- Automated legend creation in Matplotlib
- How to display the matrix value and colormap in Matplotlib?
- Matplotlib savefig with a legend outside the plot
- How to make Matplotlib scatterplots transparent as a group?
- How to plot data into imshow() with custom colormap in Matplotlib?
Advertisements