matlab读取txt数据绘图(python命令行传参)
(1)命令行實(shí)現(xiàn)高斯分布
一:綜述
Python唯一支持的參數(shù)傳遞方式是『共享傳參』(call by sharing)多數(shù)面向?qū)ο笳Z言都采用這一模式,包括Ruby、Smalltalk和Java(Java的引用類型是這樣,基本類型按值傳遞)共享傳參是指函數(shù)的各個(gè)形式參數(shù)獲得實(shí)參中各個(gè)引用的副本;也就是說,函數(shù)內(nèi)部的形參是實(shí)參的別名(alias)這種方案的結(jié)果是,函數(shù)可能會修改作為參數(shù)傳入的可變對象,但是無法修改那些對象的標(biāo)識(即不能把一個(gè)對象替換為另一個(gè)對象)
二:所需函數(shù)介紹
從一個(gè)均勻分布[low,high)中隨機(jī)采樣,注意定義域是左閉右開,即包含low,不包含high.
該函數(shù)返回兩個(gè)值. opts 和args
opts 是一個(gè)存有所有選項(xiàng)及其輸入值的元組.當(dāng)輸入確定后,這個(gè)值不能被修改了.
args 是去除有用的輸入以后剩余的部分.
rand_data:數(shù)據(jù)
bins:直方圖繪圖的列數(shù)
density=True:把數(shù)據(jù)轉(zhuǎn)為密度直方圖
count : 數(shù)組或數(shù)組列表百分比
ignored : 列表 或者列表的列表 圖形對象
三:python代碼
1.需求分析
該代碼主要通過命令行傳參的方式實(shí)現(xiàn)高斯模型,將圖像和數(shù)據(jù)導(dǎo)入本地txt文件
2.代碼
# 導(dǎo)入必要的庫 import numpy as np import pandas as pd import matplotlib.pylab as plt import sys import getopt from scipy import stats #import emmu=1 sigma = 3 # 標(biāo)準(zhǔn)差為3 num = 10000 # 個(gè)數(shù)為10000argc = len(sys.argv) opts, args = getopt.getopt(sys.argv[1:], "hdp", ["help", "data=", "png="]) if argc>1:#np.random.seed(100) # 固定隨機(jī)數(shù)種子,確保下次運(yùn)行數(shù)據(jù)相同for opt, arg in opts:if opt in ("-h", "--help"):print('請輸入正確的參數(shù)\n''-d 代表生成高斯分布數(shù)據(jù)保存到本地文件\n''-p 代表畫出高斯分布圖\n''如python test04.py -p\n''python test04.py -d')sys.exit()elif opt in ("-d", "--data"):#username = arg#mu = eval(arg) # 男生#sigma = 3 # 標(biāo)準(zhǔn)差為3#num = 10000 # 個(gè)數(shù)為10000rand_data = np.random.normal(mu, sigma, num)count, bins, ignored = plt.hist(rand_data, 30, density=True)# 前面省略,從下面直奔主題:data = 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu) ** 2 / (2 * sigma ** 2))file = open('results_storage.txt', 'a')# i=0#保存到本地txt文件for i in range(len(data)):# s = str(bins[i]).replace('[',").replace('[',")+'\t'+str(data[i]).replace('[',").replace('[',")#去除[],這兩行按數(shù)據(jù)不同,可以選擇s = str(bins[i]).replace('[', ").replace('[',") + ' ' + str(data[i]).replace('[', ").replace('[',")s = s.replace("'", ").replace(',',") + '\n' # 去除單引號,逗號,每行末尾追加換行符file.write(s)file.close()elif opt in ("-p", "--png"):#password = arg#mu = eval(arg)rand_data = np.random.normal(mu, sigma, num)count, bins, ignored = plt.hist(rand_data, 30, density=True)plt.plot(bins, 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu) ** 2 / (2 * sigma ** 2)),linewidth=2, color='r')plt.show()# arg1 = sys.argv[1]else:print('運(yùn)行錯(cuò)誤!請輸入正確的參數(shù)\n''如python test04.py -p \n''python test03.py -d ')#python test04.py -p四:結(jié)果顯示
1.參數(shù)不足
python test04.py 運(yùn)行錯(cuò)誤!請輸入正確的參數(shù) 如python test04.py -p python test03.py -d2.請求幫助
python test04.py --help 請輸入正確的參數(shù) -d 代表生成高斯分布數(shù)據(jù)保存到本地文件 -p 代表畫出高斯分布圖 如python test04.py -p python test04.py -d3.生成圖像
python test04.py -p4.生成數(shù)據(jù)保存
python test04.py -d(2)matlab讀取數(shù)據(jù)驗(yàn)證
一:需求分析
將python生成的數(shù)據(jù)保存到results_storage.txt,利用matlab讀取數(shù)據(jù)繪圖。
二:matlab代碼
matlab_data.m
results_storage.txt
clc clear date=load('G:\python\GMM\results_storage.txt'); x=date(:,1); y=date(:,2); plot(x,y,'r'); xlabel('x');ylabel('y'); %axis([0 80 0 30]); hold on三:結(jié)果顯示
與python畫的圖像一致
與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的matlab读取txt数据绘图(python命令行传参)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下系统函数open,read,
- 下一篇: collect2: error: ld