├── LICENSE ├── basicLinePlotting.py ├── curvePlot_VideoPrep_3dPlot.py └── curvePlot_VideoPrep_fitPlot.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 codinglikemad 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /basicLinePlotting.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib 3 | import matplotlib.pyplot as plt 4 | from matplotlib.animation import FFMpegWriter 5 | 6 | # This needs to be changed for your code 7 | plt.rcParams['animation.ffmpeg_path'] = 'C:\\Users\\spsha\\Desktop\\ffmpeg-4.4-full_build\\bin\\ffmpeg.exe' 8 | 9 | # This is the final example I showed in the code - notice I have 2 "cursor marks" not shown in the video 10 | fig = plt.figure() 11 | l, = plt.plot([], [], 'k-') 12 | l2, = plt.plot([], [], 'm--') 13 | p1, = plt.plot([], [], 'ko') 14 | p2, = plt.plot([], [], 'mo') 15 | 16 | plt.xlabel('xlabel') 17 | plt.ylabel('ylabel') 18 | plt.title('title') 19 | 20 | plt.xlim(-5, 5) 21 | plt.ylim(-5, 5) 22 | 23 | def func(x): 24 | return np.sin(x)*3 25 | 26 | def func2(x): 27 | return np.cos(x)*3 28 | 29 | metadata = dict(title='Movie', artist='codinglikemad') 30 | writer = FFMpegWriter(fps=15, metadata=metadata) 31 | 32 | 33 | xlist = [] 34 | xlist2 = [] 35 | ylist = [] 36 | ylist2 = [] 37 | 38 | with writer.saving(fig, "sinWave2.mp4", 100): 39 | 40 | # Plot the first line and cursor 41 | for xval in np.linspace(-5,5,100): 42 | xlist.append(xval) 43 | ylist.append(func(xval)) 44 | 45 | l.set_data(xlist,ylist) 46 | l2.set_data(xlist2,ylist2) 47 | 48 | p1.set_data(xval,func(xval)) 49 | 50 | writer.grab_frame() 51 | 52 | # plot the second line and cursor 53 | for xval in np.linspace(-5,5,100): 54 | xlist2.append(xval) 55 | ylist2.append(func2(xval)) 56 | 57 | l.set_data(xlist,ylist) 58 | l2.set_data(xlist2,ylist2) 59 | 60 | p2.set_data(xval,func2(xval)) 61 | 62 | writer.grab_frame() 63 | -------------------------------------------------------------------------------- /curvePlot_VideoPrep_3dPlot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib 3 | # matplotlib.use("Agg") # useful for a webserver case where you don't want to ever visualize the result live. 4 | from matplotlib import cm 5 | import matplotlib.pyplot as plt 6 | from matplotlib.animation import FFMpegWriter, PillowWriter 7 | 8 | # Change to reflect your file location! 9 | plt.rcParams['animation.ffmpeg_path'] = 'C:\\Users\\spsha\\Desktop\\ffmpeg-4.4-full_build\\bin\\ffmpeg.exe' 10 | 11 | 12 | # Fixing random state for reproducibility 13 | np.random.seed(19680801) 14 | 15 | 16 | metadata = dict(title='Movie', artist='codinglikemad') 17 | # writer = PillowWriter(fps=15, metadata=metadata) 18 | writer = FFMpegWriter(fps=15, metadata=metadata) 19 | 20 | fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) 21 | 22 | plt.xlim(-5, 5) 23 | plt.ylim(-5, 5) 24 | 25 | 26 | def func(x,y,r,t): 27 | return np.cos(r/2+t)*np.exp(-np.square(r)/50) 28 | 29 | xvec = np.linspace(-10, 10, 1000) 30 | yvec = np.linspace(-10, 10, 1000) 31 | 32 | xlist, ylist = np.meshgrid(xvec, yvec) 33 | 34 | rlist = np.sqrt( np.square(xlist) + np.square(ylist) ) 35 | 36 | with writer.saving(fig, "exp3d.mp4", 100): 37 | for tval in np.linspace(0,20,160): 38 | print(tval) 39 | zval = func(xlist,ylist,rlist, tval) 40 | ax.set_zlim(-1, 1) 41 | ax.plot_surface(xlist,ylist,zval,cmap=cm.viridis) 42 | 43 | writer.grab_frame() 44 | plt.cla() -------------------------------------------------------------------------------- /curvePlot_VideoPrep_fitPlot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib 3 | # matplotlib.use("Agg") # You can uncomment this line to have a totally non-gui experience - useful for webserver use where the file is rendered and shown to a user. 4 | import matplotlib.pyplot as plt 5 | from matplotlib.animation import FFMpegWriter, PillowWriter 6 | 7 | # This will need to be changed to match your directory. 8 | plt.rcParams['animation.ffmpeg_path'] = 'C:\\Users\\spsha\\Desktop\\ffmpeg-4.4-full_build\\bin\\ffmpeg.exe' 9 | 10 | # To make sure your example looks like mine, set the random seed. Helps with fine tuning the graph too. 11 | np.random.seed(102434201) 12 | 13 | metadata = dict(title='Movie', artist='codinglikemad') 14 | # writer = PillowWriter(fps=15, metadata=metadata) 15 | writer = FFMpegWriter(fps=15, metadata=metadata) 16 | 17 | fig = plt.figure() 18 | plt.xlim(-8, 8) 19 | plt.ylim(-8, 8) 20 | 21 | # Generate some random linear data to fit: 22 | def func(x): 23 | return x*1.2 + 0.1 + np.random.normal(0,2, x.shape) 24 | 25 | x = np.random.uniform(-7,7,10) 26 | x = np.sort(x) # Sort the x values here so we get a nice left to right progression in the animation 27 | y = func(x) 28 | 29 | coeff = np.polyfit(x,y,1) 30 | print(coeff) 31 | xline = np.linspace(-6,6,40) # This controls how long the animation takes below. 32 | yline = np.polyval(coeff, xline) 33 | 34 | lPnt, = plt.plot(x, y, 'o') 35 | l, = plt.plot(xline, yline, 'k-', linewidth=3) 36 | 37 | plt.show() 38 | # You need to close the figure for the 2nd half of the script to run - remove the plotting above if you want to generate without intervention. 39 | 40 | 41 | fig = plt.figure() 42 | plt.xlim(-10, 10) 43 | plt.ylim(-10, 10) 44 | 45 | lPnt, = plt.plot([], [], 'o') 46 | l, = plt.plot([], [], 'k-', linewidth=3) 47 | 48 | xLineList = [] 49 | yLineList = [] 50 | 51 | xPntList = [] 52 | yPntList = [] 53 | 54 | # We generate each plot sequentially here 55 | with writer.saving(fig, "fitPlot.mp4", 100): 56 | 57 | # First show the data points 58 | for xval,yval in zip(x,y): 59 | 60 | xPntList.append(xval) 61 | yPntList.append(yval) 62 | 63 | lPnt.set_data(xPntList,yPntList) 64 | l.set_data(xLineList,yLineList) 65 | 66 | # Double up the frames to slow things down a bit here. 67 | writer.grab_frame() 68 | writer.grab_frame() 69 | 70 | # Add the line fit 71 | for xval,yval in zip(xline,yline): 72 | 73 | xLineList.append(xval) 74 | yLineList.append(yval) 75 | 76 | lPnt.set_data(xPntList,yPntList) 77 | l.set_data(xLineList,yLineList) 78 | 79 | writer.grab_frame() 80 | 81 | # We pad at the end to create some "pauseing space" 82 | for ii in range(10): 83 | writer.grab_frame() 84 | --------------------------------------------------------------------------------