Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make rug plot in matplotlib

Im making a density plot with matplotlib and I would also like to get rug plot under it. good example to make density plot is here How to create a density plot in matplotlib?

but I couldn't find any good example for rug plot. in R it can be done easly by rug(data).

like image 307
TheLaama Avatar asked Jul 26 '13 07:07

TheLaama


People also ask

What does a rug plot show?

A rug plot is a plot of data for a single quantitative variable, displayed as marks along an axis. It is used to visualise the distribution of the data. As such it is analogous to a histogram with zero-width bins, or a one-dimensional scatter plot.

What is plot CLF?

plt. clf() clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots. plt. close() closes a window, which will be the current window, if not specified otherwise.

What is Mat plot?

Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB. Developers can also use matplotlib's APIs (Application Programming Interfaces) to embed plots in GUI applications.

What is the use of PLT plot ()?

The plot() function is used to draw points (markers) in a diagram. By default, the plot() function draws a line from point to point. The function takes parameters for specifying points in the diagram. Parameter 1 is an array containing the points on the x-axis.


1 Answers

You can plot markers at each datapoint.

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt

sample = np.hstack((np.random.randn(30), np.random.randn(20)+5))
density = stats.kde.gaussian_kde(sample)

fig, ax = plt.subplots(figsize=(8,4))

x = np.arange(-6,12,0.1)
ax.plot(x, density(x))

ax.plot(sample, [0.01]*len(sample), '|', color='k')

enter image description here

like image 120
Rutger Kassies Avatar answered Sep 21 '22 01:09

Rutger Kassies