PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/PaperPainting.py

https://gitlab.com/m10774708/dotcode
Python | 309 lines | 156 code | 65 blank | 88 comment | 13 complexity | 22c415f4636e0caee7896f1fbbc22dfa MD5 | raw file
  1. from pymongo import MongoClient
  2. from sklearn import cluster, datasets, preprocessing, metrics
  3. from sklearn.linear_model import LinearRegression
  4. from scipy.spatial.distance import cdist
  5. import scipy
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. import pandas as pd
  9. import traceback
  10. import csv
  11. import os
  12. import time
  13. import datetime
  14. import gc
  15. import PaperCommon as pc
  16. def plot_multi_yaxis(xaxis, yaxisList, xlabel, ylabelsList):
  17. fig, ax = plt.subplots(figsize=(10, 6))
  18. fig.subplots_adjust(right=0.75)
  19. twin1 = ax.twinx()
  20. twin2 = ax.twinx()
  21. #twin1 = ax.twiny()
  22. twin2 = twin1.twiny()
  23. # Offset the right spine of twin2. The ticks and label have already been
  24. # placed on the right by twinx above.
  25. #twin2.spines['right'].set_position(("axes", 1.2))
  26. yaxisP1, yaxisP2, yaxisP3 = yaxisList
  27. ylabel1, ylabel2, ylabel3 = ylabelsList
  28. p1, = ax.plot(xaxis, yaxisP1, "b-", label=ylabel1)
  29. p2, = twin1.plot(xaxis, yaxisP2, "r-", label=ylabel2)
  30. p3, = twin2.plot(xaxis, yaxisP3, "g-", label=ylabel3)
  31. ax.set_xlim(0, 60)
  32. ax.set_ylim(-1, 3)
  33. twin1.set_ylim(0, 2000)
  34. #twin2.set_ylim(1, 65)
  35. ax.set_xlabel(xlabel)
  36. ax.set_ylabel(ylabel1)
  37. #twin1.set_ylabel(ylabel2)
  38. #twin2.set_ylabel(ylabel3)
  39. ax.yaxis.label.set_color(p1.get_color())
  40. twin1.yaxis.label.set_color(p2.get_color())
  41. twin2.yaxis.label.set_color(p3.get_color())
  42. tkw = dict(size=4, width=1.5)
  43. ax.tick_params(axis='y', colors=p1.get_color(), **tkw)
  44. twin1.tick_params(axis='y', colors=p2.get_color(), **tkw)
  45. #twin2.tick_params(axis='y', colors=p3.get_color(), **tkw)
  46. #ax.tick_params(axis='x', **tkw)
  47. ax.legend(handles=[p1, p2, p3])
  48. #ax.legend(handles=[p1, p2])
  49. plt.show()
  50. plt.close()
  51. def plot_scatter(xList, yList, color, scale, title, xLabel, yLabel, filePath, fileName, cbar_label=None, xLimit=100, yLimit=1):
  52. '''分散點圖, 顏色有漸層效果, 僅1個子圖, xLabel and yLabel must be string'''
  53. #有cmap
  54. plt.style.use('dark_background')
  55. fig, ax = plt.subplots(figsize=(10, 6)) #plt.subplots是開出總畫布
  56. plt.title(title, fontsize = 14)
  57. #等同於 fig = plt.figure()
  58. #ax = plt.subplot(111)
  59. #ax = plt.subplots()
  60. #給子圖一個名稱後面設定給color
  61. sc = ax.scatter(xList, yList, c=color, s=scale, cmap='bwr', alpha=0.7)
  62. #設定X軸與Y軸字體大小
  63. ax.set_xlabel(xLabel, fontsize = 12)
  64. ax.set_ylabel(yLabel, fontsize = 12)
  65. #设置坐标轴刻度
  66. my_x_ticks = np.arange(0, (xLimit+(xLimit/10)), (xLimit/10))
  67. my_y_ticks = np.arange(0, (yLimit+(yLimit/10)), (yLimit/10))
  68. #plt.xticks(my_x_ticks)
  69. plt.yticks(my_y_ticks)
  70. #设置刻度字体大小
  71. plt.xticks(fontsize = 12)
  72. plt.yticks(fontsize = 12)
  73. #設置格線
  74. ax.grid(True)
  75. #有子圖時需將子圖設定給colorbar當對象
  76. cbar = plt.colorbar(sc)
  77. cbar.set_label(cbar_label,fontsize=12)
  78. #设置图例
  79. #plt.legend(loc = 'best', fontsize = 12)
  80. #圖表過度集中可以使用.tight_layout分開
  81. plt.tight_layout()
  82. #儲存圖檔
  83. pc.create_path(filePath)
  84. plt.savefig(os.path.join(filePath, fileName))
  85. #显示图片
  86. #plt.show()
  87. plt.close()
  88. def plot_single_scatter(xList, yList, scale, title, xLabel, yLabel, filePath, fileName, color='skyblue', xLimit=100, yLimit=1):
  89. '''畫單一顏色的分散點圖,僅1個子圖'''
  90. #沒有cmap
  91. #xLabel and yLabel must be word
  92. plt.style.use('dark_background')
  93. fig, ax = plt.subplots(figsize=(13.66, 7.68)) #plt.subplots是開出總畫布
  94. plt.title(title, fontsize=18)
  95. #給子圖一個名稱後面設定給color
  96. sc = ax.scatter(xList, yList, c=color, s=scale, alpha=0.7)
  97. #設定X軸與Y軸字體大小
  98. ax.set_xlabel(xLabel, fontsize=18)
  99. ax.set_ylabel(yLabel, fontsize=18)
  100. #设置坐标轴范围
  101. #plt.xlim((-5, 5)) #也可写成plt.xlim(-5, 5)
  102. plt.ylim((0, yLimit+yLimit/10)) #也可写成plt.ylim(-4, 4)
  103. #设置坐标轴刻度
  104. my_x_ticks = np.arange(0, (xLimit+(xLimit/10)), (xLimit/10))
  105. my_y_ticks = np.arange(0, (yLimit+(yLimit/10)), (yLimit/10))
  106. #plt.xticks(my_x_ticks)
  107. #plt.yticks(my_y_ticks)
  108. #设置刻度字体大小
  109. plt.xticks(fontsize=12)
  110. plt.yticks(fontsize=12)
  111. #設置格線
  112. ax.grid(True)
  113. #设置图例
  114. #plt.legend(loc='best', fontsize=12)
  115. #圖表過度集中可以使用.tight_layout分開
  116. plt.tight_layout()
  117. #儲存圖檔
  118. pc.create_path(filePath)
  119. plt.savefig(os.path.join(filePath, fileName))
  120. #显示图片
  121. #plt.show()
  122. plt.close()
  123. def plot_multi_scatter(xList, yList, scale, title, xLabel, yLabel, savePath, saveName, legendIllustration=None):
  124. ''' 分散點圖, 依據axNum的數字決定子圖的數量,
  125. 圖的數量有2, 5, 6 三種, 會呈現在同一個畫布上
  126. '''
  127. # 決定子圖的數量
  128. axNum = len(xList)
  129. # 設置畫布背景顏色
  130. plt.style.use('dark_background')
  131. plt.figure(figsize=(13.66, 7.68))
  132. colorList = pc.color_degrees()
  133. if scale is None:
  134. scale = pc.create_lists(0, len(xList), [])
  135. for i in range(len(xList)):
  136. scale[i] = pc.create_lists(0, len(xList[i]), 50)
  137. if legendIllustration is None:
  138. legendIllustration = [num for num in range(len(xList))]
  139. axes = []
  140. for i in range(axNum):
  141. locals()['ax%s' %i] = plt.scatter(xList[i], yList[i], s=scale[i], color=colorList[i], label=legendIllustration[i], alpha=0.7)
  142. axes.append(locals()['ax%s' %i])
  143. plt.legend(axes, legendIllustration, loc='best', edgecolor='w', prop={'size':10})
  144. #設定title, x, y軸標籤
  145. plt.title(title, fontsize=18)
  146. plt.xlabel(xLabel, fontsize=18)
  147. plt.ylabel(yLabel, fontsize=18)
  148. #设置坐标轴范围
  149. #plt.xlim((-5, 5)) #也可写成plt.xlim(-5, 5)
  150. plt.ylim(0, 1) #也可写成plt.ylim(-4, 4)
  151. #設置格線, 格線為虛線, minor為次要格線
  152. plt.grid(True)
  153. pc.create_path(savePath)
  154. plt.savefig(os.path.join(savePath, saveName))
  155. #plt.show()
  156. plt.close()
  157. def plot_line(yList, title, savePath, saveFile, xList=None):
  158. ''' 折線圖, 僅有一個子圖'''
  159. pc.create_path(savePath)
  160. if xList is None:
  161. xList = [i+1 for i in range(len(yList))]
  162. #plt.style.use('dark_background')
  163. #設置畫布大小
  164. plt.figure(figsize=(13.66, 7.68))
  165. # 標示x軸(labelpad代表與圖片的距離)
  166. plt.xlabel('quantity of answers', fontsize=18, labelpad=10)
  167. # 標示y軸(labelpad代表與圖片的距離)
  168. plt.ylabel('elo points', fontsize=18, labelpad=10)
  169. # 標示title
  170. plt.title(title, fontsize=18)
  171. #设置坐标轴范围
  172. #plt.xlim((-5, 5)) #也可写成plt.xlim(-5, 5)
  173. plt.ylim((-3,3)) #也可写成plt.ylim(-4, 4)
  174. #设置坐标轴刻度
  175. #my_x_ticks = np.arange(-5, 5, 0.5)
  176. my_y_ticks = np.arange(-3, 4, 0.5)
  177. #plt.xticks(my_x_ticks)
  178. #plt.yticks(my_y_ticks)
  179. #設置格線
  180. plt.grid(True) #格線為虛線minor為次要格線
  181. #plt.xticks(fontsize=15)
  182. #plt.yticks(fontsize=15)
  183. #plt.plot(xList, yList, 's-', color='g', label='Elo estimating')
  184. plt.plot(xList, yList, color='r', label='Elo estimating')
  185. # 顯示出線條標記位置
  186. plt.legend(loc='best', fontsize=18)
  187. plt.savefig(os.path.join(savePath, saveFile))
  188. #plt.show()
  189. plt.close()
  190. def plot_box(data, savePath, saveName, title=None, xLabel=None, yLabel=None, labels=None):
  191. '''盒鬚圖 僅有1個子圖'''
  192. plt.figure(figsize=(13.66, 7.68))
  193. pc.create_path(savePath)
  194. outPath = os.path.join(savePath, saveName)
  195. plt.boxplot(data, labels = labels)
  196. plt.title(title)
  197. plt.xlabel(xLabel)
  198. plt.ylabel(yLabel)
  199. plt.savefig(outPath)
  200. #plt.show()
  201. plt.close()
  202. def plot_distribution(data, distribution, savePath, saveName):
  203. '''用EDA檢查data的資料分布型態'''
  204. pc.create_path(savePath)
  205. fig = plt.figure()
  206. scipy.stats.probplot(data, dist=distribution, sparams=(5,), fit=True, plot=plt)
  207. plt.savefig(os.path.join(savePath, saveName))
  208. #plt.show()
  209. plt.close()
  210. #print("%s completed!" %distribution)
  211. def plot_multi_subplots(data, plotType, savePath, saveName, subs=(3,3), **kwargs):
  212. '''用EDA檢查data的資料分布型態
  213. 將多種分布呈現現在同一圖中
  214. '''
  215. m, n = subs[0], subs[1]
  216. pc.create_path(savePath)
  217. fig, axes = plt.subplots(m, n)
  218. #plt.figure(figsize=(13.66, 7.68))
  219. i = 0
  220. for row in range(m):
  221. for column in range(n):
  222. print("第{}幅畫".format(i+1))
  223. #plt.text(0.5, 0.5, str(dist), fontsize=8, ha='bottom')
  224. if plotType == 'probplot':
  225. axes[row, column] = scipy.stats.probplot(data, dist=data[i], sparams=(5,), fit=True, plot=plt)
  226. elif plotType == 'line':
  227. axes[row, column] = plt.plot([(j+1) for j in range(len(data[i]))], data[i], color='r', label='Elo estimating')
  228. i += 1
  229. del data
  230. gc.collect()
  231. #fig.subplots_adjust(hspace=0.8, wspace=0.5) #設定子圖的間隔
  232. saveFile = os.path.join(savePath, saveName)
  233. print("繪圖中請稍候...")
  234. #plt.show()
  235. plt.savefig(saveFile)
  236. plt.close()
  237. print("completed!")
  238. def plot_hist(data, savePath, saveName):
  239. '''人數長條圖'''
  240. bins = [i for i in range(0, 300+1, 1)]
  241. plt.hist(data, bins = bins)
  242. plt.savefig(os.path.join(savePath, saveName))
  243. #plt.show() #會讓圖檔儲存無效
  244. plt.close()