Skip to content

Commit 480411f

Browse files
committed
add python_visual_animation.py
1 parent 53753da commit 480411f

File tree

2 files changed

+178
-15
lines changed

2 files changed

+178
-15
lines changed

python_visual.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def simple_plot():
2626

2727
# 生成画布,并设定标题
2828
plt.figure(figsize=(8, 6), dpi=80)
29-
plt.title("可视化标题", fontproperties=myfont)
29+
plt.title("简单曲线图", fontproperties=myfont)
3030
plt.grid(True)
3131

3232
# 设置X轴
@@ -40,8 +40,8 @@ def simple_plot():
4040
plt.yticks(np.linspace(-1, 1, 9, endpoint=True))
4141

4242
# 画两条曲线
43-
plt.plot(x, y_cos, "b--", linewidth=2.0, label="cos事例")
44-
plt.plot(x, y_sin, "g-", linewidth=2.0, label="sin事例")
43+
plt.plot(x, y_cos, "b--", linewidth=2.0, label="cos示例")
44+
plt.plot(x, y_sin, "g-", linewidth=2.0, label="sin示例")
4545

4646
# 设置图例位置,loc可以为[upper, lower, left, right, center]
4747
plt.legend(loc="upper left", prop=myfont, shadow=True)
@@ -62,7 +62,7 @@ def simple_advanced_plot():
6262

6363
# 生成画布, 并设定标题
6464
plt.figure(figsize=(8, 6), dpi=80)
65-
plt.title("可视化标题", fontproperties=myfont)
65+
plt.title("复杂曲线图", fontproperties=myfont)
6666
plt.grid(True)
6767

6868
# 画图的另外一种方式
@@ -129,7 +129,7 @@ def bar_plot():
129129
means_women = (25, 32, 34, 20, 25)
130130

131131
# 设置标题
132-
plt.title("可视化标题", fontproperties=myfont)
132+
plt.title("柱状图", fontproperties=myfont)
133133

134134
# 设置相关参数
135135
index = np.arange(len(means_men))
@@ -167,7 +167,7 @@ def barh_plot():
167167
means_women = (25, 32, 34, 20, 25)
168168

169169
# 设置标题
170-
plt.title("plot title")
170+
plt.title("横向柱状图", fontproperties=myfont)
171171

172172
# 设置相关参数
173173
index = np.arange(len(means_men))
@@ -205,7 +205,7 @@ def bar_advanced_plot():
205205
means_women = np.array((25, 32, 34, 20, 25, 20, 35, 30, 35, 27))
206206

207207
# 设置标题
208-
plt.title("plot title")
208+
plt.title("高级柱状图", fontproperties=myfont)
209209

210210
# 设置相关参数
211211
index = np.arange(len(means_men))
@@ -247,7 +247,7 @@ def table_plot():
247247
])
248248

249249
# 设置标题
250-
plt.title("可视化标题", fontproperties=myfont)
250+
plt.title("层次柱状图", fontproperties=myfont)
251251

252252
# 设置相关参数
253253
index = np.arange(len(data[0]))
@@ -279,7 +279,7 @@ def histograms_plot():
279279
x = mu + sigma * np.random.randn(10000)
280280

281281
# 设置标题
282-
plt.title("可视化标题", fontproperties=myfont)
282+
plt.title("直方图", fontproperties=myfont)
283283

284284
# 画直方图, 并返回相关结果
285285
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():
307307
colors = ["yellowgreen", "gold", "lightskyblue", "lightcoral"]
308308

309309
# 设置标题
310-
plt.title("可视化标题", fontproperties=myfont)
310+
plt.title("饼图", fontproperties=myfont)
311311

312312
# 设置突出参数
313313
explode = [0, 0.05, 0, 0]
@@ -334,7 +334,7 @@ def scatter_plot():
334334
y_index = np.random.random(point_count)
335335

336336
# 设置标题
337-
plt.title("可视化标题", fontproperties=myfont)
337+
plt.title("散点图", fontproperties=myfont)
338338

339339
# 设置相关参数
340340
color_list = np.random.random(point_count)
@@ -358,7 +358,7 @@ def fill_plot():
358358
y = np.sin(x)
359359

360360
# 设置标题
361-
plt.title("可视化标题", fontproperties=myfont)
361+
plt.title("填充图", fontproperties=myfont)
362362

363363
# 画图
364364
plt.plot(x, y, color="blue", alpha=1.00)
@@ -388,7 +388,7 @@ def radar_plot():
388388

389389
# 画图方式
390390
plt.subplot(111, polar=True)
391-
plt.title("可视化标题", fontproperties=myfont)
391+
plt.title("雷达图", fontproperties=myfont)
392392

393393
# 设置"theta grid"/"radar grid"
394394
plt.thetagrids(theta*(180/np.pi), labels=labels, fontproperties=myfont)
@@ -418,8 +418,10 @@ def three_dimension_scatter():
418418

419419
# 生成画布(两种形式)
420420
fig = plt.figure()
421-
# ax = fig.gca(projection="3d", title="plot title")
422-
ax = fig.add_subplot(111, projection="3d", title="plot title")
421+
fig.suptitle("三维散点图", fontproperties=myfont)
422+
423+
# ax = fig.gca(projection="3d")
424+
ax = fig.add_subplot(111, projection="3d")
423425

424426
# 画三维散点图
425427
ax.scatter(x, y, z, s=scale, c=color, marker=".")

python_visual_animation.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# _*_ coding: utf-8 _*_
2+
3+
"""
4+
python_visual_animation.py by xianhu
5+
"""
6+
7+
import numpy as np
8+
import matplotlib
9+
import matplotlib.pyplot as plt
10+
import matplotlib.font_manager as fm
11+
from mpl_toolkits.mplot3d import Axes3D
12+
13+
# 解决中文乱码问题
14+
myfont = fm.FontProperties(fname="/Library/Fonts/Songti.ttc", size=14)
15+
matplotlib.rcParams["axes.unicode_minus"] = False
16+
17+
18+
def simple_plot():
19+
"""
20+
simple plot
21+
"""
22+
# 生成画布
23+
plt.figure(figsize=(8, 6), dpi=80)
24+
25+
# 打开交互模式
26+
plt.ion()
27+
28+
# 循环
29+
for index in range(100):
30+
# 清除原有图像
31+
plt.cla()
32+
33+
# 设定标题等
34+
plt.title("动态曲线图", fontproperties=myfont)
35+
plt.grid(True)
36+
37+
# 生成测试数据
38+
x = np.linspace(-np.pi + 0.1*index, np.pi+0.1*index, 256, endpoint=True)
39+
y_cos, y_sin = np.cos(x), np.sin(x)
40+
41+
# 设置X轴
42+
plt.xlabel("X轴", fontproperties=myfont)
43+
plt.xlim(-4 + 0.1*index, 4 + 0.1*index)
44+
plt.xticks(np.linspace(-4 + 0.1*index, 4+0.1*index, 9, endpoint=True))
45+
46+
# 设置Y轴
47+
plt.ylabel("Y轴", fontproperties=myfont)
48+
plt.ylim(-1.0, 1.0)
49+
plt.yticks(np.linspace(-1, 1, 9, endpoint=True))
50+
51+
# 画两条曲线
52+
plt.plot(x, y_cos, "b--", linewidth=2.0, label="cos示例")
53+
plt.plot(x, y_sin, "g-", linewidth=2.0, label="sin示例")
54+
55+
# 设置图例位置,loc可以为[upper, lower, left, right, center]
56+
plt.legend(loc="upper left", prop=myfont, shadow=True)
57+
58+
# 暂停
59+
plt.pause(0.1)
60+
61+
# 关闭交互模式
62+
plt.ioff()
63+
64+
# 图形显示
65+
plt.show()
66+
return
67+
# simple_plot()
68+
69+
70+
def scatter_plot():
71+
"""
72+
scatter plot
73+
"""
74+
# 打开交互模式
75+
plt.ion()
76+
77+
# 循环
78+
for index in range(50):
79+
# 清除原有图像
80+
# plt.cla()
81+
82+
# 设定标题等
83+
plt.title("动态散点图", fontproperties=myfont)
84+
plt.grid(True)
85+
86+
# 生成测试数据
87+
point_count = 5
88+
x_index = np.random.random(point_count)
89+
y_index = np.random.random(point_count)
90+
91+
# 设置相关参数
92+
color_list = np.random.random(point_count)
93+
scale_list = np.random.random(point_count) * 100
94+
95+
# 画散点图
96+
plt.scatter(x_index, y_index, s=scale_list, c=color_list, marker="o")
97+
98+
# 暂停
99+
plt.pause(0.2)
100+
101+
# 关闭交互模式
102+
plt.ioff()
103+
104+
# 显示图形
105+
plt.show()
106+
return
107+
# scatter_plot()
108+
109+
110+
def three_dimension_scatter():
111+
"""
112+
3d scatter plot
113+
"""
114+
# 生成画布
115+
fig = plt.figure()
116+
117+
# 打开交互模式
118+
plt.ion()
119+
120+
# 循环
121+
for index in range(50):
122+
# 清除原有图像
123+
fig.clf()
124+
125+
# 设定标题等
126+
fig.suptitle("三维动态散点图", fontproperties=myfont)
127+
128+
# 生成测试数据
129+
point_count = 100
130+
x = np.random.random(point_count)
131+
y = np.random.random(point_count)
132+
z = np.random.random(point_count)
133+
color = np.random.random(point_count)
134+
scale = np.random.random(point_count) * 100
135+
136+
# 生成画布
137+
ax = fig.add_subplot(111, projection="3d")
138+
139+
# 画三维散点图
140+
ax.scatter(x, y, z, s=scale, c=color, marker=".")
141+
142+
# 设置坐标轴图标
143+
ax.set_xlabel("X Label")
144+
ax.set_ylabel("Y Label")
145+
ax.set_zlabel("Z Label")
146+
147+
# 设置坐标轴范围
148+
ax.set_xlim(0, 1)
149+
ax.set_ylim(0, 1)
150+
ax.set_zlim(0, 1)
151+
152+
# 暂停
153+
plt.pause(0.2)
154+
155+
# 关闭交互模式
156+
plt.ioff()
157+
158+
# 图形显示
159+
plt.show()
160+
return
161+
# three_dimension_scatter()

0 commit comments

Comments
 (0)