- 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 make a rug plot in Matplotlib?
Rug plots are used to visualize the distribution of data. It is a plot of data for a single variable, displayed as marks along an axis. To make a rug plot in Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create x data points using numpy.
- Add representation of a kernel-density estimate using Gaussian kernels, kde1 and kde2.
- Create a new figure or activate an existing figure using figure() method.
- Add an 'ax1' to the figure as part of a subplot arrangement.
- Make a rug plot with marker_size=20.
- Plot x_eval, kde1(x_eval) and kde2(x_eval) data points using plot() method.
- Place a legend on the plot.
- To display the figure, use show() method.
Example
import numpy as np from scipy import stats import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([-6, -4, 2, 1, 4], dtype=np.float) kde1 = stats.gaussian_kde(x) kde2 = stats.gaussian_kde(x, bw_method='silverman') fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, np.zeros(x.shape), 'b+', ms=20) x_eval = np.linspace(-5, 5, num=10) ax.plot(x_eval, kde1(x_eval), 'k-', label="Scott's Rule") ax.plot(x_eval, kde2(x_eval), 'r-', label="Silverman's Rule") plt.legend(loc='upper right') plt.show()
Output
It will produce the following output
- Related Articles
- How to make a mosaic plot in Matplotlib?
- How to make a simple lollipop plot in Matplotlib?
- How to make a broken horizontal bar plot in Matplotlib?
- How to make a quiver plot in polar coordinates using Matplotlib?
- How to make a discrete colorbar for a scatter plot in matplotlib?
- How to make a 4D plot with Matplotlib using arbitrary data?
- Make a multiline plot from .CSV file in matplotlib
- How to a plot stem plot in Matplotlib Python?
- How can I make a scatter plot colored by density in Matplotlib?
- How to plot a circle in Matplotlib?
- How to remove lines in a Matplotlib plot?
- How to animate a scatter plot in Matplotlib?
- How to plot a watermark image in Matplotlib?
- How to plot a 2D histogram in Matplotlib?
- How to animate a line plot in Matplotlib?

Advertisements