最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python?seaborn?barplot畫圖案例

 更新時間:2022年07月22日 10:23:25   作者:qq_45759229  
這篇文章主要介紹了Python?seaborn?barplot畫圖案例,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下

默認barplot

import seaborn as sns
import matplotlib.pyplot as plt 
import numpy as np 

sns.set_theme(style="whitegrid")
df = sns.load_dataset("tips")
#默認畫條形圖
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顯示并不是標準差

     total_bill
           mean
day
Thur  17.682742
Fri   17.151579
Sat   20.441379
Sun   21.410000
     total_bill
            std
day
Thur   7.886170
Fri    8.302660
Sat    9.480419
Sun    8.832122

使用案例

# import libraries
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
# load dataset
tips = sns.load_dataset("tips")
# Set the figure size
plt.figure(figsize=(14, 8))
# plot a bar chart
ax = 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 sns
import matplotlib.pyplot as plt 
sns.set_theme(style="whitegrid")
df = sns.load_dataset("tips")
#默認畫條形圖
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.7
Line2D(_line0)
[[ 1.         13.93096053]
 [ 1.         21.38463158]]
2.7
Line2D(_line1)
[[ 2.         18.57236207]
 [ 2.         22.40351437]]
2.7
Line2D(_line2)
[[ 3.         19.66244737]
 [ 3.         23.50109868]]
2.7
Line2D(_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 mean
for 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 sns
import matplotlib.pyplot as plt 
sns.set_theme(style="whitegrid")
df = sns.load_dataset("tips")
#默認畫條形圖
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
           mean
day
Thur  17.682742
Fri   17.151579
Sat   20.441379
Sun   21.410000
     total_bill
            std
day
Thur   7.886170
Fri    8.302660
Sat    9.480419
Sun    8.832122

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

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

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

import seaborn as sns
import matplotlib.pyplot as plt 
sns.set_theme(style="whitegrid")
df = sns.load_dataset("tips")
#默認畫條形圖
sns.barplot(x="day",y="total_bill",data=df,ci=95)
plt.show()

#計算平均值看是否和條形圖的高度一致
print(df.groupby("day").agg({"total_bill":[np.mean]}))

     total_bill
           mean
day
Thur  17.682742
Fri   17.151579
Sat   20.441379
Sun   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]})) # 分組求標準差
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
           mean
day
Thur  17.682742
Fri   17.151579
Sat   20.441379
Sun   21.410000
====================
     total_bill
            std
day
Thur   7.886170
Fri    8.302660
Sat    9.480419
Sun    8.832122
====================
      total_bill
day
Thur          61
Fri           18
Sat           85
Sun           76
====================
      total_bill
day
Thur          62
Fri           19
Sat           87
Sun           76
====================
Sat     87
Sun     76
Thur    62
Fri     19
Name: day, dtype: int64
====================

dataframe aggregate 自定義函數(shù)

import numpy as np
import pandas as pd

df = 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.nan
result = df.groupby(['Trader'])['Buy/Sell'].agg([categorize, 'sum', 'count'])
result = result.rename(columns={'categorize' : 'Buy/Sell'})
result
   Buy/Sell Trader
0         1      A
1         0      A
2         1      B
3         1      B
4         0      B
5         1      C
6         0      C
7         0      C

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

df = sns.load_dataset("tips")
#默認畫條形圖

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]})) # 分組求標準差

sns.barplot(x="day",y="total_bill",data=df,ci=68)
plt.show()
print(df.groupby("day").agg({"total_bill":[np.std,custom2]})) #

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

     total_bill
            std    custom1
day
Thur   7.886170  19.645769
Fri    8.302660  20.884910
Sat    9.480419  22.433538
Sun    8.832122  23.395703

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

     total_bill
            std    custom2
day
Thur   7.886170  18.684287
Fri    8.302660  19.056340
Sat    9.480419  21.457787
Sun    8.832122  22.423114

seaborn顯示網格

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

seaborn設置刻度

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
            sum
day
Fri      325.88
Sat     1778.40
Sun     1627.16
Thur    1096.33
'''

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

相關文章

  • Python類中使用cursor.execute()時語法錯誤的解決方法

    Python類中使用cursor.execute()時語法錯誤的解決方法

    在 Python 類中使用 cursor.execute() 時,出現(xiàn)語法錯誤(如 SyntaxError 或 SQL 語法相關錯誤)通常是因為 SQL 語句格式不正確、占位符使用不當,或參數(shù)傳遞方式不符合預期,以下是解決此類問題的常見方法和建議,需要的朋友可以參考下
    2024-09-09
  • Python使用Pandas對csv文件進行數(shù)據(jù)處理的方法

    Python使用Pandas對csv文件進行數(shù)據(jù)處理的方法

    這篇文章主要介紹了Python使用Pandas對csv文件進行數(shù)據(jù)處理的方法,本文通過實例代碼相結合給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • python實現(xiàn)梯度下降算法的實例詳解

    python實現(xiàn)梯度下降算法的實例詳解

    在本篇文章里小編給大家整理的是一篇關于python實現(xiàn)梯度下降算法的實例詳解內容,需要的朋友們可以參考下。
    2020-08-08
  • windows下python模擬鼠標點擊和鍵盤輸示例

    windows下python模擬鼠標點擊和鍵盤輸示例

    這篇文章主要介紹了windows下python模擬鼠標點擊和鍵盤輸示例,需要的朋友可以參考下
    2014-02-02
  • Python異步與定時任務提高程序并發(fā)性和定時執(zhí)行效率

    Python異步與定時任務提高程序并發(fā)性和定時執(zhí)行效率

    Python異步與定時任務是Python編程中常用的兩種技術,異步任務可用于高效處理I/O密集型任務,提高程序并發(fā)性;定時任務可用于定時執(zhí)行計劃任務,提高程序的執(zhí)行效率。這兩種技術的應用有助于提升Python程序的性能和效率
    2023-05-05
  • python裝飾器與遞歸算法詳解

    python裝飾器與遞歸算法詳解

    本文給大家詳細講解了python中的裝飾器與遞歸算法,有需要的小伙伴可以來參考下,希望對大家學習Python能夠有所幫助
    2016-02-02
  • Python中__new__()方法適應及注意事項詳解

    Python中__new__()方法適應及注意事項詳解

    這篇文章主要介紹了Python中__new__()方法適應及注意事項的相關資料,new()方法是Python中的一個特殊構造方法,用于在創(chuàng)建對象之前調用,并負責返回類的新實例,它與init()方法不同,需要的朋友可以參考下
    2025-03-03
  • Flask接收上傳圖片方法實現(xiàn)

    Flask接收上傳圖片方法實現(xiàn)

    本文主要介紹了Flask接收上傳圖片方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Python人工智能實戰(zhàn)之以圖搜圖的實現(xiàn)

    Python人工智能實戰(zhàn)之以圖搜圖的實現(xiàn)

    這篇文章主要為大家詳細介紹了如何基于vgg網絡和Keras深度學習框架實現(xiàn)以圖搜圖功能。文中的示例代碼講解詳細,感興趣的小伙伴可以學習一下
    2022-05-05
  • python繪制直方圖和密度圖的實例

    python繪制直方圖和密度圖的實例

    今天小編就為大家分享一篇python繪制直方圖和密度圖的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07

最新評論

漳平市| 开化县| 孟连| 晴隆县| 翼城县| 文成县| 盐亭县| 普格县| 郓城县| 神池县| 永登县| 子洲县| 海丰县| 沙洋县| 朝阳市| 隆子县| 孝昌县| 淮南市| 石阡县| 淅川县| 西吉县| 巨野县| 济宁市| 双牌县| 江陵县| 灵武市| 商水县| 泽库县| 汕尾市| 宽甸| 荣昌县| 和林格尔县| 灵武市| 建瓯市| 阳城县| 黎川县| 米林县| 包头市| 昌邑市| 治多县| 保定市|