• 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:8_cooperation9_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:8_cooperation9_TCReport医学部数据\%d.png'%(j+1)) # 输出图片
    33     plt.close() # 关闭
    34 print('finished') # 通知完成

  • 相关阅读:
    递归获取指定盘符下的所有文件及文件夹
    单例模式和多线程有没有关系?
    eclipse启动tomcat时设置端口
    dozer转化对象
    枚举
    dubbo
    json
    配网失败问题
    esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx);
    base64编码
  • 原文地址:https://www.cnblogs.com/caicai2019/p/10973696.html
Copyright © 2020-2023  润新知