plt12_contours.py 982 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. # 12 - contours
  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. def f(x,y):
  14. # the height function
  15. return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 -y**2)
  16. n = 256
  17. x = np.linspace(-3, 3, n)
  18. y = np.linspace(-3, 3, n)
  19. X,Y = np.meshgrid(x, y)
  20. # use plt.contourf to filling contours
  21. # X, Y and value for (X,Y) point
  22. plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot)
  23. # use plt.contour to add contour lines
  24. C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=.5)
  25. # adding label
  26. plt.clabel(C, inline=True, fontsize=10)
  27. plt.xticks(())
  28. plt.yticks(())
  29. plt.show()