第一次在这个论坛问问题就得到了您的大力帮助,我是matlab新手,以下是我画这个图的函数,其中有些内容可能与没有贴出的部分有关,但是整个思路应该在这个函数中体现出来了,可能有很多不合理的地方,或者有些地方可以用更简单的方法实现,还请各位继续指教,谢谢!
- %% 绘制有因次曲线图 --- Executes on button press in pushbutton3.
- function pushbutton3_Callback(hObject, eventdata, handles)
- % hObject handle to pushbutton3 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- format long g
- if (isnan(handles.current_data) == 1)
- errordlg('没有计算数据,不能绘制图像', '错误');
- return ;
- end
- %% 读取数据
- shy_id = handles.shy_id;
- colname = handles.current_cols;
- data = handles.current_data;
- len = length(colname);
- for index = 1 : len
- if (strcmp(colname(index), 'qVsg1Gu') == 1)
- qVsg1 = data(: , index);
- end
- if (strcmp(colname(index), 'psfGu') == 1)
- psf = data(: , index);
- end
- if (strcmp(colname(index), 'pfGu') == 1)
- pf = data(: , index);
- end
- if (strcmp(colname(index), 'PrGu') == 1)
- Pr = data(: , index);
- end
- if (strcmp(colname(index), 'eta_srGu') == 1)
- eta_sr = data(: , index);
- end
- if (strcmp(colname(index), 'eta_rGu') == 1)
- eta_r = data(: , index);
- end
- if (strcmp(colname(index), 'LAGu') == 1)
- LA = data(: , index);
- end
- if (strcmp(colname(index), 'rho_sg1Gu') == 1)
- rho_sg1Gu = data(: , index);
- end
- if (strcmp(colname(index), 'nGu') == 1)
- nGu = data(: , index);
- end
- end
- %% 画原始点图
- x = qVsg1'; % qVsg1
- f1x_y1 = [psf, pf]'; % psf, pf
- f1x_y2 = Pr';
- f1x_y2 = f1x_y2 ./ 1000;
- f2x_y1 = [eta_sr, eta_r]'; % eta_sr, eta_r
- f2x_y1 = f2x_y1 .* 100;
- f2x_y2 = LA';
- % 拟合曲线
- FX11 = polyfit(x, f1x_y1(1, :), 4);
- FX12 = polyfit(x, f1x_y1(2, :), 4);
- % 根据拟合曲线多项式取2000个点的值用于画曲线图
- X = linspace(min(x), max(x), 2000);
- fx_y11 = polyval(FX11, X);
- fx_y12 = polyval(FX12, X);
- fx_y1 = [fx_y11; fx_y12];
- FX21 = polyfit(x, f1x_y2, 4);
- fx_y2 = polyval(FX21, X);
- scrsz = get(0, 'ScreenSize');
- % 注意figure与其上的各个axis都通过像素点的方式表示位置与大小,这样便于控制各个对象的相对位置
- % figures2 是用GUIDE预先做的一个模板,上面有axes1、axes2、axes3;
- % 其中axes1用来画图的下部分,并且根据axes1的尺寸画出图形的上部分;
- % axes2用来放图片,执行的时候自动调整为图片尺寸;
- % axes3用来标注试验条件,运行的时候也调整为axes2一样的高度
- hfig = figures2('Position', [scrsz(3)/6, scrsz(4)/12, scrsz(3)*2/3, scrsz(4)*5/6]);
- % psf, pf
- [AX1, H11, H12] = plotyy(x, f1x_y1, x, f1x_y2);
- xlabel('q_{Vsg1}(m^3/s)');
- set(H11, 'LineStyle', '.');
- set(H12, 'LineStyle', '.');
- set(H11, 'Marker', '.');
- set(H12, 'Marker', '.');
- hold('all');
- %[AX2, H21, H22] = plotyy(X, fx_y1, X, fx_y2);
- hlines21 = line(X, fx_y1, 'Parent', AX1(1));
- hlines22 = line(X, fx_y2, 'Parent', AX1(2));
- axes(AX1(1));
- text('String', 'p_{sf}', 'Position', [X(800), fx_y11(800)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines21(1), 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
- text('String', 'p_f', 'Position', [X(1000), fx_y12(1000)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines21(2), 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
- axes(AX1(2));
- text('String', 'P_r', 'Position', [X(1200),fx_y2(1200)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines22, 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
- grid on
- % 调整Y轴的YTickLabel信息
- YT = get(AX1(1), 'YTick');
- YT = linspace(min(YT), max(YT), 11);
- YTB = num2cell(YT);
- YTB{length(YTB)} = 'p(Pa)';
- set(AX1(1), 'YTick', YT);
- set(AX1(1), 'YTickLabel', YTB);
- YT = get(AX1(2), 'YTick');
- YT = linspace(min(YT), max(YT), 11);
- YTB = num2cell(YT);
- YTB{length(YTB)} = 'Pr(KW)';
- set(AX1(2), 'YTick', YT);
- set(AX1(2), 'YTickLabel', YTB);
- set(AX1, 'XMinorTick', 'on', 'YMinorTick', 'on', 'Box', 'off', 'YColor', 'k');
- % eta_sr, eta_r
- cfig = get(gcf, 'color');
- pos = get(AX1, 'position');
- pos = [pos{1}(1) pos{1}(2) pos{1}(3) pos{1}(4)/2];
- % 将已画的axes调整为原来的一半,再在其上方位置创建一个新的axes画出另外的数据
- set(AX1, 'position', pos);
- pos2 = [pos(1) pos(2) + pos(4) pos(3) pos(4)];
- % 注意figure与其上的各个axis都通过像素点的方式表示大小,这样便于控制各个对象的相对位置 'Units', 'pixels'
- AX2 = axes('Units', 'pixels', 'Box', 'off', 'Parent', hfig, 'Position', pos2);
- axes(AX2);
- %set(AX2, 'XAxisLocation', 'top', 'XMinorTick', 'on', 'YMinorTick', 'on', 'Box', 'off', 'YColor', 'k', 'XTickLabel', {});
- FX11 = polyfit(x, f2x_y1(1, :), 4);
- FX12 = polyfit(x, f2x_y1(2, :), 4);
- X = linspace(min(x), max(x), 2000);
- fx_y11 = polyval(FX11, X);
- fx_y12 = polyval(FX12, X);
- fx_y1 = [fx_y11; fx_y12];
- FX21 = polyfit(x, f2x_y2, 4);
- fx_y2 = polyval(FX21, X);
- % 因为发现经过拟合后,Y的的数据最大值超过了原值,
- % 如果先用原值plotyy的话,Y轴能表示的最大值不够,导致出错,
- % 所以先判断原值与拟合值那个中有最大值,就先用那个plotyy,
- % 这样后画的那个就不会因为值超出Y轴的表示范围而出错
- if (max(f2x_y2) > max(fx_y2))
- [AX1, H11, H12] = plotyy(x, f2x_y1, x, f2x_y2); % eta_sr, eta_r
- set(H11, 'LineStyle', '.');
- set(H12, 'LineStyle', '.');
- set(H11, 'Marker', '.');
- set(H12, 'Marker', '.');
- hold('all');
- hlines21 = line(X, fx_y1, 'Parent', AX1(1));
- hlines22 = line(X, fx_y2, 'Parent', AX1(2));
- else
- [AX1, H11, H12] = plotyy(X, fx_y1, X, fx_y2); % eta_sr, eta_r
- hold('all');
- hlines21 = line(x, f2x_y1, 'Parent', AX1(1));
- hlines22 = line(x, f2x_y2, 'Parent', AX1(2));
- set(hlines21, 'LineStyle', '.');
- set(hlines22, 'LineStyle', '.');
- set(hlines21, 'Marker', '.');
- set(hlines22, 'Marker', '.');
- end
- axes(AX1(1));
- text('String', '\eta_{sr}', 'Position', [X(800), fx_y11(800)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines21(1), 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
- text('String', '\eta_r', 'Position', [X(1000), fx_y12(1000)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines21(2), 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
- axes(AX1(2));
- text('String', 'L_A', 'Position', [X(1200), fx_y2(1200)], 'HorizontalAlignment', 'right', 'fontsize', 12, 'Color', get(hlines22, 'Color'), 'BackgroundColor', get(AX1(1), 'Color'));
- grid on
- % 调整Y轴的YTickLabel信息
- YT = get(AX1(1), 'YTick');
- YT = linspace(min(YT), max(YT), 11);
- YTB = num2cell(YT);
- YTB{1} = 'p(Pa)';
- YTB{length(YTB)} = 'ηsr,ηr(%)';
- set(AX1(1), 'YTick', YT);
- set(AX1(1), 'YTickLabel', YTB);
- YT = get(AX1(2), 'YTick');
- YT = linspace(min(YT), max(YT), 11);
- YTB = num2cell(YT);
- YTB{1} = 'Pr(KW)';
- YTB{length(YTB)} = 'LA(dB)';
- set(AX1(2), 'YTick', YT);
- set(AX1(2), 'YTickLabel', YTB);
- set(AX1, 'XAxisLocation', 'top', 'XMinorTick', 'off', 'YMinorTick', 'on', 'Box', 'off', 'YColor', 'k', 'XTickLabel', {});
- title('有因次曲线图', 'fontsize', 16);
- imf = imfinfo ('A.bmp'); % 读取图片信息
- hchildren = get(hfig, 'Children');
- for index = 1: length(hchildren)
- if (strcmp(get(hchildren(index), 'Tag'), 'axes2') == 1) % 将图片放在 axes2 上
- X = imread('A.bmp');
- axes(hchildren(index));
- image(X);
- cm = imf.Colormap;
- colormap(cm);
- pos = get(hchildren(index), 'Position');
- pos = [pos(1), pos(2), imf.Width, imf.Height]; % 注意figure与其上的各个axis都通过像素点的方式表示大小,这样便于控制各个对象的相对位置
- set(hchildren(index), 'Position', pos);
- %set(hchildren(index), 'XMinorTick', 'off', 'YMinorTick', 'off', 'Box', 'on', 'XTickLabel', {}, 'YTickLabel', {});
- set(hchildren(index), 'Box', 'on', 'XTick', [], 'YTick', []);
- end
- if (strcmp(get(hchildren(index), 'Tag'), 'axes3') == 1) % 将试验条件放在 axes3 上
- axes(hchildren(index));
- pos = get(hchildren(index), 'Position');
- pos = [pos(1), pos(2), pos(3), imf.Height]; % 设置与图片高度一样,这样与axes2的高度也就一样了,两边高度一致
- set(hchildren(index), 'Position', pos);
- text(0.15, 0.6, ['n = ', num2str(nGu(1)), ' r/min']);
- text(0.15, 0.3, ['\rho = ', num2str(rho_sg1Gu(1)), ' kg/m^3']);
- %set(hchildren(index), 'XMinorTick', 'off', 'YMinorTick', 'off', 'Box', 'on', 'XTickLabel', {}, 'YTickLabel', {});
- set(hchildren(index), 'Box', 'on', 'XTick', [], 'YTick', []);
- end
- end
- pic_filename = strcat(shy_id, '有因次曲线图.emf');
- saveas(hfig, pic_filename, 'emf');
- hold off;
复制代码 |