gpt4 book ai didi

python - Matplotlib/Seaborn : how to plot a rugplot on the top edge of x-axis?

转载 作者:行者123 更新时间:2023-12-01 08:14:10 24 4
gpt4 key购买 nike

假设我使用下面的代码绘制一个图。如何在 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()

enter image description here

最佳答案

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()

enter image description here

关于python - Matplotlib/Seaborn : how to plot a rugplot on the top edge of x-axis?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55066869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com