plot in different colors for fit function

163 views (last 30 days)
Hi, I want to plot the following two graphs in one plot - unfortunately they are both red:
mid_bin = [189 247 305 363 421];
corr_perct = [0.2500 0.8889 1.0000 1.0000 0.8000];
edges = [160 218 276 334 392 450];
mid_bin_Kendall = [189 247 305 363 421];
corr_perct_Kendall = [8.3750 9.5000 10.0000 10.0000 9.4000];
edges_Kendall = [160 218 276 334 392 450];
hold on
f = fit(mid_bin', corr_perct', 'smoothingspline')
plot(f,mid_bin',corr_perct')
f = fit(mid_bin_Kendall', corr_perct_Kendall', 'smoothingspline')
plot(f,mid_bin_Kendall',corr_perct_Kendall')
axis([min(edges_Kendall) max(edges_Kendall) 0 15])
hold off
Now if I change the lower graphs color as below, the graph becomes red AND black at the same time with the datapoints not visible anymore. That is not the idea. The graph should become just black with the datapoints still visible. What can I do?
hold on
f = fit(mid_bin', corr_perct', 'smoothingspline')
plot(f,mid_bin',corr_perct')
f = fit(mid_bin_Kendall', corr_perct_Kendall', 'smoothingspline')
plot(f,mid_bin_Kendall',corr_perct_Kendall','black') % color added
axis([min(edges_Kendall) max(edges_Kendall) 0 15])
hold off

Accepted Answer

dpb
dpb on 12 May 2015
hL1=plot(f,mid_bin',corr_perct');
hold on
f = fit(mid_bin_Kendall', corr_perct_Kendall', 'smoothingspline');
hL2=plot(f,mid_bin_Kendall',corr_perct_Kendall');
axis([min(edges_Kendall) max(edges_Kendall) 0 15])
set([hL1(2) hL2(2)],'color','k')
seems fine here. You might want to make the markersize a little larger, but that would be personal choice.
  3 Comments
dpb
dpb on 12 May 2015
The "problem" is that the cfit/plot specific routine is creating two separate lines for the plot -- one for the data points and another for the fitted curve; hence the two default labels of 'data|fitted curve'. You've got four items to label but have given only two labels above as legend associates the text to the line on a one-to-one correspondence.
To see this
>> get(hL1,'xdata')
ans =
[1x5 double]
[1x1001 double]
>>
You see that the data points are only the five points but it generated a 1001 points between first and last to draw the smooth curve.
To put a label for the two solid lines and leave the data to be inferred by their proximity and appear to be a single plotted line for each use
legend([hL1(2) hL2(2)],'Kendall','Percentage')
This will leave the legend line color consistent with the lines on the plot; the symbol won't show. It would take munging on the actual line objects within the legend itself to do that--doable if you save the handles of the objects therein and then set the properties. See
doc legend
for details; NB: the optional second output that gives handles of the various objects.

Sign in to comment.

More Answers (3)

Amanullah Khan
Amanullah Khan on 3 Dec 2020
Edited: Amanullah Khan on 3 Dec 2020
Hello,
Even I have to plot Multiple graphs with different color and the filenames should also be displayed on the plotted surface. Please could you let me know how we could do it with this Program I have attached.
One I have made with Repeating the format and the other with a for loop.
Please let me know which would be better.
  3 Comments
Amanullah Khan
Amanullah Khan on 5 Dec 2020
Hello ,
I didn't understand the numeric vectors part. have plotted the surfaces in a single plot. But do you know if I could change their colors?
Walter Roberson
Walter Roberson on 10 Dec 2020
Draw one surface. colormap() an appropriate set of colors for it. Use freezecolors() from the File Exchange to convert the colormap data into RGB data. Then draw the next surface, colormap() to set colors for it. The existing surface will not be affected because the existing surface was already converted to RBG.

Sign in to comment.


Amanullah Khan
Amanullah Khan on 18 Feb 2021
Dear Mr Roberson,
I need different color for different surfaces. I have uploaded my program. Please could you check the program and let me know what could be done.
Regards,
Amanullah

Walter Roberson
Walter Roberson on 18 Feb 2021
Here is what you need to do in order to plot different colors for different surfaces:
For each surface:
  1. define the minumum and maximum data values that you want to have respond to color.
  2. find or design a colormap that you want to represent the surface. A colormap is an N x 3 array representing N colors, with the columns representing R, G, and B components as double precision numbers in the range 0 (dark) to 1 (full bright.) The first row, cmap(1,:) is to be used for the minimum data value you want to map, and the last row cmap(N,:) is to be used for the maximum data you want to map
  3. double() the data values if they are integer data type. Subtract the minimum data value you want to map. Divide the result by the difference between the maximum and minimum value that you want to map. This normalizes the values to the nominal range 0 to 1. Take max(0, min(1, NormalizedData)) . This will cause any values below the minimum you wanted mapped to be represented by the minimum, and any values above the maximum you wanted mapped to be represented by the maximum.
  4. Multiply the normalized data values by (1-eps) . Now multiply the result by N, the number of colors in the color map, and take floor() of that, and add 1 to the result. This has the effect of mapping the normalized 0 to 1 (first index in the color map) and the normalized 1 to the last index in the color map. These values will be used as colormap indices.
  5. Take the colormap, cmap, and index it cmap(Indices,:) and reshape() that to have size(values,1) rows, size(values,2) columns, and 3 colorplanes. The result is an RGB image the same size as your data. Call it surfImage for the moment.
  6. now call warp() passing in your grid of x coordinates, then your grid of y coordinates, then your grid of z coordinates, and then your RGB image. So warp(Xgrid, Ygrid, Z, surfImage)
  7. hold on
  8. go back and do the other surfaces
You have total control over the colormap contents. You have total control over the range of data to be mapped. You have total control over the x, y, and z that your surface will be drawn at. If you had good reason to, you could even implement a log mapping instead of the linear mapping I described in the third step.
You can use mat2gray() or rescale() to implement the third step, the normalization of the values to the 0 to 1 range.
  4 Comments
Amanullah Khan
Amanullah Khan on 23 Feb 2021
Edited: Amanullah Khan on 23 Feb 2021
Dear Mr Roberson,
I went through this but it only gives me gradient color for surfaces individually. I would also like to read all the table. mat files at one time and all surface simultaneously. Even if its a rigid colour for one surface its fine. but different surfaces should have different Color.
Walter Roberson
Walter Roberson on 23 Feb 2021
"find or design a colormap that you want to represent the surface."
You can use a different colormap for each one. You can arrange so that you do not use the same color in any other color map. You can use a colormap with the same color repeated for the (only) two entries so that you get a solid color for that surface.
You can figure out the number of surfaces ahead of time and linspace(0,1,N+1) and throw away the last value. Now you can use those individually as the Hue component of an HSV list of colors, that you then convert to rgb.

Sign in to comment.

Categories

Find more on Colormaps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!