plt18_secondary_yaxis.py 748 B

12345678910111213141516171819202122232425262728293031
  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. # 18 - secondary y axis
  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.python-course.eu/matplotlib_multiple_figures.php
  10. """
  11. import matplotlib.pyplot as plt
  12. import numpy as np
  13. x = np.arange(0, 10, 0.1)
  14. y1 = 0.05 * x**2
  15. y2 = -1 *y1
  16. fig, ax1 = plt.subplots()
  17. ax2 = ax1.twinx() # mirror the ax1
  18. ax1.plot(x, y1, 'g-')
  19. ax2.plot(x, y2, 'b-')
  20. ax1.set_xlabel('X data')
  21. ax1.set_ylabel('Y1 data', color='g')
  22. ax2.set_ylabel('Y2 data', color='b')
  23. plt.show()