162 lines
3.6 KiB
Python
162 lines
3.6 KiB
Python
|
# _*_ coding: utf-8 _*_
|
||
|
|
||
|
"""
|
||
|
python_visual_animation.py by xianhu
|
||
|
"""
|
||
|
|
||
|
import numpy as np
|
||
|
import matplotlib
|
||
|
import matplotlib.pyplot as plt
|
||
|
import matplotlib.font_manager as fm
|
||
|
from mpl_toolkits.mplot3d import Axes3D
|
||
|
|
||
|
# 解决中文乱码问题
|
||
|
myfont = fm.FontProperties(fname="/Library/Fonts/Songti.ttc", size=14)
|
||
|
matplotlib.rcParams["axes.unicode_minus"] = False
|
||
|
|
||
|
|
||
|
def 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 = 5
|
||
|
x_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 = 100
|
||
|
x = 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()
|