Python 画图 合图 调用美化风格

Python 画图 合图 调用美化风格

直接将代码贴出,自取。
将多张折线图画到同一张图中
让你使用Python代码画出R中ggplot的图片格式,摆脱原生的丑丑风格。

先让你看下效果图吧!

效果图

在这里插入图片描述

代码

跟据自己文件随便调下代码吧!

# -*- coding: utf-8 -*-
"""
Created on Thu Oct 17 09:55:27 2020

@author: dujidan
"""

import matplotlib.pyplot as plt
import sys
import os


plt.style.use('ggplot') #使用'ggplot'风格美化显示的图表


input_file = sys.argv[1]
out_path = sys.argv[2]

#创建文件夹
if not os.path.exists(out_path):
    os.makedirs(out_path)


with open(input_file) as f_input:
    data_raw = f_input.read().strip().split('\n')

chr_list = []
T_list = []
N_list = []
for info in data_raw:
    if info.startswith('chr'):
        chr_list.append(info.strip())
    elif info.startswith('T:'):
        T_list.append(info.strip())
    elif info.startswith('N:'):
        N_list.append(info.strip())

T_list_int = []
for t in T_list:
   t_int=[int(i) for i in t.split(' ')[1:]]
   T_list_int.append(t_int)
   
N_list_int = []
for n in N_list:
   n_int=[int(j) for j in n.split(' ')[1:]]
   N_list_int.append(n_int)
X = range(1,101)


#多图合并
plt.figure(figsize=(30,30))

for num in range(len(T_list_int)):
    T_list_int_num = T_list_int[num]
    N_list_int_num = N_list_int[num]
    title = chr_list[num].replace(' ','_').replace(':','_').split('_')[0] +\
            chr_list[num].replace(' ','_').replace(':','_').split('_')[1]
    #site_name = out_path  + '/'  +title + '.png'
    
    plt.subplot(4,4,num+1)
    plt.title(title) 
    plt.plot(X, N_list_int_num, marker='.', label='N')
    plt.plot(X, T_list_int_num, marker='.', label='T')
    
    plt.legend()  # 让图例生效
    plt.xlabel('dis_distributed') #X轴标签
    plt.ylabel("dis_num") #Y轴标签
plt.suptitle(out_path,fontsize=60)   
plt.savefig(out_path  + '.png', dpi=500)
plt.show()

#------------------------------ 
#另附
#单张图输出

for num in range(len(T_list_int)):
    T_list_int_num = T_list_int[num]
    N_list_int_num = N_list_int[num]
    title = chr_list[num].replace(' ','_').replace(':','_')
    out_name = out_path  + '/'  +title + '.png'
    
    plt.figure()
    plt.title(title) 
    plt.plot(X, N_list_int_num, marker='.', label='N')
    plt.plot(X, T_list_int_num, marker='.', label='T')
    
    plt.legend()  # 让图例生效
    plt.xlabel('dis_distributed') #X轴标签
    plt.ylabel("dis_num") #Y轴标签
    
    plt.savefig(out_name, dpi=1000)
    plt.show()

文件格式

自己按照这个格式随便造一个就好。
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值