""" /lectureNote/chapters/chapt05/codes/plot_sin_cos.py The original code is from http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html """ # Slightly modified: Ian May Fall 2020, and again 2021 import numpy as np import matplotlib.pyplot as plt # Interactive defaults to False, we'll leave it that way # Uncomment the following to change that #plt.interactive(True) # Instead of passing fontsize arguments all the time we can set them globally plt.rc('axes',titlesize=14) plt.rc('axes',labelsize=12) plt.rc('xtick',labelsize=10) plt.rc('ytick',labelsize=10) plt.rc('legend',fontsize=8) # Create a figure of size 8x6 inches, 200 dots per inch (the default is 100 dots/inch) plt.figure(figsize=(8, 6), dpi=200) # Set up a grid and some functions to plot X = np.linspace(-np.pi, np.pi, 50) C, S = np.cos(X), np.sin(X) # Plot cosine with a blue continuous line of width 3.0 (pixels) # Note that plot commands apply to the axis object plt.plot(X, C, color="blue", linewidth=3.0, linestyle="-.") # Plot sine with a green continuous line of width 2.5 and diamonds as markers # compare format string '-gd' with individual keywords from previous call plt.plot(X, S, '-gd', linewidth=2.5, markersize=5) # Set axis labels plt.xlabel(r'$x$') plt.ylabel(r'$y$') # Set x limits and tick positions/lebels plt.xlim(-4.0, 4.0) plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) # Set y limits and ticks plt.ylim(-1.0, 1.0) plt.yticks([-1, 0, +1], [r'$-1$', r'$0$', r'$+1$']) # Set title plt.title(r'$y=\sin(x)$ and $y=\cos(x)$ over $[-\pi, \pi]$') # Set legend # Note that line names are given as a tuple of strings plt.legend((r'$\cos(x)$',r'$\sin(x)$'), loc='upper left') # Show result on screen # You need this when using plt.interactive(False) plt.show()