简体   繁体   中英

Seaborn and Matplotlib xaxis labels and behaviour

I have the attached Seaborn plot which I'm nearly very very happy with. However, two (related?) things could be fixed.

在此处输入图像描述

First, the x-axis range spacing isn't regular; 1973 and 2001 appear to be '5 years' apart.

Second, I can't seem to extend the x-axis on the RHS to eg 2023 in order to make the less squashed at that side.

Setting

ax.set_xlim(1970,2022)

seems to return just a blank plot, which is very weird. Key code and data are:

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])

The x-axis of the boxplot is categorical. Internally, they have numbers 0, 1, .... And then they get a string label. You can see the given labels if you leave out the MultipleLocator and rotate the labels ( ax.tick_params(axis='x', labelrotation=45) ).

To create empty space for missing years, the easiest is the order= keyword. There you can set the desired x-ticks (and their order).

Setting ax.set_xlim(1970,2022) is way outside the internal numbering from 0 to 48. An approach could be to retrieve the current xlims ( ax.get_xlim() ) and then set them again with some extra margin.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM