科研畫圖:散點連接並平滑(基於Matlab和Python)


導師要求參照別人論文中的圖(下圖),將其論文中的圖畫美觀些,網上關於科研畫圖相關的代碼比較少,就自己鼓搗了下。

 附上自己整合驗證過的代碼:

功能:將散點連接並平滑

1)Matlab

效果圖:

x1=[431.50032,759.5552,1335.3736,2530.388] %輸入以下三組數據
y1=[34.06366,35.73132,37.2244,38.61294]
x2=[263.8656,458.7952,839.6584,1740.9088]
y2=[33.5318074,35.1415668,36.8603528,38.244926]
x3=[253.91296,441.854,803.4116,1625.2548]
y3=[34.3625,35.88912,37.5403,38.45364]
a=linspace(min(x1),max(x1)); %插值后將散點連線平滑化
b=interp1(x1,y1,a,'cubic');
c=linspace(min(x2),max(x2));
d=interp1(x2,y2,c,'cubic');
e=linspace(min(x3),max(x3));
f=interp1(x3,y3,e,'cubic');
plot(a,b, 'LineWidth',2, 'LineSmoothing', 'on'); %畫ab對應曲線,粗細,平滑
hold on
plot(c,d, 'LineWidth',2, 'LineSmoothing', 'on'); %畫cd對應曲線,粗細,平滑
hold on
plot(e,f, 'LineWidth',2, 'LineSmoothing', 'on'); %畫ef對應曲線,粗細,平滑
axis([0,3000,33,39])  %確定x軸與y軸框圖大小
legend({'MRMV','MVDM','MVLL'},'FontSize',13,'Location','southeast','Orientation','vertical') %題注設置:名稱,字號,位置,方向
xlabel('Bit rates(kbps)','FontSize',13,'FontWeight','bold') %x軸設置:標題,字號,字體粗細
ylabel('PSNR(dB)','FontSize',13,'FontWeight','bold') %y軸設置:名稱,字號,字體粗細
title('Balloons','FontSize',15,'FontWeight','bold') %標題描述,名稱,字號,字體粗細
set(gca,'ygrid','on','gridlinestyle','--','Gridalpha',0.3) %網格設置
grid on; %網格
print(gcf, '-dpng', '-r800', 'C:\Users\Administrator\Desktop\test.png') %保存圖片,格式為png,分辨率800,保存路徑

2)Python

小問題:翹尾問題需要解決 

# author: Kobay time:2019/10/18
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import spline
x1 = np.array([431.50032,759.5552,1335.3736,2530.388])
y1 = np.array([34.06366,35.73132,37.2244,38.61294])
x2 = np.array([263.8656,458.7952,839.6584,1740.9088])
y2 = np.array([33.5318074,35.1415668,36.8603528,38.244926])
x3 = np.array([253.91296,441.854,803.4116,1625.2548])
y3 = np.array([34.3625,35.88912,37.5403,38.45364])
x1_new = np.linspace(x1.min(), x1.max())  # 300 represents number of points to make between T.min and T.max
y1_smooth = spline(x1, y1, x1_new)
x2_new = np.linspace(x2.min(), x2.max(), 3000)  # 300 represents number of points to make between T.min and T.max
y2_smooth = spline(x2, y2, x2_new)
x3_new = np.linspace(x3.min(), x3.max(), 3000)  # 300 represents number of points to make between T.min and T.max
y3_smooth = spline(x3, y3, x3_new)
# 散點圖
plt.scatter(x1, y1, c='black', alpha=0.5)  # alpha:透明度) c:顏色
# 折線圖
plt.plot(x1, y1, linewidth=1)  # 線寬linewidth=1matl
# 平滑后的折線圖
plt.plot(x1_new, y1_smooth, c='blue',label='MRMV')
plt.plot(x2_new, y2_smooth, c='orange',label='MVDM')
plt.plot(x3_new, y3_smooth, c='gray',label='MVLL')
# 解決中文顯示問題
# plt.rcParams['font.sans-serif'] = ['SimHei']  # SimHei黑體
# plt.rcParams['axes.unicode_minus'] = False
plt.title("Balloons", fontdict={'family' : 'Calibri', 'size': 16,'weight':'bold'})  # 標題及字號
plt.xlabel("Bit rates(kbps)", fontdict={'family' : 'Calibri', 'size': 14,'weight':'bold'})  # X軸標題及字號
plt.ylabel("PSNR(dB)", fontdict={'family' : 'Calibri', 'size': 14,'weight':'bold'})  # Y軸標題及字號
plt.tick_params(axis='both', labelsize=14)  # 刻度大小
plt.axis([0, 3000, 33, 39])#設置坐標軸的取值范圍
plt.grid(linestyle='-.')
plt.legend(loc=4)
plt.show()
# plt.save('squares_plot.png'(文件名), bbox_inches='tight'(將圖表多余的空白部分剪掉))
# 用它替換plt.show實現自動保存圖表

 碼字不易,如果您覺得有幫助,麻煩點個贊再走唄~

 


免責聲明!

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



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