Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib/Seaborn:如何在x轴的上边缘绘制rugplot?_Python_Matplotlib_Seaborn - Fatal编程技术网

Python Matplotlib/Seaborn:如何在x轴的上边缘绘制rugplot?

Python Matplotlib/Seaborn:如何在x轴的上边缘绘制rugplot?,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,假设我使用下面的代码绘制一个图。如何在x轴的上边缘绘制地毯部分 将numpy导入为np 将matplotlib.pyplot作为plt导入 导入seaborn作为sns sns.distplot(np.random.normal(0,0.1100),rug=True,hist=False) plt.show() 只是数据点上的细线。你可以把它们想象成细条。也就是说,您可以有以下解决方法:在不使用地毯的情况下绘制distplot,然后创建一个双x轴,并使用细条绘制条形图。以下是一个可行的答案:

假设我使用下面的代码绘制一个图。如何在x轴的上边缘绘制地毯部分

将numpy导入为np
将matplotlib.pyplot作为plt导入
导入seaborn作为sns
sns.distplot(np.random.normal(0,0.1100),rug=True,hist=False)
plt.show()
只是数据点上的细线。你可以把它们想象成细条。也就是说,您可以有以下解决方法:在不使用地毯的情况下绘制
distplot
,然后创建一个双x轴,并使用细条绘制条形图。以下是一个可行的答案:

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

fig, ax = plt.subplots()

data = np.random.normal(0, 0.1, 100)
sns.distplot(data, rug=False, hist=False, ax=ax)

ax1 = ax.twinx()
ax1.bar(data, height=ax.get_ylim()[1]/10, width=0.001)
ax1.set_ylim(ax.get_ylim())
ax1.invert_yaxis()
ax1.set_yticks([])
plt.show()

seaborn.rugplot创建一个
线集合
,线的长度在轴坐标中定义。这些轴始终相同,因此,如果反转轴,则绘图不会更改

不过,您可以根据数据创建自己的
LineCollection
。与使用
bar
s相比,它的优点是线宽以点为单位,因此不会丢失任何与数据范围无关的行

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

我不知道为什么有人否决了你的答案,因为这似乎是正确的做法。这就是说,它不会产生一个相同的地毯(我将种子设置为21,就像你为了比较结果所做的那样)。它们非常接近,但并不完全相同。有什么想法吗?(我的seaborn答案经常被否决;我想我知道是谁做的,但不知道为什么。)总之,我想你可以直接在倒置的双轴上绘制一个
rugplot
,对吧?@Victorvalentie:我的数据看起来像
[-0.00519642-0.01111961 0.10417968-0.12567393 0.07453877-0.17110538-0.02058644-0.02345713 0.1128144-0.0012626-0.06132003 0.13736885…
。您也有相同的数字数组吗?@ImportanceOfBeingErnest:xli特别尝试了
ax1=ax.twiny()
sns.rugplot(数据,ax=ax1)
axim.set\u())
但rugplot仍在底部x上-axis@Bazingaa是的,这里是相同的数组。您可以检查
数据[82]
是否由sns默认方法绘制,但不是由您绘制的。我不知道为什么会发生这种情况。