from matplotlib import pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation # graphing parameters font = {'size':16} fig,ax = plt.subplots() plt.rcParams['savefig.facecolor'] = '#c0c0c0' plt.rc('font', **font) marker_style = dict(linestyle = 'none', markersize = 10, markeredgecolor = 'k') ax.set_aspect(1) (min_plot, max_plot) = (0, 10) bar_extent = 0.4 height_above_bar = 0.4 # input data left_points = [1, 3] right_points = [6, 6.5, 8.3] divisions = (3.7, 4.5, 5.2) def slope(point): if point', 'lw':1, 'facecolor':'k'}, va = 'center', zorder = -1) ax.annotate('$x$', xy = (0, 0), xytext = (max_plot + 0.2, 0), va = 'center') ax.annotate('$y$', xy = (0, 0), xytext = (0, 2), ha = 'center') ax.annotate('', xy = (0, 1.7), xytext = (0, -1.7), arrowprops = {'arrowstyle':'<->', 'lw':1, 'facecolor':'k'}, va = 'center') plt.box(on = None) ax.spines['bottom'].set_position('zero') ax.spines['bottom'].set_color('none') ax.spines['left'].set_position('zero') ax.spines['left'].set_color('none') ax.spines['left'].set_linewidth(1) ax.spines['bottom'].set_linewidth(1) plt.xticks(np.arange(2, 9, 2), fontsize = 10) plt.yticks(np.arange(-1, 2, 1), fontsize = 10) ax.plot(left_points, np.zeros(len(left_points)) - 1, marker = "^", markerfacecolor = 'red', **marker_style) ax.plot(right_points,np.zeros(len(right_points)) + 1, marker = "s", markerfacecolor = 'blue', **marker_style) plt.xlim([-1, 11]) plt.ylim([-2.3, 2.7]) # set animation parameters, initial states, and expected motion paths frame_number = 50 darken_margin = 0.1 def init(): return plane, ann1, ann2 def update(i): point = divisions[2] - abs(frame_number/2 - i)/(frame_number/2)*(divisions[2] - divisions[0]) plane.set_ydata((slope(point)*(0 - point), slope(point)*(10 - point))) plane.set_color('k' if abs(point - divisions[1]) < darken_margin else '0.5') ann1.set_position((point, bar_extent)) ann1.xy = (point, -bar_extent) ann1.arrow_patch.set_color('k' if abs(point - divisions[1]) < darken_margin else '0.5') ann2_text = 'B' if abs(point - divisions[1]) < darken_margin else ('A' if abs(point - divisions[0]) < darken_margin else ('C' if abs(point - divisions[2]) < darken_margin else '')) ann2.set_text(ann2_text) ann2.set_position((point, bar_extent + height_above_bar)) ann2.set_color('k' if abs(point - divisions[1]) < darken_margin else '0.5') return plane, ann1, ann2 # implement the animation and save the results anim = FuncAnimation(fig, update, frames = np.arange(0, frame_number),interval = 100, init_func = init, blit = True) anim.save('graph.gif', dpi = 200, writer = 'imagemagick') plt.show()