How to plot number of a matrix with colorcode? (2024)

4 views (last 30 days)

Show older comments

AS on 16 Dec 2021

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode

Commented: AS on 16 Dec 2021

Accepted Answer: DGM

A matrix with dimension 2193 * 2, where 1st column is depth and 2nd column is number ( 1 to 6). I want to plot these number with specific color like 1 is green, 2 is black, 3 is cyan, 4 is red, 5 is blue, and 6 is yellow. How to plot it with specific color against depth. Please, help me with useful information. thanks

2 Comments

Show NoneHide None

DGM on 16 Dec 2021

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1889845

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1889845

Edited: DGM on 16 Dec 2021

Open in MATLAB Online

  • Book1.xlsx

What exactly do you want? When you say "plot it with specific color against depth", you realize that the result for each category will be constant-valued, right?

A = xlsread('Book1.xlsx');

scatter(A(:,1),A(:,2),10,A(:,2))

cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];

colormap(cmap)

How to plot number of a matrix with colorcode? (3)

I'm assuming you want something other than this. Maybe something like this?

figure;

A = xlsread('Book1.xlsx');

idx = 1:size(A,1);

uv = unique(A(:,2));

hold on

for k = 1:numel(uv)

mask = A(:,2) == uv(k);

plot(idx(mask),A(mask,1),'o');

end

cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];

set(gca,'colororder',cmap)

How to plot number of a matrix with colorcode? (4)

Not that I think that's particularly readable either.

AS on 16 Dec 2021

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1889875

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1889875

thank you for your answer. But I want to plot these number with depth value (1st column). Depth will be in Y-axis. And the numbers will be plotted as color.

Sign in to comment.

Sign in to answer this question.

Accepted Answer

DGM on 16 Dec 2021

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#answer_856315

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#answer_856315

Edited: DGM on 16 Dec 2021

Open in MATLAB Online

  • Book1.xlsx

You can use pcolor()

A = xlsread('Book1.xlsx');

w = 100;

h = pcolor(1:w,A(:,1),repmat(A(:,2),[1 w]));

set(h,'edgecolor','none')

set(gca,'ydir','reverse','xticklabel',{})

axis equal

xlim([1 w])

cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];

colormap(cmap)

How to plot number of a matrix with colorcode? (7)

Something like that?

3 Comments

Show 1 older commentHide 1 older comment

AS on 16 Dec 2021

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1890085

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1890085

Thank you so much.

Why w is taking 100

DGM on 16 Dec 2021

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1890240

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1890240

w sets the width of the plot box in data units. You can adjust it to make the plot wider or narrower.

AS on 16 Dec 2021

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1890285

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#comment_1890285

thank you. Understood.

Sign in to comment.

More Answers (3)

Walter Roberson on 16 Dec 2021

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#answer_856230

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#answer_856230

Edited: Walter Roberson on 16 Dec 2021

Open in MATLAB Online

filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/835655/Book1.xlsx';

A = readmatrix(filename);

N = size(A,1);

pointsize = 25;

cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];

scatter(1:N, A(:,1), pointsize, A(:,2));

colormap(cmap);

colorbar()

ylabel('Depth')

xlabel('row number')

How to plot number of a matrix with colorcode? (12)

scatter(A(:,2), A(:,1), pointsize, A(:,2))

colormap(cmap)

xlabel('category')

ylabel('Depth')

How to plot number of a matrix with colorcode? (13)

boxplot(A(:,1), A(:,2))

xlabel('category')

ylabel('depth')

How to plot number of a matrix with colorcode? (14)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

DGM on 16 Dec 2021

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#answer_856245

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#answer_856245

Edited: DGM on 16 Dec 2021

Open in MATLAB Online

  • Book1.xlsx

Here's my latest guess:

A = xlsread('Book1.xlsx');

uv = unique(A(:,2));

hold on

for k = 1:numel(uv)

mask = A(:,2) == uv(k);

plot(A(mask,1));

end

cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];

set(gca,'colororder',cmap)

ylabel('depth')

xlabel('sample number')

How to plot number of a matrix with colorcode? (16)

Here, depth is plotted against the number of each sample in the respective categories. If depth is plotted against the sample number across all categories (i.e. the row subscript), then the result is the same as Walter's first example and the second example I gave.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

AS on 16 Dec 2021

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#answer_856265

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1612075-how-to-plot-number-of-a-matrix-with-colorcode#answer_856265

Thanks everyone for your responses. But, I want to plot it using imagesc coomand. I have plotted it and unable to mention specific colorcode of mine. Like ike 1 is green, 2 is black, 3 is cyan, 4 is red, 5 is blue, and 6 is yellow. Kindly, help me to sove this.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D Plots

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

  • colormap
  • imagesc

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to plot number of a matrix with colorcode? (18)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How to plot  number of a matrix with colorcode? (2024)

References

Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 6146

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.