code: def fix_ticks(ax):
      
      plt.setp(ax.get_yticklabels(), ha='center', x=0.5,
               transform=ax._yaxis_transform)
  
      # We'll still wind up with some tick labels beyond axes limits for reasons
      # I don't fully understand...
      limits = ax.get_ylim()
      for label, loc in zip(ax.yaxis.get_ticklabels(), ax.yaxis.get_ticklocs()):
          if loc < min(limits) or loc > max(limits):
              label.set(visible=False)
          else:
              label.set(visible=True)
docs: Center ticklabels and hide any outside axes limits.
  
      By Joe Kington