plt5_ax_setting1.py 1005 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # View more python tutorials on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. # 5 - axis setting
  5. """
  6. Please note, this script is for python3+.
  7. If you are using python2+, please modify it accordingly.
  8. Tutorial reference:
  9. http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html
  10. """
  11. import matplotlib.pyplot as plt
  12. import numpy as np
  13. x = np.linspace(-3, 3, 50)
  14. y1 = 2*x + 1
  15. y2 = x**2
  16. plt.figure()
  17. plt.plot(x, y2)
  18. # plot the second curve in this figure with certain parameters
  19. plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
  20. # set x limits
  21. plt.xlim((-1, 2))
  22. plt.ylim((-2, 3))
  23. plt.xlabel('I am x')
  24. plt.ylabel('I am y')
  25. # set new sticks
  26. new_ticks = np.linspace(-1, 2, 5)
  27. print(new_ticks)
  28. plt.xticks(new_ticks)
  29. # set tick labels
  30. plt.yticks([-2, -1.8, -1, 1.22, 3],
  31. [r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
  32. plt.show()