add python_visual_animation.py

master
xianhu 2017-11-23 10:59:37 +08:00
parent 53753da3ee
commit 480411f2d2
2 changed files with 178 additions and 15 deletions

View File

@ -26,7 +26,7 @@ def simple_plot():
# 生成画布,并设定标题
plt.figure(figsize=(8, 6), dpi=80)
plt.title("可视化标题", fontproperties=myfont)
plt.title("简单曲线图", fontproperties=myfont)
plt.grid(True)
# 设置X轴
@ -40,8 +40,8 @@ def simple_plot():
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")
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)
@ -62,7 +62,7 @@ def simple_advanced_plot():
# 生成画布, 并设定标题
plt.figure(figsize=(8, 6), dpi=80)
plt.title("可视化标题", fontproperties=myfont)
plt.title("复杂曲线图", fontproperties=myfont)
plt.grid(True)
# 画图的另外一种方式
@ -129,7 +129,7 @@ def bar_plot():
means_women = (25, 32, 34, 20, 25)
# 设置标题
plt.title("可视化标题", fontproperties=myfont)
plt.title("柱状图", fontproperties=myfont)
# 设置相关参数
index = np.arange(len(means_men))
@ -167,7 +167,7 @@ def barh_plot():
means_women = (25, 32, 34, 20, 25)
# 设置标题
plt.title("plot title")
plt.title("横向柱状图", fontproperties=myfont)
# 设置相关参数
index = np.arange(len(means_men))
@ -205,7 +205,7 @@ def bar_advanced_plot():
means_women = np.array((25, 32, 34, 20, 25, 20, 35, 30, 35, 27))
# 设置标题
plt.title("plot title")
plt.title("高级柱状图", fontproperties=myfont)
# 设置相关参数
index = np.arange(len(means_men))
@ -247,7 +247,7 @@ def table_plot():
])
# 设置标题
plt.title("可视化标题", fontproperties=myfont)
plt.title("层次柱状图", fontproperties=myfont)
# 设置相关参数
index = np.arange(len(data[0]))
@ -279,7 +279,7 @@ def histograms_plot():
x = mu + sigma * np.random.randn(10000)
# 设置标题
plt.title("可视化标题", fontproperties=myfont)
plt.title("直方图", fontproperties=myfont)
# 画直方图, 并返回相关结果
n, bins, patches = plt.hist(x, bins=50, normed=1, cumulative=False, color="green", alpha=0.6, label="直方图")
@ -307,7 +307,7 @@ def pie_plot():
colors = ["yellowgreen", "gold", "lightskyblue", "lightcoral"]
# 设置标题
plt.title("可视化标题", fontproperties=myfont)
plt.title("饼图", fontproperties=myfont)
# 设置突出参数
explode = [0, 0.05, 0, 0]
@ -334,7 +334,7 @@ def scatter_plot():
y_index = np.random.random(point_count)
# 设置标题
plt.title("可视化标题", fontproperties=myfont)
plt.title("散点图", fontproperties=myfont)
# 设置相关参数
color_list = np.random.random(point_count)
@ -358,7 +358,7 @@ def fill_plot():
y = np.sin(x)
# 设置标题
plt.title("可视化标题", fontproperties=myfont)
plt.title("填充图", fontproperties=myfont)
# 画图
plt.plot(x, y, color="blue", alpha=1.00)
@ -388,7 +388,7 @@ def radar_plot():
# 画图方式
plt.subplot(111, polar=True)
plt.title("可视化标题", fontproperties=myfont)
plt.title("雷达图", fontproperties=myfont)
# 设置"theta grid"/"radar grid"
plt.thetagrids(theta*(180/np.pi), labels=labels, fontproperties=myfont)
@ -418,8 +418,10 @@ def three_dimension_scatter():
# 生成画布(两种形式)
fig = plt.figure()
# ax = fig.gca(projection="3d", title="plot title")
ax = fig.add_subplot(111, projection="3d", title="plot title")
fig.suptitle("三维散点图", fontproperties=myfont)
# ax = fig.gca(projection="3d")
ax = fig.add_subplot(111, projection="3d")
# 画三维散点图
ax.scatter(x, y, z, s=scale, c=color, marker=".")

View File

@ -0,0 +1,161 @@
# _*_ 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()