Advertisement
Guest User

Untitled

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