Seaborn 和 Matplotlib xaxis 标签和行为

Seaborn and Matplotlib xaxis labels and behaviour

我有附加的 Seaborn 图,我几乎非常非常满意。但是,可以修复两个(相关的?)问题。

首先,x轴范围间距不规则; 1973 年和 2001 年似乎相差“5 年”。

其次,我似乎无法将 RHS 上的 x 轴扩展到例如2023 是为了减少那一边的压扁。

设置

ax.set_xlim(1970,2022)

好像return只是一个空白的情节,很奇怪。关键代码和数据为:

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、...。然后它们得到一个字符串标签。如果您省略 MultipleLocator 并旋转标签 (ax.tick_params(axis='x', labelrotation=45)),您可以看到给定的标签。

要为缺少的年份创建空 space,最简单的方法是 order= 关键字。您可以在那里设置所需的 x 刻度(及其顺序)。

设置 ax.set_xlim(1970,2022) 超出了从 0 到 48 的内部编号。一种方法可能是检索当前的 xlims (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()