How to add title to seaborn boxplot

前端 未结 5 1973
花落未央
花落未央 2020-12-12 16:16

Seems pretty Googleable but haven\'t been able to find something online that works.

I\'ve tried both sns.boxplot(\'Day\', \'Count\', data= gg).title(\'lalala\'

5条回答
  •  执念已碎
    2020-12-12 16:40

    Seaborn box plot returns a matplotlib axes instance. Unlike pyplot itself, which has a method plt.title(), the corresponding argument for an axes is ax.set_title(). Therefore you need to call

    sns.boxplot('Day', 'Count', data= gg).set_title('lalala')
    

    A complete example would be:

    import seaborn as sns
    import matplotlib.pyplot as plt
    
    tips = sns.load_dataset("tips")
    sns.boxplot(x=tips["total_bill"]).set_title("LaLaLa")
    
    plt.show()
    

    Of course you could also use the returned axes instance to make it more readable:

    ax = sns.boxplot('Day', 'Count', data= gg)
    ax.set_title('lalala')
    ax.set_ylabel('lololo')
    

提交回复
热议问题