# _*_ coding: utf-8 _*_"""python_visual_animation.py by xianhu"""import numpy as npimport matplotlibimport matplotlib.pyplot as pltimport matplotlib.font_manager as fmfrom mpl_toolkits.mplot3d import Axes3D# 解决中文乱码问题myfont = fm.FontProperties(fname="/Library/Fonts/Songti.ttc", size=14)matplotlib.rcParams["axes.unicode_minus"] = Falsedef simple_plot():"""simple plot"""# 生成画布plt.figure(figsize=(8, 6), dpi=80)# 打开交互模式plt.ion()# 循环for index in range(100):# 清除原有图像plt.cla()# 设定标题等plt.title("动态曲线图", fontproperties=myfont)plt.grid(True)# 生成测试数据x = np.linspace(-np.pi + 0.1*index, np.pi+0.1*index, 256, endpoint=True)y_cos, y_sin = np.cos(x), np.sin(x)# 设置X轴plt.xlabel("X轴", fontproperties=myfont)plt.xlim(-4 + 0.1*index, 4 + 0.1*index)plt.xticks(np.linspace(-4 + 0.1*index, 4+0.1*index, 9, endpoint=True))# 设置Y轴plt.ylabel("Y轴", fontproperties=myfont)plt.ylim(-1.0, 1.0)plt.yticks(np.linspace(-1, 1, 9, endpoint=True))# 画两条曲线plt.plot(x, y_cos, "b--", linewidth=2.0, label="cos示例")plt.plot(x, y_sin, "g-", linewidth=2.0, label="sin示例")# 设置图例位置,loc可以为[upper, lower, left, right, center]plt.legend(loc="upper left", prop=myfont, shadow=True)# 暂停plt.pause(0.1)# 关闭交互模式plt.ioff()# 图形显示plt.show()return# simple_plot()def scatter_plot():"""scatter plot"""# 打开交互模式plt.ion()# 循环for index in range(50):# 清除原有图像# plt.cla()# 设定标题等plt.title("动态散点图", fontproperties=myfont)plt.grid(True)# 生成测试数据point_count = 5x_index = np.random.random(point_count)y_index = np.random.random(point_count)# 设置相关参数color_list = np.random.random(point_count)scale_list = np.random.random(point_count) * 100# 画散点图plt.scatter(x_index, y_index, s=scale_list, c=color_list, marker="o")# 暂停plt.pause(0.2)# 关闭交互模式plt.ioff()# 显示图形plt.show()return# scatter_plot()def three_dimension_scatter():"""3d scatter plot"""# 生成画布fig = plt.figure()# 打开交互模式plt.ion()# 循环for index in range(50):# 清除原有图像fig.clf()# 设定标题等fig.suptitle("三维动态散点图", fontproperties=myfont)# 生成测试数据point_count = 100x = np.random.random(point_count)y = np.random.random(point_count)z = np.random.random(point_count)color = np.random.random(point_count)scale = np.random.random(point_count) * 100# 生成画布ax = fig.add_subplot(111, projection="3d")# 画三维散点图ax.scatter(x, y, z, s=scale, c=color, marker=".")# 设置坐标轴图标ax.set_xlabel("X Label")ax.set_ylabel("Y Label")ax.set_zlabel("Z Label")# 设置坐标轴范围ax.set_xlim(0, 1)ax.set_ylim(0, 1)ax.set_zlim(0, 1)# 暂停plt.pause(0.2)# 关闭交互模式plt.ioff()# 图形显示plt.show()return# three_dimension_scatter()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。