声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: wgwhl

[绘图技巧] 如何把一个图片放在plot的图片上

[复制链接]
 楼主| 发表于 2010-8-4 01:32 | 显示全部楼层

回复 15楼 ChaChing 的帖子

第一次在这个论坛问问题就得到了您的大力帮助,我是matlab新手,以下是我画这个图的函数,其中有些内容可能与没有贴出的部分有关,但是整个思路应该在这个函数中体现出来了,可能有很多不合理的地方,或者有些地方可以用更简单的方法实现,还请各位继续指教,谢谢!

  1. %% 绘制有因次曲线图 --- Executes on button press in pushbutton3.
  2. function pushbutton3_Callback(hObject, eventdata, handles)
  3. % hObject handle to pushbutton3 (see GCBO)
  4. % eventdata reserved - to be defined in a future version of MATLAB
  5. % handles structure with handles and user data (see GUIDATA)
  6. format long g

  7. if (isnan(handles.current_data) == 1)
  8. errordlg('没有计算数据,不能绘制图像', '错误');
  9. return ;
  10. end
  11. %% 读取数据
  12. shy_id = handles.shy_id;
  13. colname = handles.current_cols;
  14. data = handles.current_data;
  15. len = length(colname);
  16. for index = 1 : len
  17. if (strcmp(colname(index), 'qVsg1Gu') == 1)
  18. qVsg1 = data(: , index);
  19. end
  20. if (strcmp(colname(index), 'psfGu') == 1)
  21. psf = data(: , index);
  22. end
  23. if (strcmp(colname(index), 'pfGu') == 1)
  24. pf = data(: , index);
  25. end
  26. if (strcmp(colname(index), 'PrGu') == 1)
  27. Pr = data(: , index);
  28. end
  29. if (strcmp(colname(index), 'eta_srGu') == 1)
  30. eta_sr = data(: , index);
  31. end
  32. if (strcmp(colname(index), 'eta_rGu') == 1)
  33. eta_r = data(: , index);
  34. end
  35. if (strcmp(colname(index), 'LAGu') == 1)
  36. LA = data(: , index);
  37. end
  38. if (strcmp(colname(index), 'rho_sg1Gu') == 1)
  39. rho_sg1Gu = data(: , index);
  40. end
  41. if (strcmp(colname(index), 'nGu') == 1)
  42. nGu = data(: , index);
  43. end
  44. end
  45. %% 画原始点图
  46. x = qVsg1'; % qVsg1
  47. f1x_y1 = [psf, pf]'; % psf, pf
  48. f1x_y2 = Pr';
  49. f1x_y2 = f1x_y2 ./ 1000;
  50. f2x_y1 = [eta_sr, eta_r]'; % eta_sr, eta_r
  51. f2x_y1 = f2x_y1 .* 100;
  52. f2x_y2 = LA';
  53. % 拟合曲线
  54. FX11 = polyfit(x, f1x_y1(1, :), 4);
  55. FX12 = polyfit(x, f1x_y1(2, :), 4);
  56. % 根据拟合曲线多项式取2000个点的值用于画曲线图
  57. X = linspace(min(x), max(x), 2000);
  58. fx_y11 = polyval(FX11, X);
  59. fx_y12 = polyval(FX12, X);
  60. fx_y1 = [fx_y11; fx_y12];

  61. FX21 = polyfit(x, f1x_y2, 4);
  62. fx_y2 = polyval(FX21, X);

  63. scrsz = get(0, 'ScreenSize');
  64. % 注意figure与其上的各个axis都通过像素点的方式表示位置与大小,这样便于控制各个对象的相对位置
  65. % figures2 是用GUIDE预先做的一个模板,上面有axes1、axes2、axes3;
  66. % 其中axes1用来画图的下部分,并且根据axes1的尺寸画出图形的上部分;
  67. % axes2用来放图片,执行的时候自动调整为图片尺寸;
  68. % axes3用来标注试验条件,运行的时候也调整为axes2一样的高度
  69. hfig = figures2('Position', [scrsz(3)/6, scrsz(4)/12, scrsz(3)*2/3, scrsz(4)*5/6]);

  70. % psf, pf
  71. [AX1, H11, H12] = plotyy(x, f1x_y1, x, f1x_y2);
  72. xlabel('q_{Vsg1}(m^3/s)');
  73. set(H11, 'LineStyle', '.');
  74. set(H12, 'LineStyle', '.');
  75. set(H11, 'Marker', '.');
  76. set(H12, 'Marker', '.');

  77. hold('all');

  78. %[AX2, H21, H22] = plotyy(X, fx_y1, X, fx_y2);
  79. hlines21 = line(X, fx_y1, 'Parent', AX1(1));
  80. hlines22 = line(X, fx_y2, 'Parent', AX1(2));

  81. axes(AX1(1));
  82. text('String', 'p_{sf}', 'Position', [X(800), fx_y11(800)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines21(1), 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
  83. text('String', 'p_f', 'Position', [X(1000), fx_y12(1000)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines21(2), 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
  84. axes(AX1(2));
  85. text('String', 'P_r', 'Position', [X(1200),fx_y2(1200)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines22, 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));

  86. grid on
  87. % 调整Y轴的YTickLabel信息
  88. YT = get(AX1(1), 'YTick');
  89. YT = linspace(min(YT), max(YT), 11);
  90. YTB = num2cell(YT);
  91. YTB{length(YTB)} = 'p(Pa)';
  92. set(AX1(1), 'YTick', YT);
  93. set(AX1(1), 'YTickLabel', YTB);

  94. YT = get(AX1(2), 'YTick');
  95. YT = linspace(min(YT), max(YT), 11);
  96. YTB = num2cell(YT);
  97. YTB{length(YTB)} = 'Pr(KW)';
  98. set(AX1(2), 'YTick', YT);
  99. set(AX1(2), 'YTickLabel', YTB);

  100. set(AX1, 'XMinorTick', 'on', 'YMinorTick', 'on', 'Box', 'off', 'YColor', 'k');

  101. % eta_sr, eta_r
  102. cfig = get(gcf, 'color');
  103. pos = get(AX1, 'position');
  104. pos = [pos{1}(1) pos{1}(2) pos{1}(3) pos{1}(4)/2];
  105. % 将已画的axes调整为原来的一半,再在其上方位置创建一个新的axes画出另外的数据
  106. set(AX1, 'position', pos);

  107. pos2 = [pos(1) pos(2) + pos(4) pos(3) pos(4)];
  108. % 注意figure与其上的各个axis都通过像素点的方式表示大小,这样便于控制各个对象的相对位置 'Units', 'pixels'
  109. AX2 = axes('Units', 'pixels', 'Box', 'off', 'Parent', hfig, 'Position', pos2);
  110. axes(AX2);
  111. %set(AX2, 'XAxisLocation', 'top', 'XMinorTick', 'on', 'YMinorTick', 'on', 'Box', 'off', 'YColor', 'k', 'XTickLabel', {});

  112. FX11 = polyfit(x, f2x_y1(1, :), 4);
  113. FX12 = polyfit(x, f2x_y1(2, :), 4);
  114. X = linspace(min(x), max(x), 2000);
  115. fx_y11 = polyval(FX11, X);
  116. fx_y12 = polyval(FX12, X);
  117. fx_y1 = [fx_y11; fx_y12];

  118. FX21 = polyfit(x, f2x_y2, 4);
  119. fx_y2 = polyval(FX21, X);
  120. % 因为发现经过拟合后,Y的的数据最大值超过了原值,
  121. % 如果先用原值plotyy的话,Y轴能表示的最大值不够,导致出错,
  122. % 所以先判断原值与拟合值那个中有最大值,就先用那个plotyy,
  123. % 这样后画的那个就不会因为值超出Y轴的表示范围而出错
  124. if (max(f2x_y2) > max(fx_y2))
  125. [AX1, H11, H12] = plotyy(x, f2x_y1, x, f2x_y2); % eta_sr, eta_r
  126. set(H11, 'LineStyle', '.');
  127. set(H12, 'LineStyle', '.');
  128. set(H11, 'Marker', '.');
  129. set(H12, 'Marker', '.');
  130. hold('all');
  131. hlines21 = line(X, fx_y1, 'Parent', AX1(1));
  132. hlines22 = line(X, fx_y2, 'Parent', AX1(2));
  133. else
  134. [AX1, H11, H12] = plotyy(X, fx_y1, X, fx_y2); % eta_sr, eta_r
  135. hold('all');
  136. hlines21 = line(x, f2x_y1, 'Parent', AX1(1));
  137. hlines22 = line(x, f2x_y2, 'Parent', AX1(2));
  138. set(hlines21, 'LineStyle', '.');
  139. set(hlines22, 'LineStyle', '.');
  140. set(hlines21, 'Marker', '.');
  141. set(hlines22, 'Marker', '.');
  142. end

  143. axes(AX1(1));
  144. text('String', '\eta_{sr}', 'Position', [X(800), fx_y11(800)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines21(1), 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
  145. text('String', '\eta_r', 'Position', [X(1000), fx_y12(1000)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines21(2), 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
  146. axes(AX1(2));
  147. text('String', 'L_A', 'Position', [X(1200), fx_y2(1200)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines22, 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));

  148. grid on
  149. % 调整Y轴的YTickLabel信息
  150. YT = get(AX1(1), 'YTick');
  151. YT = linspace(min(YT), max(YT), 11);
  152. YTB = num2cell(YT);
  153. YTB{1} = 'p(Pa)';
  154. YTB{length(YTB)} = 'ηsr,ηr(%)';
  155. set(AX1(1), 'YTick', YT);
  156. set(AX1(1), 'YTickLabel', YTB);

  157. YT = get(AX1(2), 'YTick');
  158. YT = linspace(min(YT), max(YT), 11);
  159. YTB = num2cell(YT);
  160. YTB{1} = 'Pr(KW)';
  161. YTB{length(YTB)} = 'LA(dB)';
  162. set(AX1(2), 'YTick', YT);
  163. set(AX1(2), 'YTickLabel', YTB);

  164. set(AX1, 'XAxisLocation', 'top', 'XMinorTick', 'off', 'YMinorTick', 'on', 'Box', 'off', 'YColor', 'k', 'XTickLabel', {});
  165. title('有因次曲线图', 'fontsize', 16);

  166. imf = imfinfo ('A.bmp'); % 读取图片信息
  167. hchildren = get(hfig, 'Children');
  168. for index = 1: length(hchildren)
  169. if (strcmp(get(hchildren(index), 'Tag'), 'axes2') == 1) % 将图片放在 axes2 上
  170. X = imread('A.bmp');
  171. axes(hchildren(index));
  172. image(X);
  173. cm = imf.Colormap;
  174. colormap(cm);
  175. pos = get(hchildren(index), 'Position');
  176. pos = [pos(1), pos(2), imf.Width, imf.Height]; % 注意figure与其上的各个axis都通过像素点的方式表示大小,这样便于控制各个对象的相对位置
  177. set(hchildren(index), 'Position', pos);
  178. %set(hchildren(index), 'XMinorTick', 'off', 'YMinorTick', 'off', 'Box', 'on', 'XTickLabel', {}, 'YTickLabel', {});
  179. set(hchildren(index), 'Box', 'on', 'XTick', [], 'YTick', []);
  180. end
  181. if (strcmp(get(hchildren(index), 'Tag'), 'axes3') == 1) % 将试验条件放在 axes3 上
  182. axes(hchildren(index));
  183. pos = get(hchildren(index), 'Position');
  184. pos = [pos(1), pos(2), pos(3), imf.Height]; % 设置与图片高度一样,这样与axes2的高度也就一样了,两边高度一致
  185. set(hchildren(index), 'Position', pos);
  186. text(0.15, 0.6, ['n = ', num2str(nGu(1)), ' r/min']);
  187. text(0.15, 0.3, ['\rho = ', num2str(rho_sg1Gu(1)), ' kg/m^3']);
  188. %set(hchildren(index), 'XMinorTick', 'off', 'YMinorTick', 'off', 'Box', 'on', 'XTickLabel', {}, 'YTickLabel', {});
  189. set(hchildren(index), 'Box', 'on', 'XTick', [], 'YTick', []);
  190. end
  191. end
  192. pic_filename = strcat(shy_id, '有因次曲线图.emf');
  193. saveas(hfig, pic_filename, 'emf');

  194. hold off;
复制代码

评分

1

查看全部评分

回复 支持 反对
分享到:

使用道具 举报

发表于 2010-8-4 08:06 | 显示全部楼层
...我是matlab新手...

先鼓励下, 现没时间细看, 晚上再来学习!

个人以为应不只!:@P
若是新学, LZ编程水平基础不低喔

[ 本帖最后由 ChaChing 于 2010-8-4 08:09 编辑 ]
 楼主| 发表于 2010-8-4 08:26 | 显示全部楼层
说实在的,也就7月份开始项目以来学习matlab的,呵呵,属于现学现卖的那种
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-6-8 15:16 , Processed in 0.085342 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表