python x轴文本在海运中发生位移

x8goxv8g  于 2023-08-02  发布在  Python
关注(0)|答案(1)|浏览(104)

在下图中,x轴文本发生位移。我试着修理它,但它不起作用。我得到了这个情节


的数据
使用此脚本

from matplotlib import pyplot as plt
import seaborn as sns

plt.figure(figsize=(12, 8))
sns.barplot(data=df, x='water_yr', y='annual_volume', color='b', alpha=0.75)
plt.axhline(y=-28.5, linestyle='--', linewidth=0.5, color='black')
plt.ylim(-50, 0)
plt.xlabel(' ')
plt.ylabel('volume')
plt.xticks(rotation=45)
plt.close()

字符串

数据

df = pd.DataFrame(
{'water_yr': {0: '1989-1990',
  1: '1990-1991',
  2: '1991-1992',
  3: '1992-1993',
  4: '1993-1994',
  5: '1994-1995',
  6: '1995-1996',
  7: '1996-1997',
  8: '1997-1998',
  9: '1998-1999',
  10: '1999-2000',
  11: '2000-2001',
  12: '2001-2002',
  13: '2002-2003',
  14: '2003-2004',
  15: '2004-2005',
  16: '2005-2006',
  17: '2006-2007',
  18: '2007-2008',
  19: '2008-2009',
  20: '2009-2010',
  21: '2010-2011',
  22: '2011-2012',
  23: '2012-2013',
  24: '2013-2014',
  25: '2014-2015',
  26: '2015-2016',
  27: '2016-2017',
  28: '2017-2018',
  29: '2018-2019',
  30: '2019-2020',
  31: '2020-2021',
  32: '2021-2022',
  33: '2022-2023',
  34: '2023-2024',
  35: '2024-2025',
  36: '2025-2026',
  37: '2026-2027',
  38: '2027-2028',
  39: '2028-2029',
  40: '2029-2030',
  41: '2030-2031',
  42: '2031-2032',
  43: '2032-2033',
  44: '2033-2034',
  45: '2034-2035',
  46: '2035-2036',
  47: '2036-2037',
  48: '2037-2038',
  49: '2038-2039',
  50: '2039-2040',
  51: '2040-2041'},
 'annual_volume': {0: -0.6476186274020154,
  1: -0.8874190112462058,
  2: -0.23936598523191396,
  3: -0.1571599545428571,
  4: -1.4375944568772225,
  5: -1.3912562084979787,
  6: -0.6009783517872973,
  7: -1.608737983948505,
  8: -4.525662157378891,
  9: -3.8186180518059625,
  10: -4.767792043743975,
  11: -5.289371384971609,
  12: -3.911092115978629,
  13: -8.330089205502624,
  14: -8.564955107205309,
  15: -11.739122406357902,
  16: -9.727959421485071,
  17: -11.87098233346952,
  18: -12.488696902040065,
  19: -15.993547041347332,
  20: -12.528712995670224,
  21: -13.126507959537776,
  22: -6.849039830931952,
  23: -21.043147994799995,
  24: -15.125645412529918,
  25: -18.672817900800002,
  26: -19.28892380152,
  27: -17.243721617653723,
  28: -15.509295523363635,
  29: -14.821481563617553,
  30: -24.676870488520002,
  31: -19.4671255898,
  32: -17.243721617653723,
  33: -15.509295523363635,
  34: -14.821481563617553,
  35: -24.676870488520002,
  36: -19.4671255898,
  37: -17.243721617653723,
  38: -15.509295523363635,
  39: -14.821481563617553,
  40: -24.676870488520002,
  41: -19.4671255898,
  42: -17.243721617653723,
  43: -15.509295523363635,
  44: -14.821481563617553,
  45: -24.676870488520002,
  46: -19.4671255898,
  47: -17.243721617653723,
  48: -15.509295523363635,
  49: -14.821481563617553,
  50: -24.676870488520002,
  51: -19.4671255898}})

jdgnovmf

jdgnovmf1#

首先,在代码的末尾应该有一个plt.show命令来显示图。然后,为了解决您提出的问题,您应该向代码中添加两个命令。首先,使用plt.tight_layout()确保所有绘图元素都适合图形区域,而不会裁剪或重叠。其次,使用plt.xticks(rotation=45, ha='right'),其中ha='right'指定x轴刻度标签的水平对齐方式。将其设置为'right'会将刻度标签与刻度位置的右侧对齐。最终代码如下:

验证码:

from matplotlib import pyplot as plt
import seaborn as sns

plt.figure(figsize=(12, 8))
sns.barplot(data=df, x='water_yr', y='annual_volume', color='b', alpha=0.75)
plt.axhline(y=-28.5, linestyle='--', linewidth=0.5, color='black')
plt.ylim(-50, 0)
plt.xlabel(' ')
plt.ylabel('volume')
plt.xticks(rotation=45,ha='right')
plt.tight_layout()
plt.show()

字符串

相关问题