Images in MATLAB®

How to make Image plots in MATLAB® with Plotly.


Display Image of Matrix Data

Create matrix C. Display an image of the data in C. Add a colorbar to the graph to show the current colormap.

C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
image(C)
colorbar

fig2plotly(gcf);

By default, the CDataMapping property for the image is set to 'direct' so image interprets values in C as indices into the colormap. For example, the bottom right pixel corresponding to the last element in C, 22, uses the 22nd color of the colormap.

Scale the values to the full range of the current colormap by setting the CDataMapping property to 'scaled' when creating the image.

C = [0 2 4 6; 8 10 12 14; 16 18 20 22];

image(C,'CDataMapping','scaled')
colorbar

fig2plotly(gcf);

Alternatively, you can use the imagesc function to scale the values instead of using image(C,'CDataMapping','scaled'). For example, use imagesc(C).

Control Image Placement

Place the image so that it lies between 5 and 8 on the x-axis and between 3 and 6 on the y-axis.

x = [5 8];
y = [3 6];
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
image(x,y,C)

fig2plotly(gcf);

Notice that the pixel corresponding to C(1,1) is centered over the point (5,3). The pixel corresponding to C(3,4) is centered over the point (8,6). image positions and orients the rest of the image between those two points.

Display Image of 3-D Array of True Colors

Create C as a 3-D array of true colors. Use only red colors by setting the last two pages of the array to zeros.

C = zeros(3,3,3);
C(:,:,1) = [.1 .2 .3; .4 .5 .6; .7 .8 .9];

Display an image of the data in C.

C = zeros(3,3,3);
C(:,:,1) = [.1 .2 .3; .4 .5 .6; .7 .8 .9];

image(C)

fig2plotly(gcf);

Modify Image After Creation

Plot a line, and then create an image on top of the line. Return the image object.

plot(1:3)
hold on
C = [1 2 3; 4 5 6; 7 8 9];
im = image(C);

fig2plotly(gcf);

Make the image semitransparent so that the line shows through the image.

plot(1:3)
hold on
C = [1 2 3; 4 5 6; 7 8 9];
im = image(C);

im.AlphaData = 0.5;

fig2plotly(gcf);

Read and Display JPEG Image File

Read a JPEG image file.

C = imread('ngc6543a.jpg');

imread returns a 650-by-600-by-3 array, C.

Display the image.

C = imread('ngc6543a.jpg');

image(C)

fig2plotly(gcf);

Add Image to Axes in 3-D View

Create a surface plot. Then, add an image under the surface. image displays the image in the xy-plane.

Z = 10 + peaks;
surf(Z)
hold on 
image(Z,'CDataMapping','scaled')

fig2plotly(gcf);