0

Suppose that we have this array and pcolor function in MATLAB:

jj = [0,1,0;1,0,1;1,1,0];
pcolor(jj);

Plot:

Enter image description here

I want show all elements of jj matrix in this pcolor plot so we should have a 3*3 plot. How can I do this in MATLAB?

Anyway, to show only row and column numbers in a plot not above vertical and horizontal scaling? Is there any other recommended suitable type of plots to show this type of data (2D binary data)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eghbal
  • 3,892
  • 13
  • 51
  • 112
  • 3
    Why not use `imshow` or `imagesc`? – Suever Sep 18 '16 at 17:17
  • Thank you for comment. Please add your answer for questions. I checked `imagesc`. It worked. What's your idea about two other questions? – Eghbal Sep 18 '16 at 17:20
  • 1
    I don't understand the question about row/column numbers. Can you show a diagram of what you mean? – Suever Sep 18 '16 at 17:21
  • I don't want this scaling in horizontal and vertical axes. I only want number of rows and columns. for instance I want only number 1 instead of 1 to 2 scaling (in middle). – Eghbal Sep 18 '16 at 17:23
  • + anyway two show lines between boxes in `imagesc` (such as above plot)? – Eghbal Sep 18 '16 at 17:28
  • 1
    Have a look [here](http://stackoverflow.com/questions/39516274/how-to-plot-with-specific-colors-the-values-of-a-matrix-in-matlab/39521450#39521450) for another option to use `imagesc` to visualize your data. – EBH Sep 18 '16 at 21:02

1 Answers1

3

If you want all of the image data to be displayed with pcolor, you need to pad the input with an extra row and column prior to printing since underneath, pcolor is a surf object and the colors are assigned to the vertices not the faces.

jj = [0,1,0;1,0,1;1,1,0];

% Add an extra column
jj(:,end+1) = 0;

% Add an extra row
jj(end+1,:) = 0;

p = pcolor(jj);

Now if you want only the integer labels on the axes, you can tweak them using the XTick/Ytick and XTickLabel/YTickLabel properties of the axes to draw the proper values for each element

xlims = get(gca, 'XLim');
ylims = get(gca, 'YLim');

% Must offset ticks by 0.5 to center them
xticks = (xlims(1) + 0.5):(xlims(2) - 0.5);
yticks = (ylims(1) + 0.5):(ylims(2) - 0.5);

xticklabels = arrayfun(@num2str, 1:numel(xticks), 'UniformOutput', 0);
yticklabels = arrayfun(@num2str, 1:numel(yticks), 'UniformOutput', 0);

set(gca, 'xtick', xticks, 'ytick', yticks, ...
         'xticklabel', xticklabels, 'yticklabel', yticklabels)

If you don't need the black edges, you can accomplish the same 2D plot by just using imagesc

imagesc(jj);

xlims = get(gca, 'xlim');
ylims = get(gca, 'ylim');

set(gca, 'xtick', xlims(1):xlims(2), 'ytick', ylims(1):ylims(2))
Suever
  • 64,497
  • 14
  • 82
  • 101