【问题标题】:Seaborn : cut displaySeaborn : 剪裁展示
【发布时间】:2017-12-18 15:12:29
【问题描述】:

我在做小提琴图,但我有极端的数据点,因此我得到了类似的东西:

sns.violinplot(x = "x_axis", y = "y_axis", hue="groups", data = my_data, split = True)

如果我将 Y 轴上的数据截断以排除 > 200,如下所示:

cut_data = my_data[my_data.y_axis < 200]
sns.violinplot(x = "x_axis", y = "y_axis", hue="groups", data = cut_data, split = True)

我得到这样的东西:

当然,这是不希望的,因为就基础数据而言,第二个图与第一个图不同。 所以我的问题是:有没有办法绘制所有数据,但图表在 Y 轴上仅显示 0 到 200?

【问题讨论】:

  • 这能满足您的需求吗? ax.set_ylim(0, 200)

标签: python python-3.x pandas matplotlib seaborn


【解决方案1】:

您可以通过在 matplotlib 轴对象上设置 y 轴限制来截断绘图本身:

#create a sample data set
df = pd.DataFrame(np.random.gamma(1, 100, 10000), columns=['a'])
df['Group'] = np.random.choice(['group 1', 'group 2'], 10000)

#plot and the truncate the axis
fig, ax = plt.subplots(1)
sns.violinplot(x='Group', y='a', data=df, ax=ax)
ax.set_ylim(0, 200)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多