目的: 記錄利用python繪制圖片的心得
繪制技巧包括:
1.線圖
2.點線圖
3.線條顏色
4.修改X軸坐標文字
5.添加Y軸的數值
6.繪圖建議配合pandas使用,便於讀取數據
實例數據
refdepth altdepth totaldepth percent1 2608 20 2628 percent2 2609 40 2649 percent3 2611 69 2680 percent4 2615 97 2712 percent5 2610 103 2713 percent10 2610 198 2808 percent20 2640 432 3072 percent30 2654 633 3287 percent40 2671 831 3502 percent50 2680 1091 3771 percent80 2774 1714 4488
繪制代碼
1 import pandas as pd 2 import numpy as np 3 import matplotlib.pyplot as plt 4 import sys 5 6 infile = sys.argv[1] 7 8 df = pd.read_table(infile, index_col=0) 9 x_ticks =['1%', '2%', '3%', '4%', '5%', '10%', '20%', '30%', '40%', '50%', '80%'] # x軸預備展示的文字 11 12 refdepth = df['refdepth'].values 13 altdepth = df['altdepth'].values 14 totaldepth = df['totaldepth'].values 15 16 plt.title('different percent vs reads number') 17 plt.plot(refdepth ,'m.-', color='red', label='refdepth') # 如果沒有指定X軸,默認使用從0開始的連續數字 18 plt.plot(altdepth, 'm.-', color='green', label='altdepth') 19 plt.plot(totaldepth, 'm.-', color='orange', label='totaldepth') # m.- 繪制點線圖 20 plt.xticks(range(0,11), x_ticks, rotation=30) # 用x_ticks的內容填充x軸數值, rotation為旋轉角度. 21 plt.xlabel('diff percent') 22 plt.ylabel('reads depth') 23 24 for x,y in zip(range(0,11), altdepth): # 在每個繪制位點添加其數值 25 plt.text(x,y,y,ha='center',va='bottom',fontsize=8) 26 28 for x,y in zip(range(0,11), refdepth): 29 plt.text(x,y-200,y,ha='center',va='bottom',fontsize=8) 30 31 for x,y in zip(range(0,11), totaldepth): 32 plt.text(x,y,y,ha='center',va='bottom',fontsize=8) 33 34 35 plt.legend() 36 plt.show()
結果展示: