簡體   English   中英

Seaborn 和 Matplotlib xaxis 標簽和行為

[英]Seaborn and Matplotlib xaxis labels and behaviour

我附上了 Seaborn plot ,我對此非常滿意。 但是,可以修復兩個(相關的?)事情。

在此處輸入圖像描述

首先,x 軸范圍間距不規則; 1973 年和 2001 年似乎相隔“5 年”。

其次,我似乎無法將 RHS 上的 x 軸延伸到例如 2023 年,以減少那一側的擠壓。

環境

ax.set_xlim(1970,2022)

似乎只返回一個空白的 plot,這很奇怪。 關鍵代碼和數據是:

import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

ax = sns.boxplot(x=Year, y=No_of_vars)#ax = sns.boxplot(data=[Year, No_of_vars])

#x.set_xlim(1970,2022)
## Sorting out the x-tick labels
loc = ticker.MultipleLocator(base=5.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)

## Axes style
ax.tick_params(axis='both', which='major', labelsize=labelsize*1.2, top=True, right=True, direction='in', length=ticklength,   width=tickwidth)
ax.tick_params(axis='both', which='minor', labelsize=labelsize*1.2, top=True, right=True, direction='in', length=ticklength,   width=tickwidth)

>>> Year
array([2019, 2013, 2020, 2020, 2018, 2007, 2019, 2020, 2008, 2016, 2015,
   2012, 2020, 2019, 2021, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
   2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2019, 2019, 2019,
   2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
   2019, 2019, 2019, 2019, 2019, 2019, 2018, 2018, 2018, 2018, 2018,
   2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
   2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
   2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
   2018, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
   2017, 2017, 2017, 2017, 2017, 2017, 2017, 2016, 2016, 2016, 2016,
   2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
   2016, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015,
   2015, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
   2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2013, 2013, 2013,
   2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2012, 2012,
   2012, 2012, 2012, 2011, 2011, 2011, 2011, 2010, 2010, 2010, 2010,
   2009, 2009, 2009, 2009, 2009, 2008, 2008, 2008, 2008, 2007, 2007,
   2007, 2006, 2005, 2004, 2004, 2003, 2003, 2002, 2005, 2001, 2000,
   2000, 1996, 1990, 1989, 1973, 2021, 2021])

 >>> No_of_vars
 array([29, 33, 29, 18, 31, 28, 19, 28, 23, 32, 31, 28, 27, 23, 38, 33, 32,
   29, 28, 32, 26, 28, 27, 29, 28, 28, 29, 31, 29, 34, 23, 31, 22, 26,
   36, 26, 35, 25, 26, 31, 36, 27, 30, 31, 28, 26, 20, 31, 18, 26, 15,
   28, 15, 15, 26, 29, 24, 22, 34, 30, 26, 28, 38, 29, 21, 28, 25, 22,
   34, 30, 27, 28, 30, 29, 19, 26, 28, 31, 24, 25, 19, 26, 32, 31, 35,
   27, 23, 33, 31, 30, 26, 21, 23, 29, 15, 24, 20, 24, 31, 28, 28, 29,
   32, 23, 28, 18, 30, 28, 30, 30, 29, 25, 32, 32, 28, 29, 26, 30, 33,
   29, 23, 21, 31, 27, 30, 28, 28, 32, 26, 26, 28, 20, 26, 22, 36, 29,
   29, 30, 30, 25, 25, 25, 28, 29, 29, 29, 29, 25, 22, 31, 19, 32, 26,
   21, 30, 21, 24, 25, 26, 23, 28, 26, 27, 27, 21, 20, 29, 18, 28, 28,
   24, 26, 25, 23, 28, 46, 24, 29, 26, 24, 23, 30, 25, 27, 21, 23, 19,
   23, 19, 25, 21, 27, 26, 27, 23, 29, 23, 24, 25, 29, 23, 19, 21, 20,
   32])

箱線圖的 x 軸是分類的。 在內部,它們有數字 0、1、...。然后它們得到一個字符串 label。 如果省略MultipleLocator並旋轉標簽( ax.tick_params(axis='x', labelrotation=45) ),您可以看到給定的標簽。

要為丟失的年份創建空白空間,最簡單的方法是order=關鍵字。 在那里您可以設置所需的 x-ticks(及其順序)。

設置ax.set_xlim(1970,2022)超出了從 0 到 48 的內部編號。一種方法可能是檢索當前的 xlim ( ax.get_xlim() ),然后用一些額外的邊距再次設置它們。

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

Year = [2019, 2013, 2020, 2020, 2018, 2007, 2019, 2020, 2008, 2016, 2015, 2012, 2020, 2019, 2021, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2012, 2012, 2012, 2012, 2012, 2011, 2011, 2011, 2011, 2010, 2010, 2010, 2010, 2009, 2009, 2009, 2009, 2009, 2008, 2008, 2008, 2008, 2007, 2007, 2007, 2006, 2005, 2004, 2004, 2003, 2003, 2002, 2005, 2001, 2000, 2000, 1996, 1990, 1989, 1973, 2021, 2021]
No_of_vars = [29, 33, 29, 18, 31, 28, 19, 28, 23, 32, 31, 28, 27, 23, 38, 33, 32, 29, 28, 32, 26, 28, 27, 29, 28, 28, 29, 31, 29, 34, 23, 31, 22, 26, 36, 26, 35, 25, 26, 31, 36, 27, 30, 31, 28, 26, 20, 31, 18, 26, 15, 28, 15, 15, 26, 29, 24, 22, 34, 30, 26, 28, 38, 29, 21, 28, 25, 22, 34, 30, 27, 28, 30, 29, 19, 26, 28, 31, 24, 25, 19, 26, 32, 31, 35, 27, 23, 33, 31, 30, 26, 21, 23, 29, 15, 24, 20, 24, 31, 28, 28, 29, 32, 23, 28, 18, 30, 28, 30, 30, 29, 25, 32, 32, 28, 29, 26, 30, 33, 29, 23, 21, 31, 27, 30, 28, 28, 32, 26, 26, 28, 20, 26, 22, 36, 29, 29, 30, 30, 25, 25, 25, 28, 29, 29, 29, 29, 25, 22, 31, 19, 32, 26, 21, 30, 21, 24, 25, 26, 23, 28, 26, 27, 27, 21, 20, 29, 18, 28, 28, 24, 26, 25, 23, 28, 46, 24, 29, 26, 24, 23, 30, 25, 27, 21, 23, 19, 23, 19, 25, 21, 27, 26, 27, 23, 29, 23, 24, 25, 29, 23, 19, 21, 20, 32]

ax = sns.boxplot(x=Year, order=range(1973, 2022), y=No_of_vars)  # ax = sns.boxplot(data=[Year, No_of_vars])

ax.xaxis.set_major_locator(ticker.MultipleLocator(5)) # only show ticks for indices multiple of 5

# ax.tick_params(axis='x', labelrotation=45)
ax.tick_params(axis='both', which='both', top=True, right=True, direction='in')
xmin, xmax = ax.get_xlim()
ax.set_xlim(xmin - 1, xmax + 1)  # some more room left and right
plt.show()

帶有額外位置的 sns.boxplot

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM