作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我使用下面的代码绘制一个图。如何在 x 轴上边缘绘制地毯部分?
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.distplot(np.random.normal(0, 0.1, 100), rug=True, hist=False)
plt.show()
最佳答案
seaborn.rugplot
创建一个 LineCollection
,其中线条的长度在轴坐标中定义。这些始终是相同的,因此如果反转轴,绘图不会改变。
不过,您可以根据数据创建自己的 LineCollection
。与使用 bar 相比,优点是线宽以磅为单位,因此无论数据范围如何,都不会丢失任何线。
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
import seaborn as sns
def upper_rugplot(data, height=.05, ax=None, **kwargs):
from matplotlib.collections import LineCollection
ax = ax or plt.gca()
kwargs.setdefault("linewidth", 1)
segs = np.stack((np.c_[data, data],
np.c_[np.ones_like(data), np.ones_like(data)-height]),
axis=-1)
lc = LineCollection(segs, transform=ax.get_xaxis_transform(), **kwargs)
ax.add_collection(lc)
fig, ax = plt.subplots()
data = np.random.normal(0, 0.1, 100)
sns.distplot(data, rug=False, hist=False, ax=ax)
upper_rugplot(data, ax=ax)
plt.show()
关于python - Matplotlib/Seaborn : how to plot a rugplot on the top edge of x-axis?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55066869/
我是一名优秀的程序员,十分优秀!