一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

Python?seaborn?barplot畫圖案例

目錄

默認(rèn)barplot

import seaborn as snsimport matplotlib.pyplot as plt import numpy as np sns.set_theme(style="whitegrid")df = sns.load_dataset("tips")#默認(rèn)畫條形圖sns.barplot(x="day",y="total_bill",data=df)plt.show()#計(jì)算平均值看是否和條形圖得高度一致print(df.groupby("day").agg({"total_bill":[np.mean]}))print(df.groupby("day").agg({"total_bill":[np.std]}))# 注意這個地方error bar顯示并不是標(biāo)準(zhǔn)差

     total_bill           meandayThur  17.682742Fri   17.151579Sat   20.441379Sun   21.410000     total_bill            stddayThur   7.886170Fri    8.302660Sat    9.480419Sun    8.832122

使用案例

# import librariesimport seaborn as snsimport numpy as npimport matplotlib.pyplot as plt# load datasettips = sns.load_dataset("tips")# Set the figure sizeplt.figure(figsize=(14, 8))# plot a bar chartax = sns.barplot(x="day", y="total_bill", data=tips, estimator=np.mean, ci=85, capsize=.2, color='lightblue')

修改capsize

ax=sns.barplot(x="day",y="total_bill",data=df,capsize=1.0)plt.show()

顯示error bar得值

import seaborn as snsimport matplotlib.pyplot as plt sns.set_theme(style="whitegrid")df = sns.load_dataset("tips")#默認(rèn)畫條形圖ax=sns.barplot(x="day",y="total_bill",data=df)plt.show()for p in ax.lines:    width = p.get_linewidth()    xy = p.get_xydata() # 顯示error bar得值    print(xy)    print(width)    print(p)

[[ 0.         15.85041935] [ 0.         19.64465726]]2.7Line2D(_line0)[[ 1.         13.93096053] [ 1.         21.38463158]]2.7Line2D(_line1)[[ 2.         18.57236207] [ 2.         22.40351437]]2.7Line2D(_line2)[[ 3.         19.66244737] [ 3.         23.50109868]]2.7Line2D(_line3)

annotata error bar

fig, ax = plt.subplots(figsize=(8, 6))sns.barplot(x='day', y='total_bill', data=df, capsize=0.2, ax=ax)# show the meanfor p in ax.patches:    h, w, x = p.get_height(), p.get_width(), p.get_x()    xy = (x + w / 2., h / 2)    text = f'Mean:n{h:0.2f}'    ax.annotate(text=text, xy=xy, ha='center', va='center')ax.set(xlabel='day', ylabel='total_bill')plt.show()

error bar選取sd

import seaborn as snsimport matplotlib.pyplot as plt sns.set_theme(style="whitegrid")df = sns.load_dataset("tips")#默認(rèn)畫條形圖sns.barplot(x="day",y="total_bill",data=df,ci="sd",capsize=1.0)## 注意這個ci參數(shù)plt.show()print(df.groupby("day").agg({"total_bill":[np.mean]}))print(df.groupby("day").agg({"total_bill":[np.std]}))

     total_bill           meandayThur  17.682742Fri   17.151579Sat   20.441379Sun   21.410000     total_bill            stddayThur   7.886170Fri    8.302660Sat    9.480419Sun    8.832122

設(shè)置置信區(qū)間(68)

import seaborn as snsimport matplotlib.pyplot as plt sns.set_theme(style="whitegrid")df = sns.load_dataset("tips")#默認(rèn)畫條形圖sns.barplot(x="day",y="total_bill",data=df,ci=68,capsize=1.0)## 注意這個ci參數(shù)plt.show()

設(shè)置置信區(qū)間(95)

import seaborn as snsimport matplotlib.pyplot as plt sns.set_theme(style="whitegrid")df = sns.load_dataset("tips")#默認(rèn)畫條形圖sns.barplot(x="day",y="total_bill",data=df,ci=95)plt.show()#計(jì)算平均值看是否和條形圖得高度一致print(df.groupby("day").agg({"total_bill":[np.mean]}))

     total_bill           meandayThur  17.682742Fri   17.151579Sat   20.441379Sun   21.410000

dataframe aggregate函數(shù)使用

#計(jì)算平均值看是否和條形圖得高度一致df = sns.load_dataset("tips")print("="*20)print(df.groupby("day").agg({"total_bill":[np.mean]})) # 分組求均值print("="*20)print(df.groupby("day").agg({"total_bill":[np.std]})) # 分組求標(biāo)準(zhǔn)差print("="*20)print(df.groupby("day").agg({"total_bill":"nunique"})) # 這里統(tǒng)計(jì)得是不同得數(shù)目print("="*20)print(df.groupby("day").agg({"total_bill":"count"})) # 這里統(tǒng)計(jì)得是每個分組樣本得數(shù)量print("="*20)print(df["day"].value_counts())print("="*20)
====================     total_bill           meandayThur  17.682742Fri   17.151579Sat   20.441379Sun   21.410000====================     total_bill            stddayThur   7.886170Fri    8.302660Sat    9.480419Sun    8.832122====================      total_billdayThur          61Fri           18Sat           85Sun           76====================      total_billdayThur          62Fri           19Sat           87Sun           76====================Sat     87Sun     76Thur    62Fri     19Name: day, dtype: int64====================

dataframe aggregate 自定義函數(shù)

import numpy as npimport pandas as pddf = pd.DataFrame({'Buy/Sell': [1, 0, 1, 1, 0, 1, 0, 0],                   'Trader': ['A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']})print(df)def categorize(x):    m = x.mean()    return 1 if m > 0.5 else 0 if m < 0.5 else np.nanresult = df.groupby(['Trader'])['Buy/Sell'].agg([categorize, 'sum', 'count'])result = result.rename(columns={'categorize' : 'Buy/Sell'})result
   Buy/Sell Trader0         1      A1         0      A2         1      B3         1      B4         0      B5         1      C6         0      C7         0      C

dataframe aggregate 自定義函數(shù)2

df = sns.load_dataset("tips")#默認(rèn)畫條形圖def custom1(x):    m = x.mean()    s = x.std()    n = x.count()# 統(tǒng)計(jì)個數(shù)    #print(n)    return m+1.96*s/np.sqrt(n)def custom2(x):    m = x.mean()    s = x.std()    n = x.count()# 統(tǒng)計(jì)個數(shù)    #print(n)    return m+s/np.sqrt(n)sns.barplot(x="day",y="total_bill",data=df,ci=95)plt.show()print(df.groupby("day").agg({"total_bill":[np.std,custom1]})) # 分組求標(biāo)準(zhǔn)差sns.barplot(x="day",y="total_bill",data=df,ci=68)plt.show()print(df.groupby("day").agg({"total_bill":[np.std,custom2]})) #

?[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-pkCx72ui-1658379974318)(output_24_0.png)]

     total_bill            std    custom1dayThur   7.886170  19.645769Fri    8.302660  20.884910Sat    9.480419  22.433538Sun    8.832122  23.395703

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-GFyIePmW-1658379974318)(output_24_2.png)]

     total_bill            std    custom2dayThur   7.886170  18.684287Fri    8.302660  19.056340Sat    9.480419  21.457787Sun    8.832122  22.423114

seaborn顯示網(wǎng)格

ax=sns.barplot(x="day",y="total_bill",data=df,ci=95)ax.yaxis.grid(True) # Hide the horizontal gridlinesax.xaxis.grid(True) # Show the vertical gridlines

seaborn設(shè)置刻度

fig, ax = plt.subplots(figsize=(10, 8))sns.barplot(x="day",y="total_bill",data=df,ci=95,ax=ax)ax.set_yticks([i for i in range(30)])ax.yaxis.grid(True) # Hide the horizontal gridlines

使用其他estaimator

#estimator 指定條形圖高度使用相加得和sns.barplot(x="day",y="total_bill",data=df,estimator=np.sum)plt.show()#計(jì)算想加和看是否和條形圖得高度一致print(df.groupby("day").agg({"total_bill":[np.sum]}))'''     total_bill            sumdayFri      325.88Sat     1778.40Sun     1627.16Thur    1096.33'''

到此這篇關(guān)于Python seaborn barplot畫圖案例得內(nèi)容就介紹到這了,更多相關(guān)Python seaborn barplot 內(nèi)容請搜索之家以前得內(nèi)容或繼續(xù)瀏覽下面得相關(guān)內(nèi)容希望大家以后多多支持之家!

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評論
更多 網(wǎng)友評論1 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 日本天堂一区二区 | 国产精品日日摸夜夜添夜夜av | 国产高清一区二区 | 国产精品久久久久久妇女6080 | 日本爱爱视频 | 一区二区三区在线免费观看 | 一区二区精品在线 | 久久9热 | 美女在线观看国产 | av黄色在线| 欧美性生交大片免费 | 综合中文字幕 | 99re视频在线观看 | 国产欧美精品一区二区色综合朱莉 | 精品国产欧美一区二区 | 最新中文字幕第一页视频 | 久久久久久999 | 精品久久精品 | 国产精品久久久乱弄 | 黄色大片视频 | 在线观看亚洲精品视频 | 精品中文字幕视频 | 国产精品毛片av一区 | avtt国产| 成人福利在线观看 | 91xx在线观看 | 亚洲一区| 91在线精品秘密一区二区 | 精品一区二区三区中文字幕 | jizz视频| 97caoporn国产免费人人 | 国产一区二区三区www | 亚洲国产欧美日韩 | 在线中文字幕亚洲 | 天天综合网天天综合色 | 日韩免费一区 | 国产999精品久久久久久 | 激情五月婷婷综合 | 免费黄色日本 | 久久国产精品网站 | 欧美黄色大片在线观看 |