import matplotlib. pyplot as plt
input_txt = 'New_Text_Document_pro.txt'
f = open ( input_txt)
x = [ ]
y = [ ]
for line in f:
line = line. strip( '\n' )
line = line. split( ' ' )
x. append( '%03d' % int ( line[ 0 ] . split( ':' ) [ 1 ] ) )
y. append( float ( line[ 2 ] . split( '=' ) [ 1 ] . replace( '%' , '' ) ) )
f. close
plt. plot( x, y, marker= 'o' , label= 'lost plot' )
plt. xticks( x[ 0 : len ( x) : 10 ] , x[ 0 : len ( x) : 10 ] , rotation= 45 )
plt. margins( 0 )
plt. xlabel( "train step" )
plt. ylabel( "Accuracy" )
plt. title( "matplotlip plot" )
plt. tick_params( axis= "both" )
plt. savefig( "Epoch_Accuracy.jpg" )
plt. show( )
以下是整理封装好的折线图画法:
import matplotlib. pyplot as plt
def x_y_data ( txt_path) :
f = open ( txt_path)
content = f. readlines( )
x = [ ]
y_loss = [ ]
y_acc = [ ]
for i in range ( 0 , 10 ) :
line = content[ i] . strip( '\n' )
line = content[ i] . split( ' ' )
x. append( '%03d' % int ( line[ 0 ] . split( ':' ) [ 1 ] ) )
y_loss. append( float ( line[ 1 ] . split( ':' ) [ 1 ] ) )
y_acc. append( float ( line[ 2 ] . split( '=' ) [ 1 ] . replace( '%' , '' ) ) )
f. close
return x, y_loss, y_acc
class make_picture ( object ) :
def __init__ ( self, tips) :
self. tips = tips
def loss_pic ( self, x, y_loss) :
plt. plot( x, y_loss, marker= 'o' , label= 'lost plot' )
plt. xticks( x[ 0 : len ( x) : 10 ] , x[ 0 : len ( x) : 10 ] , rotation= 45 )
plt. margins( 0 )
plt. xlabel( "Epoch" )
plt. ylabel( "avg_loss" )
plt. title( "matplotlip plot" )
plt. tick_params( axis= "both" )
plt. savefig( "Epoch_Loss.jpg" )
plt. show( )
print ( self. tips)
def acc_pic ( self, x, y_acc) :
plt. plot( x, y_acc, marker= 'o' , label= 'lost plot' )
plt. xticks( x[ 0 : len ( x) : 10 ] , x[ 0 : len ( x) : 10 ] , rotation= 45 )
plt. margins( 0 )
plt. xlabel( "Epoch" )
plt. ylabel( "Accuracy" )
plt. title( "matplotlip plot" )
plt. tick_params( axis= "both" )
plt. savefig( "Epoch_Accuracy.jpg" )
plt. show( )
print ( self. tips)
def acc_pic2 ( self, x, y_acc) :
x_label = range ( 0 , 201 , 10 )
y_label = range ( 0 , 101 , 10 )
override = {
'fontsize' : '13' ,
'verticalalignment' : 'center' ,
'horizontalalignment' : 'center' , }
plt. plot( x, y_acc)
plt. xticks( x_label, rotation= 45 )
plt. xlim( 0 , 210 )
plt. yticks( y_label)
plt. xlabel( "Epoch" , override)
plt. ylabel( "Accuracy(%)" )
plt. tick_params( axis= "both" )
plt. savefig( "Epoch_Accuracy.jpg" )
plt. show( )
print ( self. tips)
if __name__ == '__main__' :
txt_path = 'New_Text_Document_pro.txt'
tips = 'The picture has done!'
x, y_loss, y_acc = x_y_data( txt_path)
PIC = make_picture( tips)
PIC. loss_pic( x, y_loss)
PIC. acc_pic( x, y_acc)
以下是整理封装好的曲线图画法:
import matplotlib. pyplot as plt
import numpy as np
from scipy. interpolate import spline
import json, time
def test ( ) :
a = [ 2 , 3 , 5 , 6 , 7 ]
for i in a:
num = np. sum ( list ( map ( lambda x: x <= i, a) ) )
for i in np. arange( 0 , 0.51 , 0.05 ) :
num = '%.2f' % i
b = np. array( [ 1 , 2 , 3 , 4 , 5 ] )
print ( 'b lenth: ' , len ( b) , b[ 0 ] )
def percentage ( txt_path, json_path) :
f = open ( txt_path)
lines = f. readlines( )
distanse_list = [ ]
for line in lines:
distanse_list. append( float ( line. split( ',' ) [ 0 ] ) )
distanse_array = np. array( distanse_list)
per_result = { }
for i in np. arange( 0 , 0.51 , 0.05 ) :
x_index = float ( '%.2f' % i)
num = np. sum ( list ( map ( lambda x: x <= x_index, distanse_list) ) )
per = num/ len ( distanse_list)
per_result[ x_index] = per
json_str = json. dumps( per_result)
with open ( json_path, 'w' ) as json_file:
json_file. write( json_str)
def curve_pic ( json_path, save_path) :
f = open ( json_path)
x_list = [ ]
per_list = [ ]
per_result = json. load( f)
for k_x, v_per in per_result. items( ) :
x_list. append( k_x)
per_list. append( v_per)
x_data = np. array( list ( map ( float , x_list) ) )
y_data = np. array( list ( map ( float , per_list) ) ) * 100
y_ticks = np. arange( 0 , 101 , 10 )
xnew = np. linspace( x_data. min ( ) , x_data. max ( ) , 300 )
power_smooth = spline( x_data, y_data, xnew)
x_new_data = [ ]
x_data = list ( x_data)
for i in range ( len ( x_data) ) :
if i == 0 :
x_new_data. append( 0 )
else :
x_new_data. append( x_data[ i] )
plt. plot( xnew, power_smooth)
plt. xticks( x_new_data[ 0 : ] , x_new_data[ 0 : ] )
plt. yticks( y_ticks[ 0 : ] , y_ticks[ 0 : ] )
plt. xlim( 0 , 0.5 )
plt. ylim( 0 , 100 )
plt. title( 'PCK total' )
plt. xlabel( 'Normalized distance' )
plt. ylabel( 'Detection rate(%)' )
plt. savefig( save_path)
plt. show( )
def per_range ( txt_path) :
f = open ( txt_path)
lines = f. readlines( )
distanse_list = [ ]
for line in lines:
distanse_list. append( float ( line. split( ',' ) [ 0 ] ) )
num_1 = 0
num_2 = 0
num_3 = 0
num_4 = 0
num_5 = 0
for i in distanse_list:
if i <= 0.1 :
num_1 += 1
if i <= 0.2 :
num_2 += 1
if i <= 0.3 :
num_3 += 1
if i <= 0.4 :
num_4 += 1
if i <= 0.5 :
num_5 += 1
total = len ( distanse_list)
per_1 = num_1 / total
per_2 = num_2 / total
per_3 = num_3 / total
per_4 = num_4 / total
per_5 = num_5 / total
print ( '%.5f' % per_1)
print ( '%.5f' % per_2)
print ( '%.5f' % per_3)
print ( '%.5f' % per_4)
print ( '%.5f' % per_5)
print ( per_1+ per_2+ per_3+ per_4+ per_5)
def per_15_range ( txt_path) :
f = open ( txt_path)
lines = f. readlines( )
dict_1 = { }
dict_2 = { }
dict_3 = { }
dict_4 = { }
dict_5 = { }
dict_15 = { }
for index in range ( 0 , 16 ) :
num = 0
num_1 = 0
num_2 = 0
num_3 = 0
num_4 = 0
num_5 = 0
for line in lines:
rate = float ( line. split( ',' ) [ 0 ] )
id = int ( line. split( ',' ) [ 1 ] )
if id == index:
num += 1
if rate <= 0.1 :
num_1 += 1
if rate <= 0.2 :
num_2 += 1
if rate <= 0.3 :
num_3 += 1
if rate <= 0.4 :
num_4 += 1
if rate <= 0.5 :
num_5 += 1
dict_15[ index] = num
dict_1[ index] = num_1
dict_2[ index] = num_2
dict_3[ index] = num_3
dict_4[ index] = num_4
dict_5[ index] = num_5
per_1_dict = { }
per_2_dict = { }
per_3_dict = { }
per_4_dict = { }
per_5_dict = { }
for k_index, v_number in dict_15. items( ) :
per_1_dict[ k_index] = dict_1[ k_index] / v_number
per_2_dict[ k_index] = dict_2[ k_index] / v_number
per_3_dict[ k_index] = dict_3[ k_index] / v_number
per_4_dict[ k_index] = dict_4[ k_index] / v_number
per_5_dict[ k_index] = dict_5[ k_index] / v_number
print ( 'per_0.1: ' , per_1_dict)
print ( 'per_0.2: ' , per_2_dict)
print ( 'per_0.3: ' , per_3_dict)
print ( 'per_0.4: ' , per_4_dict)
print ( 'per_0.5: ' , per_5_dict)
def json_data ( json_path) :
f = open ( json_path)
json_file = json. load( f)
print ( type ( json_file) )
print ( len ( json_file. keys( ) ) )
print ( len ( json_file. values( ) ) )
print ( json_file. keys( ) )
if __name__ == '__main__' :
txt_path = '/home/jianghusanren/Pictures/FX/4/pckh.txt'
pic_save_path = '/home/jianghusanren/Pictures/FX/4/PCK_total.jpg'
json_path = '/home/jianghusanren/Pictures/FX/4/pckh_dis_per_2.json'
statt = time. time( )
curve_pic( json_path, pic_save_path)
end = time. time( )