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()
文件格式
自己按照这个格式随便造一个就好。