python畫圖匯總(持續更新)


  1. 折線圖

plt.figure(figsize=(40, 40))            # 確定圖像畫布的大小
plt.subplot(211)                        # 將畫布分為兩行一列

plt.xlabel('Number of sample', fontsize=40)                          # x軸的label
plt.ylabel('Characteristics of the amplitude', fontsize=40)          # y軸的label     備注(plot所有的原件都可以加fontsize屬性)
plt.title('{} characteristics (ml_id=2 waveType=2)'.format(c_type), fontsize=50)     # 圖的title

plt.plot(two_type_list[:two_negative_end_index], linestyle = "-", color = 'r',                # 繪制折線圖,其中若x參數省略,則橫坐標以y列表的索引代替
         label = 'Negative | average: {} variance: {} median: {}'.format(('%.2f' % np.mean(two_type_list[ : two_negative_end_index])),     # label參數表示這條線的label,可以當作圖例顯示出來
                                                                         ('%.2f' % np.var(two_type_list[ : two_negative_end_index])), 
                                                                         ('%.2f' % np.median(two_type_list[ : two_negative_end_index]))), 
        linewidth=3.0)                                                                        # 線寬

plt.plot(two_type_list[two_negative_end_index+1:], linestyle = "-", color = 'g',              # 備注(一張圖可以累積加多個plot)
         label = 'Positive   | average: {} variance: {} median: {}'.format(('%.2f' % np.mean(two_type_list[two_negative_end_index+1 : ])),
                                                                           ('%.2f' % np.var(two_type_list[two_negative_end_index+1 : ])),
                                                                           ('%.2f' % np.median(two_type_list[two_negative_end_index+1 : ]))),
         linewidth=3.0)

# plt.ylim(0, 5)                     # 設置y軸的取值范圍,如設置(0,5)則y軸坐標為從0開始,到5結束
# 刻度值字體大小設置
plt.tick_params(labelsize=40)        # 設置坐標軸上刻度的字體大小
plt.legend(loc=0, fontsize = 40)     # 顯示圖例,loc=0表示圖例會根據圖片情況自動擺放
####################################################################################################################################
plt.subplot(212)

plt.xlabel('Number of sample', fontsize=40)
plt.ylabel('Characteristics of the amplitude', fontsize=40)
plt.title('{} characteristics (ml_id=6 waveType=2)'.format(c_type), fontsize=50)

plt.plot(six_type_list[:six_negative_end_index], linestyle = "-", color = 'r', 
         label = 'Negative | average: {} variance: {} median: {}'.format(('%.2f' % np.mean(six_type_list[ : six_negative_end_index])), 
                                                                         ('%.2f' % np.var(six_type_list[ : six_negative_end_index])), 
                                                                         ('%.2f' % np.median(six_type_list[ : six_negative_end_index]))), 
         linewidth=3.0)

plt.plot(six_type_list[six_negative_end_index+1:], linestyle = "-", color = 'g', 
         label = 'Positive   | average: {} variance: {} median: {}'.format(('%.2f' % np.mean(six_type_list[six_negative_end_index+1 : ])),
                                                                           ('%.2f' % np.var(six_type_list[six_negative_end_index+1 : ])),
                                                                           ('%.2f' % np.median(six_type_list[six_negative_end_index+1 : ]))),
         linewidth=3.0)

# 刻度值字體大小設置
plt.tick_params(labelsize=40)
plt.legend(loc=0, fontsize = 40)

plt.savefig('C:/Users/Mloong/Desktop/f_image/{} characteristics.png'.format(c_type), dpi=300)
plt.show()

2.散點圖

  

_type = 'median'

plt.scatter(range(0, 3790), two_avgAbs_list[0:3790], c='r')              # 散點圖的x參數不可省略
plt.scatter(range(3791, 4939), two_avgAbs_list[3791:4939], c='g')

plt.title('{} ml_id=2 waveType=2'.format(_type))

plt.savefig('C:/Users/Mloong/Desktop/f_image/{} scatter ml_id=2 waveType=2.png'.format(_type), dpi=300)
plt.show()

  3.概率分布圖

# 概率分布圖

type_list = two_median_list
_type = 'median'


num_bins = 100            # 條狀圖的個數

plt.hist(type_list[:3790], num_bins, normed=1, facecolor='blue', alpha=0.5)
plt.hist(type_list[3791:], num_bins, normed=1, facecolor='red', alpha=0.5)

plt.xlabel('Value')
plt.ylabel('Probability')
plt.title('{} probability distribution ml_id=2 waveType=2'.format(_type))

plt.subplots_adjust(left=0.15)
plt.savefig('C:/Users/Mloong/Desktop/f_image/{} probability distribution ml_id=2 waveType=2.png'.format(_type), dpi=300)
plt.show()

  4.箱形圖

_type = 'pca_value'

import seaborn as sns

plt.subplot(121)

plt.title('{} (ml_id=2 waveType=2)'.format(_type))
sns.set(style='whitegrid')         # 設置背景
sns.boxplot(x='label', y='{}'.format(_type), data=two_data, hue='label')    # data參數是一個dataframe對象,其中x和y分別時這個dataframe中的列名

#########################################################################################
plt.subplot(122)

plt.title('{} (ml_id=6 waveType=2)'.format(_type))
sns.set(style='whitegrid')         # 設置背景
sns.boxplot(x='label', y='{}'.format(_type), data=six_data, hue='label')    # 繪制箱形圖

plt.savefig('C:/Users/Mloong/Desktop/f_image/{} box figure.png'.format(_type), dpi=300)
plt.show()

  5.熱圖

# 2.相關矩陣
import seaborn as sns
corrmat = two_data[['avs', 'avgAbs', 'rms', 'rms2', 'wave', 'pulse', 'PeekFlag', 
                    'Margin', 'Skewness', 'Kurtosis', 'median', 'pca_value', 'label']].corr()       # .corr()求相關矩陣,此時返回的值corrmat為相關矩陣
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(corrmat, vmax=.8, square=True)                                                          # 將這個相關矩陣以熱圖的形式畫出來
plt.savefig('C:/Users/Mloong/Desktop/f_image/two correlation matrix.png', dpi=300)
plt.show()

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM