0

I need to plot a color codded 2d graph using a .dat file. data in the file is arranged as

48.000000 0.000184 0.400000 
48.500000 0.000185 0.400000 
49.000000 0.000186 0.400000 
49.500000 0.000187 0.400000 
50.000000 0.000187 0.400000 
50.500000 0.000186 0.400000 
51.000000 0.000186 0.400000 
51.500000 0.000186 0.400000 
52.000000 0.000185 0.400000 
52.500000 0.000184 0.400000 
53.000000 0.000184 0.400000 
53.500000 0.000182 0.400000 
54.000000 0.000180 0.400000 
54.500000 0.000179 0.400000 
55.000000 0.000177 0.400000 
55.500000 0.000174 0.400000 
56.000000 0.000172 0.400000 

here 3rd column is also changing. There are almost 3000 lines. I need to plot a color codded 2d graph between 1st and 2nd variable and color has to put as height of 3rd variable. Can someone help me ?

bc rusty
  • 98
  • 1
  • 2
  • 8
user1598764
  • 25
  • 1
  • 7

2 Answers2

3

If you want the colors of your data to go from dark to light based on the value in column 3, then you might be best off just using the scatter function. According to the documentation

scatter(X,Y,S,C) displays colored circles at the locations specified by the vectors X and Y (which must be the same size).

S determines the area of each marker ...

C determines the color of each marker. When C is a vector the same length as X and Y, the values in C are linearly mapped to the colors in the current colormap.

This means that you can explicitly chose the colormap that you want your data to use. Assuming col1, col2, col3 contain the values in each of the three columns of your data, the following code will draw a scatter plot with col1 and col2 defining the x and y positions (respectively) and col3 defining the color of each point.

scatter(col1, col2, 25, col3, '.');
colormap(gray);

After drawing the scatter plot, I explicitly set the colormap to gray so that points in col3 with a small value will be dark and those with a large value will be light. Note that in this example the marker area is 25 and the marker type is a dot ('.'), as specified by the 3rd and 5th parameters of the scatter function.

There are many other colormaps that you could use besides gray. For example, hot or copper might be more aesthetically pleasing. The doc for the colormap function gives more info on your other options.

Community
  • 1
  • 1
grungetta
  • 1,089
  • 1
  • 7
  • 8
  • `gscatter` does the same. The syntax would be `>> gscatter(col1, col2, col3, your_color_map(# of groups))` – AGS Aug 15 '12 at 03:09
  • i need to set color of graph with respect to height. for ex. for if column is .40 then pink and if column 3 is .50 red and it should keep on changing to other color like for .9 it should be blue i am using following code to open file fid=fopen('abc_1.33_0.6.dat'); XYZ=fscanf(fid,'%f %f %f',[3 Inf]); fclose(fid); – user1598764 Aug 15 '12 at 03:58
  • @AGS - My apologies. I knew that making a call to colormap after gscatter had no effect, but I did not realize that you could explicitly pass the colormap as a parameter. Yes, in that case, these two options are nearly identical. – grungetta Aug 15 '12 at 11:18
  • @user1598764 - If you want to specify your own colors, then the documentation for the [colormap editor](http://www.mathworks.com/help/techdoc/ref/colormapeditor.html) might be useful. I'm not 100% sure what you mean by you need the graph to be continuous not discrete. Also, in your previous comment are you asking how to get the individual vectors col1, col2, and col3 from your matrix XYZ? – grungetta Aug 15 '12 at 11:27
0

Use >> gscatter(column1,column2,column3)

Since your column 3 is the same value, the plot will look like this: enter image description here

But, add some noise to the third column, and you'd get something like this:

enter image description here

AGS
  • 14,288
  • 5
  • 52
  • 67
  • i have written in the problem that column 3 is also changing due to space i have written only a part of file. it changes from .4 to .90 with variation of .05 – user1598764 Aug 14 '12 at 18:55
  • ok, thats fine. My example showed you that when your grouping variable is actually varying, you will get the second plot. Is this helpful, if not, why? – AGS Aug 14 '12 at 18:59
  • actually i need to set the color from low to high[light to dark or vice versa] not random – user1598764 Aug 14 '12 at 19:07
  • Please actually try what I have posted above. The colors are _not_ random - the noise I applied to your grouping vector was. – AGS Aug 14 '12 at 22:11
  • i mean to say that i need to set my own color ? how can i do that? – user1598764 Aug 15 '12 at 04:03
  • and i need a continuous graph not a discreet – user1598764 Aug 15 '12 at 04:32