Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pylab as pl
- import numpy as np
- """
- This is all from the tutorial located at :
- http://scipy-lectures.github.io/intro/matplotlib/matplotlib.html
- """
- # Create a figure of size 8x6 inches, 80 dots per inch
- pl.figure(figsize=(10, 6), dpi=80)
- # Create a new subplot from a grid of 1x1
- pl.subplot(1, 1, 1)
- X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
- C, S = 4 - (1/X**2), 1/X
- #C, S = np.cos(X), np.sin(X)
- # Plot cosine with a blue continuous line of width 1 (pixels)
- pl.plot(X, C, color="blue", linewidth=1.0, linestyle="-")
- # Plot sine with a green continuous line of width 1 (pixels)
- pl.plot(X, S, color="green", linewidth=1.0, linestyle="-")
- # Set x limits
- pl.xlim(-4.0, 4.0)
- # Set x ticks
- pl.xticks(np.linspace(-4, 4, 9, endpoint=True))
- # Set y limits
- pl.ylim(-1.0, 1.0)
- # Set y ticks
- pl.yticks(np.linspace(-1, 1, 5, endpoint=True))
- # Save figure using 72 dots per inch
- # savefig("exercice_2.png", dpi=72)
- """
- Editing the above - this is to change the color of the lines and also to alter
- the thickness of the lines
- """
- pl.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
- pl.plot(X, S, color="red", linewidth=2.5, linestyle="-")
- """
- Makes is so that the lines aren't right at the edge of the screen
- easier to actually read the graph like this
- """
- pl.xlim(X.min() * 1.1, X.max() * 1.1)
- pl.ylim(C.min() * 1.1, C.max() * 1.1)
- """
- Changes the x and y ticks so that they show the values of pi
- Also uses latex to show the values explicitly with symbols
- """
- pl.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
- [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
- pl.yticks([-1,0,1])
- """
- Alters the position of the axis - moves them to the centre
- """
- ax = pl.gca() # gca stands for 'get current axis'
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- ax.xaxis.set_ticks_position('bottom')
- ax.spines['bottom'].set_position(('data',0))
- ax.yaxis.set_ticks_position('left')
- ax.spines['left'].set_position(('data',0))
- """
- """
- pl.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
- pl.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine")
- pl.legend(loc='upper left')
- """
- Annotate the graph!
- """
- t = 2 * np.pi / 3
- pl.plot([t, t], [0, np.cos(t)], color='blue', linewidth=2.5, linestyle="--")
- pl.scatter([t, ], [np.cos(t), ], 50, color='blue')
- pl.annotate(r'$sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
- xy=(t, np.sin(t)), xycoords='data',
- xytext=(+10, +30), textcoords='offset points', fontsize=16,
- arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
- pl.plot([t, t],[0, np.sin(t)], color='red', linewidth=2.5, linestyle="--")
- pl.scatter([t, ],[np.sin(t), ], 50, color='red')
- pl.annotate(r'$cos(\frac{2\pi}{3})=-\frac{1}{2}$',
- xy=(t, np.cos(t)), xycoords='data',
- xytext=(-90, -50), textcoords='offset points', fontsize=16,
- arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
- """
- """
- for label in ax.get_xticklabels() + ax.get_yticklabels():
- label.set_fontsize(16)
- label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65))
- # Show result on screen
- pl.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement