【问题标题】:Setting axis limit for plot设置绘图的轴限制
【发布时间】:2018-07-22 19:45:49
【问题描述】:

我想从0 to 325 设置此图中 X 轴的限制。当我使用xlim 设置限制时(在代码中注释)。它不能正常工作。当我使用xlim 时,情节的整个结构都会发生变化。任何帮助将不胜感激。

figure
imagesc(transpose(all_area_for_visual));
colormap("jet")
colorbar('Ticks',0:3,'TickLabels',{'Home ','Field','Bad house','Good house'})
xlabel('Time (min)')
tickLocs = round(linspace(1,length(final_plot_mat_missing_part(2:end,1)),8));
timeVector = final_plot_mat_missing_part(2:end,1);
timeForTicks = (timeVector(tickLocs))./60;
xticks(tickLocs);
xticklabels(timeForTicks);
%xlim([0 325]);
ylabel('Car identity')
yticks(1:length(Ucolumnnames_fpm))
yticklabels([Ucolumnnames_fpm(1,:)])

【问题讨论】:

    标签: matlab plot graph matlab-figure axis


    【解决方案1】:

    如果我猜对了,您只想绘制all_area_for_visual 中的部分数据,由tickLocs 的条件给出。因此,您应该首先对数据进行条件化,然后对其进行绘图:

    % generate the vector of x values:
    tickLocs = round(linspace(1,length(final_plot_mat_missing_part(2:end,1)),8));
    % create an index vector (of logicals) that marks the columns to plot from the data matix:
    validX = tickLocs(tickLocs<=325);
    % plot only the relevant part of the data:
    imagesc(transpose(all_area_for_visual(:,validX)));
    % generate the correct ticks for the data that was plotted:
    timeVector = final_plot_mat_missing_part(2:end,1);
    timeForTicks = (timeVector(tickLocs(validX)))./60;
    xticks(tickLocs(validX));
    % here you continue with setting the labels, colormap and so on...
    

    【讨论】:

      【解决方案2】:

      imagesc 默认将数据放在以整数1:width1:height 为中心的小矩形中。您可以通过在调用中添加两个向量来指定每个数据点的 x 和 y 位置:

      imagesc(x,y,transpose(all_area_for_visual));
      

      其中xy 是您要放置数据的沿x 轴和y 轴位置的向量。

      请注意,xlimxticks 不会更改数据的位置,只会更改显示的轴区域以及沿轴的刻度线位置。使用xticklabels,您可以更改每个刻度线处显示的内容,因此您可以使用它来“伪造”数据位置,但xlim 设置仍然适用于实际位置,而不是分配给刻度线的标签。

      我认为在正确的位置开始绘制数据更容易。这是一个例子:

      % Fake your data, I'm making a small matrix here for illustration purposes
      all_area_for_visual = min(floor(cumsum(rand(20,5)/2)),3);
      times = linspace(0,500,20);      % These are the locations along the time axis for each matrix element
      car_id_names = [4,5,8,15,18];    % These are the labels to put along the y-axis
      car_ids = 1:numel(car_id_names); % These are the locations to use along the y-axis
      
      % Replicate your plot
      figure
      imagesc(times,car_ids,transpose(all_area_for_visual));
      %        ^^^    ^^^   NOTE! specifying locations
      colormap("jet")
      colorbar('Ticks',0:3,'TickLabels',{'Home ','Field','Bad house','Good house'})
      xlabel('Time (min)')
      ylabel('Car identity')
      set(gca,'YTick',car_ids,'YTickLabel',car_id_names) % Combine YTICK and YTICKLABEL calls
      
      % Now you can specify your limit, in actual time units (min)
      xlim([0 325]);
      

      【讨论】:

      • 伦戈。对不起,我不明白。在这种情况下,我只想将 X 轴的限制更改为 325。
      • @Venkat:我已经添加了一个示例代码,我希望这会让它更清楚。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 2013-07-09
      • 2013-10-05
      • 2020-10-19
      • 2014-06-09
      • 2021-02-15
      相关资源
      最近更新 更多