This article records how to simuate the zoom out/in and moving camera synchronously when using matplotlib to render the animation

below code is to assume we are going to zoom in and move the camera into the moving object when come around the frame in index in the list ‘momentIndex = [2181, 89799]’.

function of scale_range is to aim to set the scale factor.

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from scipy import misc
from numpy import linspace
import numpy as np
from matplotlib import animation
import math

def scale_range(original_range,k):
  original_min, original_max = original_range

  center = (original_min + original_max)/2

  # Calculate the scaling factor
  #k = (new_max - new_min) / (original_max - original_min)
  # Apply the scaling transformation to each value in the range
  scaled_values = [center + k * (x - center) for x in original_range]

  return scaled_values


golden_ratio = (1 + np.sqrt(5)) / 2  # phi

arr1 = np.linspace(0, 466 * np.pi, 90000)  # [:frameIndex]

arr2 = np.linspace(0, 466 * np.pi * golden_ratio, 90000)  # [:frameIndex]
viewRangeX = np.array([-4.5, 4.5])
viewRangeY = np.array([-4.5, 4.5])
R1 = 2.2
R2 = 2.2
dotCenterX = R1 * np.cos(arr1)
dotCenterY = R1 * np.sin(arr1)

dotMoveinX = R1 * np.cos(arr1) + R2 * np.cos(arr2)
dotMoveinY = R1 * np.sin(arr1) + R2 * np.sin(arr2)

frameIndex = len(arr1)

plt.style.use("dark_background")
fig, ax = plt.subplots(figsize=[5, 5])
momentIndex = [2181, 89799] ##13131,>> 0.004329390237789839
#moveIndex = [0, 1181, 1918, 3098, 89799]
"""
points1 = np.array(np.linspace(0,1,50))
points2 = np.array(np.linspace(1,0,50))
pointss = np.concatenate((points1,points2),axis=0)
"""


###
count = 50
pointss = [1 for x in range(count)] + [ (count-_)/count  for _ in range(count) ]

t = []
tt = np.linspace(0.3,1,50)
for x in tt:
    t.append(x)
zooms   = [0.3 for x in range(count)]+t


imageIndex = 1
index = 1
while True:

    dotMoveinXX = dotMoveinX[:int(index)]
    dotMoveinYY = dotMoveinY[:int(index)]
    viewRangeX = np.array([-4.5, 4.5])
    viewRangeY = np.array([-4.5, 4.5])


    movePercent = 0
    zoomPercent = 1

    for m in momentIndex:
        if abs(index - m) < 100:
            movePercent = pointss[abs(int(index - m))]
            zoomPercent = zooms[abs(int(index - m))]

    x = dotMoveinX[int(index)]
    y = dotMoveinY[int(index)]

    viewRangeX = viewRangeX + x * movePercent
    viewRangeY = viewRangeY + y * movePercent

    viewRangeX = scale_range(viewRangeX, zoomPercent)
    viewRangeY = scale_range(viewRangeY, zoomPercent)

    ax.set_xlim(viewRangeX[0], viewRangeX[1])
    ax.set_ylim(viewRangeY[0], viewRangeY[1])

    plt.axis('off')
    ax.plot(dotMoveinXX, dotMoveinYY, linewidth=0.3, color='w')
    ax.plot([0, dotCenterX[int(index)-1], dotMoveinX[int(index)-1]],
            [0, dotCenterY[int(index)-1], dotMoveinY[int(index)-1]], '-ok', linewidth=0.2,
            markersize=0.2, color='w')

    plt.savefig("C:/PythonWeb/34-1/img{}.png".format(index), dpi=400)
    plt.cla()

    index = index + 1

    if index >= 90000-1:
        break

By Admin

Think-Math Website