plot in different colors for fit function
Show older comments
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
More Answers (3)
Amanullah Khan
on 3 Dec 2020
Edited: Amanullah Khan
on 3 Dec 2020
0 votes
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
Walter Roberson
on 4 Dec 2020
Plotting multiple surfaces in the same axes is not typically readable.
... the format of your variables stored in the .mat files is very odd. A table() object in which there are at least three variables, but all three variables are cell arrays of numeric vectors ?? Why not just make a table object in which the variables are each numeric vectors ??
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
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.
Amanullah Khan
on 18 Feb 2021
0 votes
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
on 18 Feb 2021
0 votes
Here is what you need to do in order to plot different colors for different surfaces:
For each surface:
- define the minumum and maximum data values that you want to have respond to color.
- 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
- 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.
- 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.
- 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.
- 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)
- hold on
- 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
on 18 Feb 2021
Thank you so much Mr Walter. Will try to do this. Also is it possible to name the surface while clicking on it?
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
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.
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!