一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久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()#計算平均值看是否和條形圖得高度一致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()#計算平均值看是否和條形圖得高度一致print(df.groupby("day").agg({"total_bill":[np.mean]}))

     total_bill           meandayThur  17.682742Fri   17.151579Sat   20.441379Sun   21.410000

dataframe aggregate函數(shù)使用

#計算平均值看是否和條形圖得高度一致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)計得是不同得數(shù)目print("="*20)print(df.groupby("day").agg({"total_bill":"count"})) # 這里統(tǒng)計得是每個分組樣本得數(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)計個數(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)計個數(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)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(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)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(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()#計算想加和看是否和條形圖得高度一致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)系我們進行處理。
發(fā)表評論
更多 網(wǎng)友評論1 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 一级黄色片网址 | 成人在线中文字幕 | 成人av一区| 久久久91| 国产精品久久久久久久久久久免费看 | 高清不卡毛片 | 一区二区三区欧美大片 | 久久久久久综合 | 欧美群妇大交群中文字幕 | 羞羞的视频在线 | 精品无码久久久久久国产 | 欧美日韩在线一区二区 | 国产精品视频不卡 | 日本精品视频在线观看 | 国产高清视频 | 欧美一级二级视频 | 中文字幕亚洲区一区二 | 国产精品久久久久久久久久久免费看 | 国产成人亚洲精品自产在线 | 久久午夜电影 | 伊人超碰 | 精品欧美乱码久久久久久1区2区 | 无码日韩精品一区二区免费 | 国产成人精品久久二区二区91 | 天天色影视综合 | 国产乱码精品一品二品 | 国产精品99一区二区 | 国产成人jvid在线播放 | 国产精品欧美日韩 | 亚洲精品黑人 | 欧美日韩中文在线 | 国产一级一级毛片 | 成人av一区二区三区 | 久久国产精品网站 | 日韩中文字幕免费在线观看 | 99热在线播放 | 黄色成人免费在线观看 | 久久久久久久久综合 | 欧美精品一区二区三区四区 在线 | 中文在线一区 | 欧美日韩不卡 |