How to scale Seaborn's y-axis with a bar plot?

后端 未结 3 1663
星月不相逢
星月不相逢 2020-12-09 15:55

I\'m using factorplot(kind=\"bar\").

How do I scale the y-axis, for example with log-scale?

I tried tinkering with the plot\'s axes, but that al

相关标签:
3条回答
  • 2020-12-09 16:12

    You can use Matplotlib commands after calling factorplot. 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()
    

    enter image description here

    0 讨论(0)
  • 2020-12-09 16:12

    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. (I'm lacking reputation to comment on the other answers).

    Using 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:

    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)
    
    0 讨论(0)
  • 2020-12-09 16:18

    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.

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

    0 讨论(0)
提交回复
热议问题