繁体   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