简体   繁体   English

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

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

I'm using factorplot(kind="bar") .我正在使用factorplot(kind="bar")

How do I scale the y-axis, for example with log-scale?如何缩放 y 轴,例如使用对数刻度?

I tried tinkering with the plots' axes, but that always messed up the bar plot in one way or another, so please try your solution first to make sure it really works.我尝试修改绘图轴,但这总是以某种方式弄乱 plot 条,因此请先尝试您的解决方案以确保它确实有效。

You can use Matplotlib commands after calling factorplot . 调用factorplot后可以使用Matplotlib命令。 For example: 例如:

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

在此输入图像描述

Considering your question mentions barplot I thought I would add in a solution for that type of plot also as it differs from the factorplot in @Jules solution. 考虑你的问题中提到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")

在此输入图像描述

If you are facing the problem of vanishing bars upon setting log-scale using the previous solutions, try adding log=True to the seaborn function call instead. 如果您在使用以前的解决方案设置日志规模时遇到消失的问题,请尝试将log=True添加到seaborn函数调用中。 (I'm lacking reputation to comment on the other answers). (我对其他答案的评论缺乏声誉)。

Using sns.factorplot : 使用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)

Using sns.barplot : 使用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's catplot does not have anymore the log parameter. Seaborn 的catplot不再有log参数。

For those looking for an updated answer , here's the quickest fix I've used: you have to use matplotlib's built-in support by accessing the axes object.对于那些寻找更新答案的人,这是我使用过的最快的修复方法:您必须通过访问轴 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