數據示例
代碼實例
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # 繪制分類散點圖 4 5 import os 6 import argparse 7 import csv 8 import pandas as pd 9 import numpy as np 10 import matplotlib.pyplot as plt 11 12 13 file_path = r'E:\08_cooperation\09_TCReport\醫學部數據\NIPT_PGS_190604.xlsx' # 數據路徑 14 NIPT_PGS = pd.read_excel(file_path,sheet_name='sub_data',header=0,index_col='SeqID') # 讀取數據 15 print(NIPT_PGS.head()) # 查看前6行數據 16 columns = NIPT_PGS.columns # 獲取列標簽(變量名稱),為后續循環使用 17 types = np.unique(NIPT_PGS['Type']) # 獲取分類標簽,為后續分類循環使用 18 print(len(columns)) # 查看列數 19 print(types) # 查看分類 20 print('running...') 21 for j in range(len(columns)): # 對變量按照變量數目進行循環 22 for i in range(len(types)): # 對分類按照分類數目進行循環 23 plt.scatter(NIPT_PGS.loc[NIPT_PGS['Type']==types[i],'Density(um^2)'] 24 ,NIPT_PGS.loc[NIPT_PGS['Type']==types[i],columns[j]] 25 ,s=20 26 ,c=np.array(plt.cm.tab10(i/len(types))).reshape(1,-1) 27 ,label=types[i]) 28 plt.legend() # 展示分類標簽 29 plt.xlabel('Density(um^2)') 30 plt.ylabel(columns[j]) 31 plt.tight_layout() #設置為緊湊型 32 plt.savefig(r'E:\08_cooperation\09_TCReport\醫學部數據\%d.png'%(j+1)) # 輸出圖片 33 plt.close() # 關閉 34 print('finished') # 通知完成