autumnrd

Rotate minor and major label in matplotlib

Nov 25th, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. figure = plt.figure()
  2. axes = figure.add_subplot (1, 1, 1)
  3.  
  4. axes.plot(m_time, m_temp, linestyle='-', color='orange', label = 'max : '+str(max(m_temp))+'\n'+'min  : '+str(min(m_temp)), linewidth=2)
  5. y_step = matplotlib.ticker.MultipleLocator(base=0.5)
  6. axes.yaxis.set_major_locator(y_step)
  7. plt.xticks(rotation=90)
  8.  
  9. f_hour_min = mpl.dates.DateFormatter('%H:%M')
  10. f_day = mpl.dates.DateFormatter('%a %d. %b %Y')
  11.  
  12. #format grid
  13. axes.xaxis.grid(True, which='major', linestyle='solid')
  14. axes.xaxis.grid(True, which='minor')
  15. axes.yaxis.grid(True, which='major')
  16. axes.axis([min(m_time), max(m_time), min(m_temp)-0.5, max(m_temp)+0.5])
  17.  
  18. #format major labels out
  19. axes.xaxis.set_major_formatter(f_day)
  20. day_tick = mpl.dates.DayLocator()
  21. axes.xaxis.set_major_locator(day_tick)
  22. major_labels = [tick.label1 for tick in axes.xaxis.get_major_ticks()]
  23. for label in major_labels:
  24.     label.set_rotation(90)
  25.     label.set_ha('center')
  26.     label.set_va('bottom')
  27.     label.set_bbox(dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.5'))
  28. axes.xaxis.set_tick_params(which='major', pad=-10)
  29.  
  30. #format minor labels out
  31. axes.xaxis.set_minor_formatter(f_hour_min)
  32. min_tick = mpl.dates.MinuteLocator(interval=30)
  33. axes.xaxis.set_minor_locator(min_tick)
  34. minor_labels = [tick.label1 for tick in axes.xaxis.get_minor_ticks()]
  35. for label in minor_labels:
  36.     label.set_rotation(50)
  37.     label.set_ha('right')
Advertisement
Add Comment
Please, Sign In to add comment