我有一個從檔案匯入資料并創建指數擬合的 Matlab 腳本。我希望腳本使用相同的顏色繪制資料點和擬合。圖例應該是資料檔案的名稱。當我為另一個輸入檔案再次運行相同的腳本時,我希望新資料和適合顯示在添加到圖例中的新資料檔案的相同繪圖視窗名稱中。
到目前為止我所擁有的是這個;
expfit = fit(x,y,'exp1')
figure(1)
hold all
plot(x,y,'*','DisplayName','fileName)
legend('Interpreter','none')
legend show
使用
plot(expfit,x,y,'DisplayName','fileName')
不作業
如何使用與資料相同的顏色將擬合線添加到每個資料并將檔案名添加到圖例?
uj5u.com熱心網友回復:
你很近...
一個簡單的方法是把顏色放在前面
c = parula(7); % default colour map with 7 colours
figure(1); clf;
hold on
% filename is a variable e.g. filename = 'blah.csv'; from where the data was loaded
plot( x, y, '*', 'DisplayName', filename, 'color', c(1,:) );
plot( expfit, x, y, '-', 'HandleVisibility', 'off', 'color', c(1,:) );
legend('show');
注意通過設定HandleVisibility關閉擬合將不會顯示在圖例中。這是個人喜好,我只喜歡資料和擬合的一個條目,您也可以使用DisplayName第二個情節。
兩個圖都使用相同的顏色使用color輸入。我已經為兩者都使用了顏色圖的第一行,如果你繪制了一些其他資料,你可以使用第二行等。
另一種方法是讓 MATLAB 自動確定顏色并為第二個圖復制顏色,您需要為p第一個圖創建一個變數 ( ) 以獲得顏色
p = plot( x, y, '*', 'DisplayName', filename );
plot( expfit, x, y, '-', 'HandleVisibility', 'off', 'color', p.Color );
其他代碼將是相同的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/459013.html
