溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python散點(diǎn)圖與折線圖怎么繪制

發(fā)布時(shí)間:2021-05-19 10:59:33 來(lái)源:億速云 閱讀:246 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)Python散點(diǎn)圖與折線圖怎么繪制,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

在數(shù)據(jù)分析的過程中,經(jīng)常需要將數(shù)據(jù)可視化,目前常使用的:散點(diǎn)圖 折線圖

需要import的外部包 一個(gè)是繪圖 一個(gè)是字體導(dǎo)入

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

在數(shù)據(jù)處理前需要獲取數(shù)據(jù),從TXT XML csv excel 等文本中獲取需要的數(shù)據(jù),保存到list

def GetFeatureList(full_path_file):
  file_name = full_path_file.split('\\')[-1][0:4]
  # print(file_name)
  # print(full_name)
  K0_list = []
  Area_list = []
  all_lines = []
  f = open(full_path_file,'r')
  all_lines = f.readlines()
  lines_num = len(all_lines)
  # 數(shù)據(jù)清洗
  if lines_num > 5000:
    for i in range(3,lines_num-1):
      temp_k0 = int(all_lines[i].split('\t')[1])
      if temp_k0 == 0:
        K0_list.append(ComputK0(all_lines[i]))
      else:
        K0_list.append(temp_k0)
      Area_list.append(float(all_lines[i].split('\t')[15]))
    # K0_Scatter(K0_list,Area_list,file_name)
  else:
    print('{} 該樣本量少于5000'.format(file_name))
  return K0_list, Area_list,file_name

繪制兩組數(shù)據(jù)的散點(diǎn)圖,同時(shí)繪制兩個(gè)散點(diǎn)圖,上下分布在同一個(gè)圖片中

def K0_Scatter(K0_list, area_list, pic_name):
  plt.figure(figsize=(25, 10), dpi=300)
  # 導(dǎo)入中文字體,及字體大小
  zhfont = FontProperties(fname='C:/Windows/Fonts/simsun.ttc', size=16)
  ax = plt.subplot(211)
  # print(K0_list)
  ax.scatter(range(len(K0_list)), K0_list, c='r', marker='o')
  plt.title(u'散點(diǎn)圖', fontproperties=zhfont)
  plt.xlabel('Sampling point', fontproperties=zhfont)
  plt.ylabel('K0_value', fontproperties=zhfont)
  ax = plt.subplot(212)
  ax.scatter(range(len(area_list)), area_list, c='b', marker='o')
  plt.xlabel('Sampling point', fontproperties=zhfont)
  plt.ylabel(u'大小', fontproperties=zhfont)
  plt.title(u'散點(diǎn)圖', fontproperties=zhfont)
  # imgname = 'E:\\' + pic_name + '.png'
  # plt.savefig(imgname, bbox_inches = 'tight')
  plt.show()

散點(diǎn)圖顯示

Python散點(diǎn)圖與折線圖怎么繪制

繪制一個(gè)折線圖 每個(gè)數(shù)據(jù)增加標(biāo)簽

def K0_Plot(X_label, Y_label, pic_name):
  plt.figure(figsize=(25, 10), dpi=300)
  # 導(dǎo)入中文字體,及字體大小
  zhfont = FontProperties(fname='C:/Windows/Fonts/simsun.ttc', size=16)
  ax = plt.subplot(111)
  # print(K0_list)
  ax.plot(X_label, Y_label, c='r', marker='o')
  plt.title(pic_name, fontproperties=zhfont)
  plt.xlabel('coal_name', fontproperties=zhfont)
  plt.ylabel(pic_name, fontproperties=zhfont)
  # ax.xaxis.grid(True, which='major')
  ax.yaxis.grid(True, which='major')
  for a, b in zip(X_label, Y_label):
    str_label = a + str(b) + '%'
    plt.text(a, b, str_label, ha='center', va='bottom', fontsize=10)
  imgname = 'E:\\' + pic_name + '.png'
  plt.savefig(imgname, bbox_inches = 'tight')
  # plt.show()

Python散點(diǎn)圖與折線圖怎么繪制

繪制多條折線圖

def K0_MultPlot(dis_name, dis_lsit, pic_name):
  plt.figure(figsize=(80, 10), dpi=300)
  # 導(dǎo)入中文字體,及字體大小
  zhfont = FontProperties(fname='C:/Windows/Fonts/simsun.ttc', size=16)
  ax = plt.subplot(111)
  X_label = range(len(dis_lsit[1]))
  p1 = ax.plot(X_label, dis_lsit[1], c='r', marker='o',label='Euclidean Distance')
  p2 = ax.plot(X_label, dis_lsit[2], c='b', marker='o',label='Manhattan Distance')
  p3 = ax.plot(X_label, dis_lsit[4], c='y', marker='o',label='Chebyshev Distance')
  p4 = ax.plot(X_label, dis_lsit[5], c='g', marker='o',label='weighted Minkowski Distance')
  plt.legend()
  plt.title(pic_name, fontproperties=zhfont)
  plt.xlabel('coal_name', fontproperties=zhfont)
  plt.ylabel(pic_name, fontproperties=zhfont)
  # ax.xaxis.grid(True, which='major')
  ax.yaxis.grid(True, which='major')
  for a, b,c in zip(X_label, dis_lsit[5],dis_name):
    str_label = c + '_'+ str(b)
    plt.text(a, b, str_label, ha='center', va='bottom', fontsize=5)
  imgname = 'E:\\' + pic_name + '.png'
  plt.savefig(imgname,bbox_inches = 'tight')
  # plt.show()

Python散點(diǎn)圖與折線圖怎么繪制

圖形顯示還有許多小技巧,使得可視化效果更好,比如坐標(biāo)軸刻度的定制,網(wǎng)格化等

python主要應(yīng)用領(lǐng)域有哪些

1、云計(jì)算,典型應(yīng)用OpenStack。2、WEB前端開發(fā),眾多大型網(wǎng)站均為Python開發(fā)。3.人工智能應(yīng)用,基于大數(shù)據(jù)分析和深度學(xué)習(xí)而發(fā)展出來(lái)的人工智能本質(zhì)上已經(jīng)無(wú)法離開python。4、系統(tǒng)運(yùn)維工程項(xiàng)目,自動(dòng)化運(yùn)維的標(biāo)配就是python+Django/flask。5、金融理財(cái)分析,量化交易,金融分析。6、大數(shù)據(jù)分析。

關(guān)于“Python散點(diǎn)圖與折線圖怎么繪制”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI