日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

seaborn 画堆叠柱状图_Seaborn-基于matplotlib的更强力制图库

發布時間:2025/3/19 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 seaborn 画堆叠柱状图_Seaborn-基于matplotlib的更强力制图库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

相信大家在開始python的使用后,便隨后接觸到了matplotlib這個與python兼容的很好的制圖庫。但是,如果想把圖做的更細,更上流,那么則需要seaborn這個庫,比起matplotlib更容易上手,并且和pandas的兩種主要數據結構Series和DataFrame有著很強的兼容性。

1 . 安裝,工欲善其事,安裝 seaborn,有兩種方法。

pip install seabornconda install seaborn

在這里我用的conda的客戶端版。

2. 對于seaborn的介紹,我們從最簡單也是最常用的"柱狀圖"開始。

barplot便是seaborn庫的柱狀圖方法。

seaborn

seaborn所做的柱狀圖

2.1 數據集介紹

在這里我們使用seaborn自帶的數據集tips。

import seaborn as sns tips = sns.load_dataset("tips") print(tips.head())>>>total_bill tip sex smoker day time size 0 16.99 1.01 Female No Sun Dinner 2 1 10.34 1.66 Male No Sun Dinner 3 2 21.01 3.50 Male No Sun Dinner 3 3 23.68 3.31 Male No Sun Dinner 2 4 24.59 3.61 Female No Sun Dinner 4

Tips是Dataframe結構, 其矩陣為(244, 7),一共有244條數據,7個屬性,分別為

  • total_bill: 賬單總額
  • tip: 小費
  • sex: 性別
  • smoker: 是否抽煙
  • day: 周幾(周一 至 周日)
  • time: 上午下午
  • size: 人數

2.2 seaborn.barplot 方法使用以及參數介紹

2.2.1: 嘗試 barplot

sns.barplot(x = 'day', y = 'total_bill', data = tips) plt.show() # 黑線表示置信區間(Confidence interval)

圖2.2.1

橫坐標:周幾

縱坐標:賬單金額

上圖黑線為縱坐標數據的統計數據的

置信區間_百度百科?baike.baidu.com

,如果不改動的話默認為平均數。

2.2.2 使用 hue 分類每一列數據,按性別來分

還是剛才的數據,將賬單金額按男女性別劃分開來,使用到參數 hue 。

sns.barplot(x = 'day', y = 'total_bill', hue= 'sex',data= tips) plt.show()

圖2.2.2

通過 hue 的設置,可以將圖2.2.1按性別更加細化。

2.2.3 畫一個horizontal (水平方向)的圖

sns.barplot(x = 'tip', y = 'day', data=tips) plt.show()

圖2.2.3.1

在barplot 有一個參數為orient, 參數包括 ’v' 和‘h’ ,分別對應垂直方向和水平方向,也可以通過這個函數將圖轉化為水平或者豎直方向。

sns.barplot(x = 'day', y = 'tip', data = tips, orient='v') plt.show()

圖2.2.3.2

2.2.4 顯性的控制bar的排列, 每一列的排序是默認的,但是可以通過參數 order 進行修改

fig = plt.figure() ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(2,1,2) ax1.set_title("default") ax2.set_title("after changing order")sns.barplot(x='time', y = 'tip', data=tips, ax = ax1) # lunch, dinner sns.barplot(x='time', y = 'tip', data=tips, order=['Dinner', 'Lunch'], ax = ax2) # become Dinner, lunch plt.show()

2.2.5 通過修改 estimator 調整統計方式

estimator 可以引用 numpy中的方法,例如max, min, median等。更多的方法可以參考

Statistics - NumPy v1.17 Manual?docs.scipy.orgfrom numpy import median, max, min fig = plt.figure() ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) ax3 = fig.add_subplot(2,2,3) ax4 = fig.add_subplot(2,2,4) ax1.set_title('default average') ax2.set_title('max') ax3.set_title('median') ax4.set_title('min') sns.barplot(x="day", y="tip", data=tips, ax = ax1) sns.barplot(x="day", y="tip", data=tips, estimator= max, ax = ax2) sns.barplot(x="day", y="tip", data=tips, estimator= median, ax = ax3) sns.barplot(x="day", y="tip", data=tips, estimator= min, ax = ax4) plt.show()

2.2.6 通過修改 ci 調整置信區間的大小,默認為95%

fig = plt.figure() ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(2,1,2) ax1.set_title("default") ax2.set_title("after changing ")sns.barplot(x="day", y="tip", data=tips,ax = ax1) sns.barplot(x="day", y="tip", data=tips, ci=45, ax = ax2) plt.show()

ci為45后,黑線明顯變短

2.2.7 ci 還有一個特殊的參數 ci = sd, 則直接使用標準差(standard deviation)進行統計

fig = plt.figure() ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(2,1,2) ax1.set_title("default") ax2.set_title("ci = sd ") sns.barplot(x="day", y="tip", data=tips,ax = ax1) sns.barplot(x="day", y="tip", data=tips, ci='sd', ax = ax2) plt.show()

2.2.8 通過 capsize 調整 ”帽子寬度“, 默認是 None

fig = plt.figure() ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(2,1,2) sns.barplot(x="day", y="tip", data=tips, capsize= 0.2, ax = ax1) sns.barplot(x="day", y="tip", data=tips, capsize= 0.4, ax = ax2) plt.show()

2.2.9 調整 palette 換一種其他的顏色調色板

可用的參數可以看

seaborn.color_palette - seaborn 0.10.1 documentation?seaborn.pydata.org

著這里只舉幾個常用的例子

fig = plt.figure() ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) ax3 = fig.add_subplot(2,2,3) ax4 = fig.add_subplot(2,2,4) ax1.set_title("deep") ax2.set_title('muted') ax3.set_title('bright') ax4.set_title('Blues_d') sns.barplot("size", y="total_bill", data=tips, palette="deep", ax = ax1) sns.barplot("size", y="total_bill", data=tips, palette="muted", ax = ax2) sns.barplot("size", y="total_bill", data=tips, palette="bright", ax = ax3) sns.barplot("size", y="total_bill", data=tips, palette="Blues_d", ax = ax4) plt.show()

2.2.9 hue 的進一步使用

fig = plt.figure() ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(2,1,2) tips['weekend'] = tips['day'].isin(['Sat','Sun']) sns.barplot(x='day', y='tip', data=tips, hue='weekend', ax = ax1) sns.barplot(x='day', y='tip', data=tips, hue='weekend', dodge=False, ax = ax2) plt.show()

默認的hue使用后造成不美觀的現象,使用 dodge=False 可以將空白補齊。

2.2.10 顏色的精細化調整, color 和 saturation(飽和度)的使用

ax = sns.barplot("size", y="total_bill", data=tips,color="salmon", saturation=.5) plt.show()

總結,我們將barplot基本的常用參數都演示了一遍,當然seaborn還有直方圖和散點圖等工具。

可以關注我的公眾號 : 平凡的科研生活,同時也會有其他的數據分析相關的文章分享。

總結

以上是生活随笔為你收集整理的seaborn 画堆叠柱状图_Seaborn-基于matplotlib的更强力制图库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。