繁体   English   中英

如何使用条形 plot 缩放 Seaborn 的 y 轴

[英]How to scale Seaborn's y-axis with a bar plot

我正在使用factorplot(kind="bar")

如何缩放 y 轴,例如使用对数刻度?

我尝试修改绘图轴,但这总是以某种方式弄乱 plot 条,因此请先尝试您的解决方案以确保它确实有效。

调用factorplot后可以使用Matplotlib命令。 例如:

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

titanic = sns.load_dataset("titanic")

g = sns.factorplot("class", "survived", "sex",
                   data=titanic, kind="bar",
                   size=6, palette="muted", legend=False)
g.fig.get_axes()[0].set_yscale('log')
plt.show()

在此输入图像描述

考虑你的问题中提到barplot我想我会添加在该类型的情节也的解决方案,它从不同factorplot在@Jules解决方案。

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

titanic = sns.load_dataset("titanic")

g = sns.barplot(x="class", y="survived", hue="sex",
                data=titanic, palette="muted")
g.set_yscale("log")

在此输入图像描述

如果您在使用以前的解决方案设置日志规模时遇到消失的问题,请尝试将log=True添加到seaborn函数调用中。 (我对其他答案的评论缺乏声誉)。

使用sns.factorplot

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

titanic = sns.load_dataset("titanic")

g = sns.factorplot(x="class", y="survived", hue="sex", kind='bar',
                   data=titanic, palette="muted", log=True)
g.ax.set_ylim(0.05, 1)

使用sns.barplot

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

titanic = sns.load_dataset("titanic")

g = sns.barplot(x="class", y="survived", hue="sex",
                data=titanic, palette="muted", log=True)
g.set_ylim(0.05, 1)

Seaborn 的catplot不再有log参数。

对于那些寻找更新答案的人,这是我使用过的最快的修复方法:您必须通过访问轴 object 来使用 matplotlib 的内置支持。

g = sns.catplot(data=df, <YOUR PARAMETERS>)
for ax in g.fig.axes:
    ax.set_yscale('log')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM