【发布时间】:2021-08-03 19:49:11
【问题描述】:
我有附带的 Seaborn 情节,我对此非常满意。但是,有两个(相关的?)问题可以解决。
首先,x 轴范围间距不规则; 1973 年和 2001 年似乎相隔“5 年”。
其次,我似乎无法将 RHS 上的 x 轴扩展到例如2023 年,以减少那一侧的挤压。
设置
ax.set_xlim(1970,2022)
似乎只返回一个空白图,这很奇怪。关键代码和数据是:
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])
【问题讨论】:
标签: python matplotlib seaborn data-visualization boxplot