python学习——利用循环实现分类散点图绘制

数据示例

代码实例

 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') # 通知完成

 

转载于:https://www.cnblogs.com/caicai2019/p/10973696.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值