Skip to content

Commit e37980b

Browse files
committed
update python_visual, support Chinese
1 parent 84eec7b commit e37980b

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

python_visual.py

Lines changed: 34 additions & 33 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("plot title", fontproperties=myfont)
29+
plt.title("可视化标题", fontproperties=myfont)
3030
plt.grid(True)
3131

3232
# 设置X轴
@@ -62,31 +62,31 @@ def simple_advanced_plot():
6262

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

6868
# 画图的另外一种方式
6969
ax_1 = plt.subplot(111)
70-
ax_1.plot(x, y_cos, color="blue", linewidth=2.0, linestyle="--", label="cos in left")
71-
ax_1.legend(loc="upper left", shadow=True)
70+
ax_1.plot(x, y_cos, color="blue", linewidth=2.0, linestyle="--", label="左cos")
71+
ax_1.legend(loc="upper left", prop=myfont, shadow=True)
7272

7373
# 设置Y轴(左边)
74-
ax_1.set_ylabel("y label for cos in left")
74+
ax_1.set_ylabel("左cos的y轴", fontproperties=myfont)
7575
ax_1.set_ylim(-1.0, 1.0)
7676
ax_1.set_yticks(np.linspace(-1, 1, 9, endpoint=True))
7777

7878
# 画图的另外一种方式
7979
ax_2 = ax_1.twinx()
80-
ax_2.plot(x, y_sin, color="green", linewidth=2.0, linestyle="-", label="sin in right")
81-
ax_2.legend(loc="upper right", shadow=True)
80+
ax_2.plot(x, y_sin, color="green", linewidth=2.0, linestyle="-", label="右sin")
81+
ax_2.legend(loc="upper right", prop=myfont, shadow=True)
8282

8383
# 设置Y轴(右边)
84-
ax_2.set_ylabel("y label for sin in right")
84+
ax_2.set_ylabel("右sin的y轴", fontproperties=myfont)
8585
ax_2.set_ylim(-2.0, 2.0)
8686
ax_2.set_yticks(np.linspace(-2, 2, 9, endpoint=True))
8787

8888
# 设置X轴(共同)
89-
ax_1.set_xlabel("x label")
89+
ax_1.set_xlabel("x轴", fontproperties=myfont)
9090
ax_1.set_xlim(-4.0, 4.0)
9191
ax_1.set_xticks(np.linspace(-4, 4, 9, endpoint=True))
9292

@@ -111,11 +111,10 @@ def subplot_plot():
111111

112112
# 子图的生成方式
113113
plt.subplot(2, 2, num+1)
114-
plt.title("subplot %d" % (num+1))
114+
plt.title("子图 %d" % (num+1), fontproperties=myfont)
115115
plt.plot(x, y, style_list[num])
116116

117117
# 图形显示
118-
plt.grid(True)
119118
plt.show()
120119
return
121120
# subplot_plot()
@@ -130,16 +129,16 @@ def bar_plot():
130129
means_women = (25, 32, 34, 20, 25)
131130

132131
# 设置标题
133-
plt.title("plot title")
132+
plt.title("可视化标题", fontproperties=myfont)
134133

135134
# 设置相关参数
136135
index = np.arange(len(means_men))
137136
bar_width = 0.35
138137

139138
# 画柱状图
140-
plt.bar(index, means_men, width=bar_width, alpha=0.2, color="b", label="Men")
141-
plt.bar(index+bar_width, means_women, width=bar_width, alpha=0.8, color="r", label="Women")
142-
plt.legend(loc="upper right", shadow=True)
139+
plt.bar(index, means_men, width=bar_width, alpha=0.2, color="b", label="男生")
140+
plt.bar(index+bar_width, means_women, width=bar_width, alpha=0.8, color="r", label="女生")
141+
plt.legend(loc="upper right", prop=myfont, shadow=True)
143142

144143
# 设置柱状图标示
145144
for x, y in zip(index, means_men):
@@ -149,9 +148,9 @@ def bar_plot():
149148

150149
# 设置刻度范围/坐标轴名称等
151150
plt.ylim(0, 45)
152-
plt.xlabel("Group")
153-
plt.ylabel("Scores")
154-
plt.xticks(index+(bar_width/2), ("A", "B", "C", "D", "E"))
151+
plt.xlabel("分组Group", fontproperties=myfont)
152+
plt.ylabel("得分Scores", fontproperties=myfont)
153+
plt.xticks(index+(bar_width/2), ("A组", "B组", "C组", "D组", "E组"), fontproperties=myfont)
155154

156155
# 图形显示
157156
plt.show()
@@ -248,7 +247,7 @@ def table_plot():
248247
])
249248

250249
# 设置标题
251-
plt.title("plot title")
250+
plt.title("可视化标题", fontproperties=myfont)
252251

253252
# 设置相关参数
254253
index = np.arange(len(data[0]))
@@ -259,11 +258,11 @@ def table_plot():
259258

260259
# 依次画图,并更新底部位置
261260
for i in range(len(data)):
262-
plt.bar(index, data[i], width=0.5, color=color_index[i], bottom=bottom, alpha=0.7, label="label %d" % i)
261+
plt.bar(index, data[i], width=0.5, color=color_index[i], bottom=bottom, alpha=0.7, label="标签 %d" % i)
263262
bottom += data[i]
264263

265264
# 设置图例位置
266-
plt.legend(loc="upper left", shadow=True)
265+
plt.legend(loc="upper left", prop=myfont, shadow=True)
267266

268267
# 图形显示
269268
plt.show()
@@ -280,17 +279,17 @@ def histograms_plot():
280279
x = mu + sigma * np.random.randn(10000)
281280

282281
# 设置标题
283-
plt.title("plot title")
282+
plt.title("可视化标题", fontproperties=myfont)
284283

285284
# 画直方图, 并返回相关结果
286-
n, bins, patches = plt.hist(x, bins=50, normed=1, cumulative=False, color="green", alpha=0.6, label="hist")
285+
n, bins, patches = plt.hist(x, bins=50, normed=1, cumulative=False, color="green", alpha=0.6, label="直方图")
287286

288287
# 根据直方图返回的结果, 画折线图
289288
y = mlab.normpdf(bins, mu, sigma)
290-
plt.plot(bins, y, "r--", label="line")
289+
plt.plot(bins, y, "r--", label="线条")
291290

292291
# 设置图例位置
293-
plt.legend(loc="upper left", shadow=True)
292+
plt.legend(loc="upper left", prop=myfont, shadow=True)
294293

295294
# 图形显示
296295
plt.show()
@@ -304,17 +303,19 @@ def pie_plot():
304303
"""
305304
# 生成测试数据
306305
sizes = [15, 30, 45, 10]
307-
labels = ["Frogs", "Hogs", "Dogs", "Logs"]
306+
labels = ["Frogs", "中文", "Dogs", "Logs"]
308307
colors = ["yellowgreen", "gold", "lightskyblue", "lightcoral"]
309308

310309
# 设置标题
311-
plt.title("plot title")
310+
plt.title("可视化标题", fontproperties=myfont)
312311

313312
# 设置突出参数
314313
explode = [0, 0.05, 0, 0]
315314

316315
# 画饼状图
317-
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct="%1.1f%%", shadow=True, startangle=90)
316+
patches, l_text, p_text = plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct="%1.1f%%", shadow=True, startangle=90)
317+
for text in l_text:
318+
text.set_fontproperties(myfont)
318319
plt.axis("equal")
319320

320321
# 图形显示
@@ -333,7 +334,7 @@ def scatter_plot():
333334
y_index = np.random.random(point_count)
334335

335336
# 设置标题
336-
plt.title("plot title")
337+
plt.title("可视化标题", fontproperties=myfont)
337338

338339
# 设置相关参数
339340
color_list = np.random.random(point_count)
@@ -357,7 +358,7 @@ def fill_plot():
357358
y = np.sin(x)
358359

359360
# 设置标题
360-
plt.title("plot title")
361+
plt.title("可视化标题", fontproperties=myfont)
361362

362363
# 画图
363364
plt.plot(x, y, color="blue", alpha=1.00)
@@ -377,7 +378,7 @@ def radar_plot():
377378
radar plot
378379
"""
379380
# 生成测试数据
380-
labels = np.array(["A", "B", "C", "D", "E", "F"])
381+
labels = np.array(["A组", "B组", "C组", "D组", "E组", "F组"])
381382
data = np.array([68, 83, 90, 77, 89, 73])
382383
theta = np.linspace(0, 2*np.pi, len(data), endpoint=False)
383384

@@ -387,10 +388,10 @@ def radar_plot():
387388

388389
# 画图方式
389390
plt.subplot(111, polar=True)
390-
plt.title("plot title")
391+
plt.title("可视化标题", fontproperties=myfont)
391392

392393
# 设置"theta grid"/"radar grid"
393-
plt.thetagrids(theta*(180/np.pi), labels=labels)
394+
plt.thetagrids(theta*(180/np.pi), labels=labels, fontproperties=myfont)
394395
plt.rgrids(np.arange(20, 100, 20), labels=np.arange(20, 100, 20), angle=0)
395396
plt.ylim(0, 100)
396397

0 commit comments

Comments
 (0)