首页 文章

图形没有在MATLAB的子图中正确绘制

提问于
浏览
1

我有 data file 有3列,第1列是数据的字段,或者你可以说不同的索引 . 第2列是x轴数据,第3列是y轴数据 . 现在我有不同变量的类似数据文件,如8个文件 . 我想在MATLAB中将所有图形绘制在一个图中 . 对于我的问题,我只显示一个子图 . 该子图数据文件应为5个索引(第1列)绘制5 "line plots" . 但是当我将其绘制为子图时,它只显示了1个图 . 这是我在下面的代码:

% open Zdiff Odd Mode data file
fid = fopen('Data_test.txt');
% Read data in from csv file
readData = textscan(fid,'%f %f %f','Headerlines',1,'Delimiter',',');
fclose(fid);

% Extract data from readData
index_Data = readData{1,1}(:,1);
% Identify the unique indices
uni_idx=unique(index_Data);
xData = readData{1,2}(:,1);
yData = readData{1,3}(:,1);


% Plot Data
f = figure;
%Top Title for all the subplots
p = uipanel('Parent',f,'BorderType','none'); 
p.Title = 'Electrical Characteristics'; 
p.TitlePosition = 'centertop'; 
p.FontSize = 14;
p.FontWeight = 'bold';

cla; hold on; grid on, box on;

ax1 = subplot(2,4,1,'Parent',p);
% Loop over the indices to plot the corresponding data
for i=1:length(uni_idx)
   idx=find(index_Data == uni_idx(i));
   plot(xData(idx,1),yData(idx,1))
end

情节结果如下:
enter image description here

当我将数据绘制为完整的数字时,情节是完美的 . 但是由于我有很多数据要在一个图中作为子图绘制,我需要知道我的子图代码中有什么问题 .

Here is my code for the whole figure of the data without the subplot

在绘制代码之前,它与以前相同:

% Plot Data
f1 = figure(1);
cla; hold on; grid on;

% Loop over the indices to plot the corresponding data

for i=1:length(uni_idx)
   idx=find(index_Data == uni_idx(i));
   plot(xData(idx,1),yData(idx,1))
end

结果如下:
enter image description here

我在子图中的绘图代码有什么问题?谁能帮我吗?

1 回答

  • 1

    这是您的命令序列,以及它们的作用:

    f = figure;
    

    创建一个空图,此处尚未定义轴 .

    cla
    

    清除当前轴,因为没有当前轴,它会创建一个轴 .

    hold on
    

    设置当前轴上的“保持”属性

    grid on, box on
    

    设置当前轴的一些其他属性

    ax1 = subplot(2,4,1,'Parent',p);
    

    创建新轴 . 因为它覆盖了先前创建的轴,所以将其删除 .

    plot(xData(idx,1),yData(idx,1))
    

    绘制当前轴(即由 subplot 创建的曲线) . 这些轴没有设置"hold"属性,因此后续的 plot 命令将覆盖此处绘制的数据 .

    解决方案suggested by Ander in a comment用于设置 subplot 创建的轴的"hold"属性 . 更换:

    cla; hold on; grid on, box on;
    ax1 = subplot(2,4,1,'Parent',p);
    

    有:

    ax1 = subplot(2,4,1,'Parent',p);
    hold on; grid on, box on;
    

    (请注意, cla 不是必需的,因为你正在绘制一个新的空图) .

相关问题