动画中的抗锯齿字体

2024-06-02 16:46:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个包含两个子画面的动画,一个是3D,另一个是2D。但是,我似乎不知道是否有办法从2D轴获得更好的字体渲染效果。我试着用font_manager摆弄各种设置,甚至把frame_format改成raw,但都没有成功。有人知道怎么解决这个问题吗?我得到了与mpeg4相同的结果。你知道吗

奇怪的是,3D图形似乎能够正确地呈现字体。你知道吗

import numpy as np

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D

w, h = matplotlib.figure.figaspect(.5)
fig = plt.figure(figsize=(w,h))

ax3d  = fig.add_subplot(121, projection='3d')
ax2d  = fig.add_subplot(122)

ax3d.set_xlim(-3, 3)
ax3d.set_ylim(-3, 3)
ax3d.azim = -90
ax3d.elev = 0
ax3d.set_title('Car on Parking Ramp')

ax2d.set_xlim(-20,20)
ax2d.set_ylim(-20,20)
ax2d.set_ylabel('y')
ax2d.set_xlabel('x')
ax2d.set_title('Intersection with z=0')

''' Helix '''
K = 3          ## Angular velocity
H = 2*np.pi    ## Height

t = np.linspace(0, H, 100, endpoint=True)

x = np.cos(K*t)
y = np.sin(K*t)
z = H - t

ax3d.plot(x, y, z, color='k')

''' z = 0 Plane '''
xx, yy = np.meshgrid([-20,20], [-20,20])
ax3d.plot_surface(xx, yy, 0, alpha=0.3, facecolor='b', rstride=1, cstride=1, shade=True)
ax3d.set_axis_off()

''' Tangent Line Data '''
xdata = np.array([ np.cos(K*t), np.cos(K*t) - K*(H - t)*np.sin(K*t) ])
ydata = np.array([ np.sin(K*t), np.sin(K*t) + K*(H - t)*np.cos(K*t) ])

''' Graph Lines '''
proj,     = ax2d.plot([],[])
tangent,  = ax3d.plot([], [], [], color='b')


def update_graph(n, tangent, proj, xdata, ydata):    
    tangent.set_data(xdata[:,n],
                     ydata[:,n])

    tangent.set_3d_properties([H - t[n], 0])

    proj.set_xdata(xdata[1,:n])
    proj.set_ydata(ydata[1,:n])


ani = animation.FuncAnimation(fig, update_graph, len(t), 
                              fargs=(tangent, proj, xdata, ydata), interval=75, blit=True)

ani.save('im.gif', writer='imagemagick', fps=10)
#ani.save('im.mp4', extra_args=['-vcodec', 'libx264'])

Results


Tags: importplotmatplotlibasnpfigtangentsin