When running imagesc, how can I extend the image coverage on my graph?

4 vues (au cours des 30 derniers jours)
Kristine
Kristine le 24 Fév 2025
Réponse apportée : Umar le 24 Fév 2025
As Seen in the image shared, the first plot has the full background filled in while the second has a slived of the background filled. Is it possible to make it extend all the way across the plot?
t = tiledlayout(1,2);
nexttile;
imagesc(station1.TemperatureITS90DegC,station1.DepthsaltWaterMLat,station1.TemperatureITS90DegC)
hold on
plot(samplestn1.HgpM,samplestn1.Depth,".-",'linewidth',2, 'color', 'k', 'MarkerSize',15)
hold off
title('Station 1')
% Stn1AxisLimits = axis % show axis limits based on data
axis ([0 6 0 25])
% Graph 2
nexttile;
imagesc(station2.TemperatureITS90DegC,station2.DepthsaltWaterMLat,station2.TemperatureITS90DegC)
hold on
plot(samplestn2.HgpM,samplestn2.Depth,".-",'linewidth',2, 'color', 'k', 'MarkerSize',15)
title('Station 2')
axis ([0 5 0 100])
hcb=colorbar;
hcb.Title.String = "Temperature (C)";
hold off

Réponse acceptée

Walter Roberson
Walter Roberson le 24 Fév 2025
imagesc(station2.TemperatureITS90DegC,station2.DepthsaltWaterMLat,station2.TemperatureITS90DegC)
When you call imagesc() like that, the image extends from
station2.TemperatureITS90DegC(1) to station2.TemperatureITS90DegC(end) in the X coordinate (horizontal axes), and
station2.DepthsaltWaterMLat(1) to station2.DepthsaltWaterMLat(end) in the Y coordinate (vertical axes)
If those values are less than the axis limits axis ([0 5 0 100]) then the image produced will only paint part of the axis.
If you want to have the image extend across the entire area [0 5] [0 100] then
imagesc([0 5], [0 100], station2.TemperatureITS90DegC)
Well... except the locations you pass to imagesc() are the coordinates of the centers of the outside pixels. If you want the image to be entirely inside [0 5], [0 100] then you need to tweek the coordinates slightly

Plus de réponses (1)

Umar
Umar le 24 Fév 2025

Hi @Kristine,

I do agree with @Walter Robertson’s comments. So, after reviewing your comments, I will ensure that the background image displayed by `imagesc` extends fully across the axes in your MATLAB plots, you need to adjust both the axis limits and the way you specify the data boundaries in your `imagesc` calls. Here’s a step-by-step guide to achieving this:

1. Setting Axis Limits: Make sure that the axis limits (`axis`) are appropriately set to encompass the range of your data. This means that if your temperature data for `station1` spans from 0 to 6 on the x-axis and from 0 to 25 on the y-axis, you should ensure that these values accurately reflect your data.

2. Specifying XData and YData: When using `imagesc`, it’s crucial to specify the `XData` and `YData` parameters correctly so that they align with your data dimensions. For example, if your temperature matrix is of size `m x n`, you should define `x` and `y` as follows:

     x = linspace(min_x_value, max_x_value, 
     size(station1.TemperatureITS90DegC, 2));
     y = linspace(min_y_value, max_y_value,    
    size(station1.TemperatureITS90DegC, 1));

This will ensure that each pixel in your image corresponds correctly to its position on the axes.

3. Using imagesc with XData and YData: When calling `imagesc`, use:

   imagesc('XData', x, 'YData', y, 'CData', station1.TemperatureITS90DegC);

This will allow MATLAB to place your image according to the specified coordinates, thus ensuring it fills the entire axes area.

4. Adjusting Color Limits (clims): If you want to control how data values map into colors in your colormap, consider specifying color limits using the `clims` parameter:

   clims = [min_value max_value]; % Set according to your temperature data 
   range
   imagesc('XData', x, 'YData', y, 'CData', station1.TemperatureITS90DegC, 
   'CLim', clims);

5. Final Example: Here’s how your code might look after these adjustments:

   t = tiledlayout(1,2);
   % Station 1
   nexttile;
   x1 = linspace(0, 6, size(station1.TemperatureITS90DegC, 2));
   y1 = linspace(0, 25, size(station1.TemperatureITS90DegC, 1));
   imagesc('XData', x1, 'YData', y1, 'CData', station1.TemperatureITS90DegC);
   hold on;
   plot(samplestn1.HgpM,
    samplestn1.Depth,".-",'linewidth',2,'color','k','MarkerSize',15);
   hold off;
   title('Station 1');
   axis([0 6 0 25]);
   % Station 2
   nexttile;
   x2 = linspace(0, 5, size(station2.TemperatureITS90DegC, 2));
   y2 = linspace(0, 100, size(station2.TemperatureITS90DegC, 1));
   imagesc('XData', x2, 'YData', y2, 'CData', station2.TemperatureITS90DegC);
   hold on;
   plot(samplestn2.HgpM,
   samplestn2.Depth,".-",'linewidth',2,'color','k','MarkerSize',15);
   title('Station 2');
   axis([0 5 0 100]);
   hcb=colorbar;
   hcb.Title.String = "Temperature (°C)";
   hold off;

Here are some additional insights that will help you further.

Understanding Pixel Mapping: The way pixels are mapped can significantly affect how well your images fill their designated spaces. Ensure that your temperature matrices are sized appropriately relative to their corresponding depth and temperature ranges.

Colormap Adjustment: If your color mapping does not represent the temperature data effectively due to outliers or specific ranges of interest in your data set, consider adjusting the colormap or applying a custom colormap using `colormap()` after plotting.

By following these steps and adjusting your plotting commands accordingly, you should be able to extend the image coverage across both plots effectively.

Hope this helps.

Catégories

En savoir plus sur Color and Styling dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by