Guest User

Untitled

a guest
Jan 23rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.62 KB | None | 0 0
  1. # _*_ coding: utf-8 _*_
  2.  
  3. """
  4. python_visual.py by xianhu
  5. """
  6.  
  7. import numpy as np
  8. import matplotlib
  9. import matplotlib.mlab as mlab
  10. import matplotlib.pyplot as plt
  11. import matplotlib.font_manager as fm
  12. from mpl_toolkits.mplot3d import Axes3D
  13.  
  14. # 解决中文乱码问题
  15. myfont = fm.FontProperties(fname="/Library/Fonts/Songti.ttc", size=14)
  16. matplotlib.rcParams["axes.unicode_minus"] = False
  17.  
  18.  
  19. def simple_plot():
  20. """
  21. simple plot
  22. """
  23. # 生成测试数据
  24. x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
  25. y_cos, y_sin = np.cos(x), np.sin(x)
  26.  
  27. # 生成画布,并设定标题
  28. plt.figure(figsize=(8, 6), dpi=80)
  29. plt.title("简单曲线图", fontproperties=myfont)
  30. plt.grid(True)
  31.  
  32. # 设置X轴
  33. plt.xlabel("X轴", fontproperties=myfont)
  34. plt.xlim(-4.0, 4.0)
  35. plt.xticks(np.linspace(-4, 4, 9, endpoint=True))
  36.  
  37. # 设置Y轴
  38. plt.ylabel("Y轴", fontproperties=myfont)
  39. plt.ylim(-1.0, 1.0)
  40. plt.yticks(np.linspace(-1, 1, 9, endpoint=True))
  41.  
  42. # 画两条曲线
  43. plt.plot(x, y_cos, "b--", linewidth=2.0, label="cos示例")
  44. plt.plot(x, y_sin, "g-", linewidth=2.0, label="sin示例")
  45.  
  46. # 设置图例位置,loc可以为[upper, lower, left, right, center]
  47. plt.legend(loc="upper left", prop=myfont, shadow=True)
  48.  
  49. # 图形显示
  50. plt.show()
  51. return
  52. # simple_plot()
  53.  
  54.  
  55. def simple_advanced_plot():
  56. """
  57. simple advanced plot
  58. """
  59. # 生成测试数据
  60. x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
  61. y_cos, y_sin = np.cos(x), np.sin(x)
  62.  
  63. # 生成画布, 并设定标题
  64. plt.figure(figsize=(8, 6), dpi=80)
  65. plt.title("复杂曲线图", fontproperties=myfont)
  66. plt.grid(True)
  67.  
  68. # 画图的另外一种方式
  69. ax_1 = plt.subplot(111)
  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)
  72.  
  73. # 设置Y轴(左边)
  74. ax_1.set_ylabel("左cos的y轴", fontproperties=myfont)
  75. ax_1.set_ylim(-1.0, 1.0)
  76. ax_1.set_yticks(np.linspace(-1, 1, 9, endpoint=True))
  77.  
  78. # 画图的另外一种方式
  79. ax_2 = ax_1.twinx()
  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)
  82.  
  83. # 设置Y轴(右边)
  84. ax_2.set_ylabel("右sin的y轴", fontproperties=myfont)
  85. ax_2.set_ylim(-2.0, 2.0)
  86. ax_2.set_yticks(np.linspace(-2, 2, 9, endpoint=True))
  87.  
  88. # 设置X轴(共同)
  89. ax_1.set_xlabel("x轴", fontproperties=myfont)
  90. ax_1.set_xlim(-4.0, 4.0)
  91. ax_1.set_xticks(np.linspace(-4, 4, 9, endpoint=True))
  92.  
  93. # 图形显示
  94. plt.show()
  95. return
  96. # simple_advanced_plot()
  97.  
  98.  
  99. def subplot_plot():
  100. """
  101. subplot plot
  102. """
  103. # 子图的style列表
  104. style_list = ["g+-", "r*-", "b.-", "yo-"]
  105.  
  106. # 依次画图
  107. for num in range(4):
  108. # 生成测试数据
  109. x = np.linspace(0.0, 2+num, num=10*(num+1))
  110. y = np.sin((5-num) * np.pi * x)
  111.  
  112. # 子图的生成方式
  113. plt.subplot(2, 2, num+1)
  114. plt.title("子图 %d" % (num+1), fontproperties=myfont)
  115. plt.plot(x, y, style_list[num])
  116.  
  117. # 图形显示
  118. plt.show()
  119. return
  120. # subplot_plot()
  121.  
  122.  
  123. def bar_plot():
  124. """
  125. bar plot
  126. """
  127. # 生成测试数据
  128. means_men = (20, 35, 30, 35, 27)
  129. means_women = (25, 32, 34, 20, 25)
  130.  
  131. # 设置标题
  132. plt.title("柱状图", fontproperties=myfont)
  133.  
  134. # 设置相关参数
  135. index = np.arange(len(means_men))
  136. bar_width = 0.35
  137.  
  138. # 画柱状图
  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)
  142.  
  143. # 设置柱状图标示
  144. for x, y in zip(index, means_men):
  145. plt.text(x, y+0.3, y, ha="center", va="bottom")
  146. for x, y in zip(index, means_women):
  147. plt.text(x+bar_width, y+0.3, y, ha="center", va="bottom")
  148.  
  149. # 设置刻度范围/坐标轴名称等
  150. plt.ylim(0, 45)
  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)
  154.  
  155. # 图形显示
  156. plt.show()
  157. return
  158. # bar_plot()
  159.  
  160.  
  161. def barh_plot():
  162. """
  163. barh plot
  164. """
  165. # 生成测试数据
  166. means_men = (20, 35, 30, 35, 27)
  167. means_women = (25, 32, 34, 20, 25)
  168.  
  169. # 设置标题
  170. plt.title("横向柱状图", fontproperties=myfont)
  171.  
  172. # 设置相关参数
  173. index = np.arange(len(means_men))
  174. bar_height = 0.35
  175.  
  176. # 画柱状图(水平方向)
  177. plt.barh(index, means_men, height=bar_height, alpha=0.2, color="b", label="Men")
  178. plt.barh(index+bar_height, means_women, height=bar_height, alpha=0.8, color="r", label="Women")
  179. plt.legend(loc="upper right", shadow=True)
  180.  
  181. # 设置柱状图标示
  182. for x, y in zip(index, means_men):
  183. plt.text(y+0.3, x, y, ha="left", va="center")
  184. for x, y in zip(index, means_women):
  185. plt.text(y+0.3, x+bar_height, y, ha="left", va="center")
  186.  
  187. # 设置刻度范围/坐标轴名称等
  188. plt.xlim(0, 45)
  189. plt.xlabel("Scores")
  190. plt.ylabel("Group")
  191. plt.yticks(index+(bar_height/2), ("A", "B", "C", "D", "E"))
  192.  
  193. # 图形显示
  194. plt.show()
  195. return
  196. # barh_plot()
  197.  
  198.  
  199. def bar_advanced_plot():
  200. """
  201. bar advanced plot
  202. """
  203. # 生成测试数据
  204. means_men = np.array((20, 35, 30, 35, 27, 25, 32, 34, 20, 25))
  205. means_women = np.array((25, 32, 34, 20, 25, 20, 35, 30, 35, 27))
  206.  
  207. # 设置标题
  208. plt.title("高级柱状图", fontproperties=myfont)
  209.  
  210. # 设置相关参数
  211. index = np.arange(len(means_men))
  212. bar_width = 0.8
  213.  
  214. # 画柱状图(两种:X轴以上/X轴以下)
  215. plt.bar(index, means_men, width=bar_width, alpha=0.4, color="b", label="Men")
  216. plt.bar(index, -means_women, width=bar_width, alpha=0.4, color="r", label="Women")
  217.  
  218. # 画折线图(两种,和柱状图对应)
  219. plt.plot(index, means_men, marker="o", linestyle="-", color="r", label="Men line")
  220. plt.plot(index, -means_women, marker=".", linestyle="--", color="b", label="Women line")
  221.  
  222. # 设置图形标示(两种,和柱状图对应)
  223. for x, y in zip(index, means_men):
  224. plt.text(x, y+1, y, ha="center", va="bottom")
  225. for x, y in zip(index, means_women):
  226. plt.text(x, -y-1, y, ha="center", va="top")
  227.  
  228. # 设置Y轴和图例位置
  229. plt.ylim(-45, 80)
  230. plt.legend(loc="upper left", shadow=True)
  231.  
  232. # 图形显示
  233. plt.show()
  234. return
  235. # bar_advanced_plot()
  236.  
  237.  
  238. def table_plot():
  239. """
  240. table plot
  241. """
  242. # 生成测试数据
  243. data = np.array([
  244. [1, 4, 2, 5, 2],
  245. [2, 1, 1, 3, 6],
  246. [5, 3, 6, 4, 1]
  247. ])
  248.  
  249. # 设置标题
  250. plt.title("层次柱状图", fontproperties=myfont)
  251.  
  252. # 设置相关参数
  253. index = np.arange(len(data[0]))
  254. color_index = ["r", "g", "b"]
  255.  
  256. # 声明底部位置
  257. bottom = np.array([0, 0, 0, 0, 0])
  258.  
  259. # 依次画图,并更新底部位置
  260. for i in range(len(data)):
  261. plt.bar(index, data[i], width=0.5, color=color_index[i], bottom=bottom, alpha=0.7, label="标签 %d" % i)
  262. bottom += data[i]
  263.  
  264. # 设置图例位置
  265. plt.legend(loc="upper left", prop=myfont, shadow=True)
  266.  
  267. # 图形显示
  268. plt.show()
  269. return
  270. # table_plot()
  271.  
  272.  
  273. def histograms_plot():
  274. """
  275. histograms plot
  276. """
  277. # 生成测试数据
  278. mu, sigma = 100, 15
  279. x = mu + sigma * np.random.randn(10000)
  280.  
  281. # 设置标题
  282. plt.title("直方图", fontproperties=myfont)
  283.  
  284. # 画直方图, 并返回相关结果
  285. n, bins, patches = plt.hist(x, bins=50, normed=1, cumulative=False, color="green", alpha=0.6, label="直方图")
  286.  
  287. # 根据直方图返回的结果, 画折线图
  288. y = mlab.normpdf(bins, mu, sigma)
  289. plt.plot(bins, y, "r--", label="线条")
  290.  
  291. # 设置图例位置
  292. plt.legend(loc="upper left", prop=myfont, shadow=True)
  293.  
  294. # 图形显示
  295. plt.show()
  296. return
  297. # histograms_plot()
  298.  
  299.  
  300. def pie_plot():
  301. """
  302. pie plot
  303. """
  304. # 生成测试数据
  305. sizes = [15, 30, 45, 10]
  306. labels = ["Frogs", "中文", "Dogs", "Logs"]
  307. colors = ["yellowgreen", "gold", "lightskyblue", "lightcoral"]
  308.  
  309. # 设置标题
  310. plt.title("饼图", fontproperties=myfont)
  311.  
  312. # 设置突出参数
  313. explode = [0, 0.05, 0, 0]
  314.  
  315. # 画饼状图
  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)
  319. plt.axis("equal")
  320.  
  321. # 图形显示
  322. plt.show()
  323. return
  324. # pie_plot()
  325.  
  326.  
  327. def scatter_plot():
  328. """
  329. scatter plot
  330. """
  331. # 生成测试数据
  332. point_count = 1000
  333. x_index = np.random.random(point_count)
  334. y_index = np.random.random(point_count)
  335.  
  336. # 设置标题
  337. plt.title("散点图", fontproperties=myfont)
  338.  
  339. # 设置相关参数
  340. color_list = np.random.random(point_count)
  341. scale_list = np.random.random(point_count) * 100
  342.  
  343. # 画散点图
  344. plt.scatter(x_index, y_index, s=scale_list, c=color_list, marker="o")
  345.  
  346. # 图形显示
  347. plt.show()
  348. return
  349. # scatter_plot()
  350.  
  351.  
  352. def fill_plot():
  353. """
  354. fill plot
  355. """
  356. # 生成测试数据
  357. x = np.linspace(-2*np.pi, 2*np.pi, 1000, endpoint=True)
  358. y = np.sin(x)
  359.  
  360. # 设置标题
  361. plt.title("填充图", fontproperties=myfont)
  362.  
  363. # 画图
  364. plt.plot(x, y, color="blue", alpha=1.00)
  365.  
  366. # 填充图形, plt.fill_between(x, y1, y2, where=None, *kwargs)
  367. plt.fill_between(x, 0, y, where=(y > 0), color="blue", alpha=0.25)
  368. plt.fill_between(x, 0, y, where=(y < 0), color="red", alpha=0.25)
  369.  
  370. # 图形显示
  371. plt.show()
  372. return
  373. # fill_plot()
  374.  
  375.  
  376. def radar_plot():
  377. """
  378. radar plot
  379. """
  380. # 生成测试数据
  381. labels = np.array(["A组", "B组", "C组", "D组", "E组", "F组"])
  382. data = np.array([68, 83, 90, 77, 89, 73])
  383. theta = np.linspace(0, 2*np.pi, len(data), endpoint=False)
  384.  
  385. # 数据预处理
  386. data = np.concatenate((data, [data[0]]))
  387. theta = np.concatenate((theta, [theta[0]]))
  388.  
  389. # 画图方式
  390. plt.subplot(111, polar=True)
  391. plt.title("雷达图", fontproperties=myfont)
  392.  
  393. # 设置"theta grid"/"radar grid"
  394. plt.thetagrids(theta*(180/np.pi), labels=labels, fontproperties=myfont)
  395. plt.rgrids(np.arange(20, 100, 20), labels=np.arange(20, 100, 20), angle=0)
  396. plt.ylim(0, 100)
  397.  
  398. # 画雷达图,并填充雷达图内部区域
  399. plt.plot(theta, data, "bo-", linewidth=2)
  400. plt.fill(theta, data, color="red", alpha=0.25)
  401.  
  402. # 图形显示
  403. plt.show()
  404. return
  405. # radar_plot()
  406.  
  407.  
  408. def three_dimension_scatter():
  409. """
  410. 3d scatter plot
  411. """
  412. # 生成测试数据
  413. x = np.random.random(100)
  414. y = np.random.random(100)
  415. z = np.random.random(100)
  416. color = np.random.random(100)
  417. scale = np.random.random(100) * 100
  418.  
  419. # 生成画布(两种形式)
  420. fig = plt.figure()
  421. fig.suptitle("三维散点图", fontproperties=myfont)
  422.  
  423. # ax = fig.gca(projection="3d")
  424. ax = fig.add_subplot(111, projection="3d")
  425.  
  426. # 画三维散点图
  427. ax.scatter(x, y, z, s=scale, c=color, marker=".")
  428.  
  429. # 设置坐标轴图标
  430. ax.set_xlabel("X Label")
  431. ax.set_ylabel("Y Label")
  432. ax.set_zlabel("Z Label")
  433.  
  434. # 设置坐标轴范围
  435. ax.set_xlim(0, 1)
  436. ax.set_ylim(0, 1)
  437. ax.set_zlim(0, 1)
  438.  
  439. # 图形显示
  440. plt.show()
  441. return
  442. # three_dimension_scatter()
  443.  
  444.  
  445. def three_dimension_line():
  446. """
  447. 3d line plot
  448. """
  449. # 生成测试数据
  450. x = np.linspace(0, 1, 1000)
  451. y = np.linspace(0, 1, 1000)
  452. z = np.sin(x * 2 * np.pi) / (y + 0.1)
  453.  
  454. # 生成画布(两种形式)
  455. fig = plt.figure()
  456. ax = fig.gca(projection="3d", title="plot title")
  457. # ax = fig.add_subplot(111, projection="3d", title="plot title")
  458.  
  459. # 画三维折线图
  460. ax.plot(x, y, z, color="red", linestyle="-")
  461.  
  462. # 设置坐标轴图标
  463. ax.set_xlabel("X Label")
  464. ax.set_ylabel("Y Label")
  465. ax.set_zlabel("Z Label")
  466.  
  467. # 图形显示
  468. plt.show()
  469. return
  470. # three_dimension_line()
  471.  
  472.  
  473. def three_dimension_bar():
  474. """
  475. 3d bar plot
  476. """
  477. # 生成测试数据(位置数据)
  478. xpos = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  479. ypos = [2, 3, 4, 5, 1, 6, 2, 1, 7, 2]
  480. zpos = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  481.  
  482. # 生成测试数据(柱形参数)
  483. dx = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  484. dy = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  485. dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  486.  
  487. # 生成画布(两种形式)
  488. fig = plt.figure()
  489. ax = fig.gca(projection="3d", title="plot title")
  490.  
  491. # 画三维柱状图
  492. ax.bar3d(xpos, ypos, zpos, dx, dy, dz, alpha=0.5)
  493.  
  494. # 设置坐标轴图标
  495. ax.set_xlabel("X Label")
  496. ax.set_ylabel("Y Label")
  497. ax.set_zlabel("Z Label")
  498.  
  499. # 图形显示
  500. plt.show()
  501. return
  502. # three_dimension_bar()
Add Comment
Please, Sign In to add comment