# Heat (or color coded Map) in Matlab

15 ビュー (過去 30 日間)
CMatlabWold 2020 年 6 月 23 日
コメント済み: CMatlabWold 2020 年 6 月 26 日
Hello,
I am creating a heatmap in Matlab. For each Sewershed, there is a median income. It is in the shapefile. When I produce the Sewershape in Matlab, I get this...
17×1 struct array with fields:
Geometry
BoundingBox
X
Y
Pervious
Impervious
Sewershed
Population
Housing
Acres
Squarefeet
GallonsH2O
SymbolID
Median
This is my code:
mapshow(S)
for i=1:17
meanValue = mean(S(i).BoundingBox);
text(meanValue(1),meanValue(2),num2str(S(i).Sewershed))
end
So, I need the median income for each sewershed to have a color scale, where the largest income is darker red and the smaller incomes become lighter... (red or any color... but darker for higher income and lighter for lower income).
Please, let me know if you can help.
Thanks
C

サインインしてコメントする。

### 採用された回答

Kelly Kearney 2020 年 6 月 24 日
In my opinion, the easiest way to do this is to alter the colors of the patches after plotting with mapshow/geoshow. An example:
h = mapshow(S);
set(h.Children, {'CData'}, {S.PopDens2000}'); % Add single cdata value to each based on property
set(h.Children, 'facecolor', 'flat'); % And link to the colormap
set(gca, 'clim', [5 500]);
An alternative would be to take a look at the makesymbolspec function, and the 'SymbolSpec' option for mapshow. But I find that option horribly unintuitive, inflexible, and a little buggy.

#### 3 件のコメント

CMatlabWold 2020 年 6 月 24 日
Thank you! This works perfectly.
I excluded set(gca, 'clim', [5 500]);
For some reason, it wasn't working for me.
I went to edit the color map. I wanted the color to be "Summer". Is there anyway to add that to the code.
Also, is there a way to add a legend that shows the values for the colors or at least the min and max?
Thanks again!
Kelly Kearney 2020 年 6 月 24 日
The 'clim' bit was just an example... that line sets the color limits of the figure (the example data I showed had a few high outliers, so I wanted to narrow the color range from the defailt). You can stick with the defaults, or choose limits that are appropriate to your data.
If you want to change the colormap and add a colorbar, simply add
colormap(summer);
colorbar;
CMatlabWold 2020 年 6 月 26 日
Thanks

サインインしてコメントする。

### その他の回答 (1 件)

KSSV 2020 年 6 月 23 日
Arrange all your mean values in an array.
M = mean(S(:).BoundingBox) ; % get all means in an array
rgb = vals2colormap(M,'jet') ; % get the respective colorcode for M
figure
hold on
for i = 1:17
patch(S(i).X,S(i).Y,rgb(i,:)) ;
end
colorbar

#### 2 件のコメント

CMatlabWold 2020 年 6 月 23 日
Thank you.
I used the function and the code; however, the color does not show.
Of my structural array, there are 17 fields. The only field where I need a color density is the "Median".
Where would I incorporate this into the code?
KSSV 2020 年 6 月 24 日
Share the file.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by