4

Making a distplot with a rug using seaborn is easy:

import seaborn as sns, numpy as np
%matplotlib inline
sns.set(rc={"figure.figsize": (8, 4)}); np.random.seed(0)
x = np.random.randn(100)
ax = sns.distplot(x, rug=True)

The rug plot above reflects the distribution x. But what if I want to display the rug from a different distribution, rug_array, under the dist plot of x?

rug_array = np.array([-2.0, -1, 0, 1, 2, 2.1, 3])

The answer should display a plot of the curve x with rug plot ticks at -2,-1, ,0, 1, 2, 2.1, and 3.

1 Answer 1

5

Seaborn provides a function rugplot to draw the rugplot. The idea is to use this function.

import seaborn as sns
import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt

x = np.random.randn(100)
rug_array = np.array([-2.0, -1, 0, 1, 2, 2.1, 3])

ax = sns.distplot(x, rug=False)
sns.rugplot(rug_array, height=0.05, axis='x', ax=ax)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Not to make this more complicated, but if rug_array was in a different scale than the current x-axis (eg log10), how could I add a separate axis to match the rug? I can post this as a different question if need be.
You can add a new axis with ax2 = ax.twiny(), and plot the rugplot to it via sns.rugplot(..., ax=ax2). However, I doubt that the plot will be easily understandable. If this does not help you, posting a new question with an exact problem description might make sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.