Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. import pylab as pl
  2. import numpy as np
  3.  
  4. """
  5. This is all from the tutorial located at :
  6.  
  7. http://scipy-lectures.github.io/intro/matplotlib/matplotlib.html
  8.  
  9. """
  10.  
  11.  
  12. # Create a figure of size 8x6 inches, 80 dots per inch
  13. pl.figure(figsize=(10, 6), dpi=80)
  14.  
  15.  
  16. # Create a new subplot from a grid of 1x1
  17. pl.subplot(1, 1, 1)
  18.  
  19.  
  20. X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
  21. C, S = 4 - (1/X**2), 1/X
  22.  
  23.  
  24. #C, S = np.cos(X), np.sin(X)
  25.  
  26.  
  27. # Plot cosine with a blue continuous line of width 1 (pixels)
  28. pl.plot(X, C, color="blue", linewidth=1.0, linestyle="-")
  29.  
  30. # Plot sine with a green continuous line of width 1 (pixels)
  31. pl.plot(X, S, color="green", linewidth=1.0, linestyle="-")
  32.  
  33. # Set x limits
  34. pl.xlim(-4.0, 4.0)
  35.  
  36. # Set x ticks
  37. pl.xticks(np.linspace(-4, 4, 9, endpoint=True))
  38.  
  39. # Set y limits
  40. pl.ylim(-1.0, 1.0)
  41.  
  42. # Set y ticks
  43. pl.yticks(np.linspace(-1, 1, 5, endpoint=True))
  44.  
  45. # Save figure using 72 dots per inch
  46. # savefig("exercice_2.png", dpi=72)
  47.  
  48. """
  49. Editing the above - this is to change the color of the lines and also to alter
  50. the thickness of the lines
  51.  
  52. """
  53.  
  54. pl.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
  55. pl.plot(X, S, color="red", linewidth=2.5, linestyle="-")
  56.  
  57. """
  58. Makes is so that the lines aren't right at the edge of the screen
  59. easier to actually read the graph like this
  60. """
  61.  
  62. pl.xlim(X.min() * 1.1, X.max() * 1.1)
  63. pl.ylim(C.min() * 1.1, C.max() * 1.1)
  64.  
  65. """
  66. Changes the x and y ticks so that they show the values of pi
  67.  
  68. Also uses latex to show the values explicitly with symbols
  69. """
  70.  
  71. pl.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
  72.           [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
  73. pl.yticks([-1,0,1])
  74.  
  75. """
  76. Alters the position of the axis - moves them to the centre
  77. """
  78. ax = pl.gca()  # gca stands for 'get current axis'
  79. ax.spines['right'].set_color('none')
  80. ax.spines['top'].set_color('none')
  81. ax.xaxis.set_ticks_position('bottom')
  82. ax.spines['bottom'].set_position(('data',0))
  83. ax.yaxis.set_ticks_position('left')
  84. ax.spines['left'].set_position(('data',0))
  85.  
  86. """
  87.  
  88. """
  89. pl.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
  90. pl.plot(X, S, color="red",  linewidth=2.5, linestyle="-", label="sine")
  91.  
  92. pl.legend(loc='upper left')
  93.  
  94. """
  95. Annotate the graph!
  96. """
  97. t = 2 * np.pi / 3
  98. pl.plot([t, t], [0, np.cos(t)], color='blue', linewidth=2.5, linestyle="--")
  99. pl.scatter([t, ], [np.cos(t), ], 50, color='blue')
  100.  
  101. pl.annotate(r'$sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
  102.             xy=(t, np.sin(t)), xycoords='data',
  103.             xytext=(+10, +30), textcoords='offset points', fontsize=16,
  104.             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
  105.  
  106. pl.plot([t, t],[0, np.sin(t)], color='red', linewidth=2.5, linestyle="--")
  107. pl.scatter([t, ],[np.sin(t), ], 50, color='red')
  108.  
  109. pl.annotate(r'$cos(\frac{2\pi}{3})=-\frac{1}{2}$',
  110.             xy=(t, np.cos(t)), xycoords='data',
  111.             xytext=(-90, -50), textcoords='offset points', fontsize=16,
  112.             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
  113.  
  114. """
  115.  
  116. """
  117.  
  118. for label in ax.get_xticklabels() + ax.get_yticklabels():
  119.     label.set_fontsize(16)
  120.     label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65))
  121.  
  122.  
  123. # Show result on screen
  124. pl.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement