
Теперь сохраненный видеофайл имеет гораздо более низкое качество и четко показывает «ячейки сетки» в плоскости xy:

Я много пробовал экспериментировать с dpi, сеткой размер и параметры кодека, но мне не удалось получить лучший результат. Как сохранить видео, чтобы оно было того же качества, что и изображение, показанное с помощью plt.show()?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
# Define the cot and csc functions
def cot(x):
return np.cos(x) / np.sin(x)
def csc(x):
return 1 / np.sin(x)
# Define the function p(x, y)
def p(x, y):
h = csc(y) ** 5 - x
return np.exp(-x**2 / 2) * np.exp(-h**2 / 2)
# Create a grid of x and y values on [-10, 10)
x = np.linspace(-10, 10, 1000)
y = np.linspace(-10, 10, 1000)
X, Y = np.meshgrid(x, y)
Z = p(X, Y)
# Set up the figure and 3D axis
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
# Create initial 3D surface plot
surf = ax.plot_surface(X, Y, Z, cmap='viridis')
# Turn off Z-axis grid lines
ax.grid(False)
# Function to update the animation frames, transitioning from 3D to 2D
def update(frame):
if frame < 90:
# Start looking from the side (elevation = 30, azimuth = 45)
elev = 30 + frame * (60 / 90) # Elevate to a top-down view
azim = 45 + frame * (45 / 90) # Rotate so that the X-axis ends up horizontal
ax.view_init(elev = elev, azim = azim) # Move the camera
ax.set_axis_off()
return surf,
else:
# When the transition completes, switch to a contour plot
ax.clear() # Clear the 3D plot
ax.contourf(X, Y, Z, levels = 50, cmap='viridis') # Render contour plot
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_xticks(np.arange(-10, 11, 2)) # Set x-ticks for clarity
ax.set_yticks(np.arange(-10, 11, 2)) # Set y-ticks for clarity
ax.set_aspect('auto') # Set the aspect ratio to auto for proper alignment
ax.set_axis_off()
return ax,
# Create the animation: 90 frames for the transition, then hold the 2D plot
ani = FuncAnimation(fig, update, frames = 100, interval = 50)
# Save the animation as an mp4 video
ani.save('3d_to_2d_transition_fixed.mp4', writer = 'ffmpeg', fps = 24, extra_args = ['-vcodec', 'libx264', '-crf', '0', '-preset', 'veryslow'])
plt.show()
Подробнее здесь: https://stackoverflow.com/questions/790 ... ow-quality