MATLAB Answers

How does pcolor basically work? Please help me understanding pcolor, color matrix, and colormap work.

1 ビュー (過去 30 日間)
HyunSuk Lee
HyunSuk Lee 2021 年 5 月 7 日
編集済み: HyunSuk Lee 2021 年 5 月 7 日
I am trying use pcolor, but I do not understand how pcolor works.
First, I read the description in the above link.
X = [1 2 3; 1 2 3; 1 2 3];
Y = X';
mymap = [1 0 0; 0 1 0; 0 0 1; 1 1 0; 0 0 0];
C = [3 4 5; 1 2 5; 5 5 5];
It's written that "The color of a face depends on the color at one of its four vertices. Because of this relationship between the vertex colors and face colors, none of the values in the last row and column of C are represented in the plot."
Therefore, I thought only elements C(1,1), C(1,2), C(2,1) and C(2,2) do matter. The elements C(1,3), C(2,3), C(3,1), C(3,2), C(3,3) do no matter, so I can put any arbitrary number in these places to produce the same image above.
Second, I read the description in the above link.
It’s written that “To create a custom colormap, specify map as a three-column matrix of RGB triplets where each row defines one color. An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color.”
Therefore, I thought first row [1 0 0] of mymap designates red, and the second row [0 1 0] designates green, and the third row [0 0 1] designates blue, and the fourth row [1 1 0] designates yellow, and the fifth row [0 0 0] designates black. For example, C(1,1)=3 designates the third row of mymap, which is blue. C(2,2)=2 designates second row of mymap, which is green.
Then, based on my understanding, I tried to plot a simple square with a specific color I want.
X = [0 1; 0 1];
Y = [0 0; 1 1];
mymap = [1 0 0; 0 0 0];
C = [1 1 ; 1 1];
pcolor(X, Y, C)
Based on my understanding, only one element of matrix C would be represented in the plot, and the remaining three elements do not matter. Since all elements are 1, it designates first row of mymap, which is red.
However, it plots a black square, which is not what I expected.
Based on my understanding, the color matrix C must have only positive integers as its elements, since it should designate specific row of mymap. However, C = [1.4 1.7 ; 1.2 0.1] does not provide any error, and it plots a black square again.
Basically, I do not understand how pcolor, color matrix, and colormap work. I have been Googling for a full day, but I have not understood yet, which is very frustrating. Please help me.


Bjorn Gustavsson
Bjorn Gustavsson 2021 年 5 月 7 日
If you add a call:
to your tests you will see what levels of C corresponds to what colours in the colormap. That will help you to grasp this. In essence you get the caxis/clims to be between the largest and smallest element in the matrix C - which will get you the display of the 1.4 element to map to the second and last colour in your red-black colour-map. You should also have a look at how
shading interp
modifies your plot.
  1 件のコメント
HyunSuk Lee
HyunSuk Lee 2021 年 5 月 7 日
Really thanks a lot. Those two lines
shading interp
helped me understand how the codes work. In fact, I am not still sure how "shading interp" exactly works.
Although I have not fully understood, I succeeded in getting the plot I wanted.


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by