MATLAB Answers

How to plot the colormap of a 3D data set

122 views (last 30 days)
Penemonie
Penemonie on 19 Jan 2018
Commented: Penemonie on 21 Jan 2018
1st var: age [31,32,33,34,35]
2nd var: years of working [10,15]
3rd var: corresponding yearly income(*10k) [8,8,6,9,9,10,14,17,13,9]
-----edits---->Each combination of age&years corresponds to an income. For example, 31-yr-old person with 10 yrs of working experience earns 80k, 31-yr-old person with 15 yrs of working experience also earns 80k, etc.<----edits-----
I would like to represent the numbers in the third column with a color scale (smaller=blue --> larger=yellow) looking like a 2D histogram except the third dimension doesn't represent number of occurrence.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jan 2018
income_matrix = reshape(yearly_income, length(years_working), []);
imagesc(age,years_working,income_matrix)
xticks(age)
yticks(years_working)
It was not clear what order the data was in in yearly_matrix so you need to check the first line. Is your yearly income arranged so that all of the ages are used in order for each years working entry? Or is it arranged so that all of the years working are used in order for each age? The above code assumes it is the years working are grouped together.

  5 Comments

Show 2 older comments
Penemonie
Penemonie on 19 Jan 2018
Thanks Walter!
I tried your suggestion with the following code
clc
clear all
age = [31,32,33,34,50];
year = [10,15];
income = [8,8,6,9,9,10,14,17,13,9];
income_matrix = reshape(income, length(year), []);
imagesc(age,income,income_matrix);
xticks(age);
yticks(year);
colorbar;
and here is the result:
It is what I wanted but there appears to be two problems:
1. y-axis doesn't display the corresponding year
2. I changed the last age to 50 and now the previous ages are pushed all the way to the left. They don't sit right underneath the blocks.
Do you have any suggestions on fixing these?
Walter Roberson
Walter Roberson on 19 Jan 2018
I suspect the colorbar might be interfering with the y axis labels. Try
set(gca, 'YAxisLocation', 'left')
If that does not work then try again without the colorbar and see whether the y axes ticks show up.
Getting the x axes to label properly is a going to be a bit of a nuisance if you need the data cursor to show the correct position information. If you do not care about data cursor then use
imagesc(income_matrix);
xticks(1:length(age));
yticks(1:length(year));
set(gca, 'xticklabel', age);
set(gca, 'yticklabel', year);
Penemonie
Penemonie on 21 Jan 2018
combing these two worked!!Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!