Python 如何使用条形图缩放 Seaborn 的 y 轴?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27019153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to scale Seaborn's y-axis with a bar plot?
提问by user124114
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 plot's 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.
我尝试修改情节的轴,但这总是以一种或另一种方式弄乱条形图,因此请先尝试您的解决方案以确保它确实有效。
回答by Jules
You can use Matplotlib commands after calling factorplot.
For example:
您可以在调用后使用 Matplotlib 命令factorplot。例如:
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()


回答by Michael Hall
Considering your question mentions barplotI thought I would add in a solution for that type of plot also as it differs from the factorplotin @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")
回答by rvf
If you are facing the problem of vanishing bars upon setting log-scale using the previous solutions, try adding log=Trueto the seaborn function call instead. (I'm lacking reputation to comment on the other answers).
如果您在使用之前的解决方案设置对数刻度时遇到条形消失的问题,请尝试添加log=True到 seaborn 函数调用中。(我缺乏评论其他答案的声誉)。
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)

