Highlighting a Specific Region on a Matlab Plot with Contour Question

  • MATLAB
  • Thread starter eahaidar
  • Start date
  • Tags
    Matlab
In summary: As for your second question, yes you can change the tick marks on the colorbar. Here's an example of changing the tick marks to range from 0 to 10 in intervals of 2.colorbar('YTick',0:2:10)In summary, if you want to highlight a specific region of a function plotted over x and y intervals, you can split the domains up and make separate plots overlaid. Alternatively, you can use logical indexing to pick out the relevant function values and change the color of the region. You can also adjust the colormap and tick marks on the colorbar to your desired values.
  • #1
eahaidar
71
1
Hello everyone
If I have a plot of a function of two variables and I want the whole curve over the whole x and y intervals but in addition I want to highlight with a black colour a specific region when the function is between two values
F between -8 and 0 or what ever
How can I do that !??
Thank you
 
Physics news on Phys.org
  • #2
This question is poorly presented, so I'll just take a guess as to what you mean.

If I have a plot of a function of two variables and I want the whole curve over the whole x and y intervals but in addition I want to highlight with a black colour a specific region when the function is between two values

This sounds like it has nothing to do with contours, but that you just want to plot a certain interval of the function in a different color. In that case you just split the domains up and make separate plots overlaid.

Code:
[x1,y1] = meshgrid(-8:0.25:-5,-8:0.25:8);
[x2,y2] = meshgrid(-5:0.25:5,-8:0.25:8);
[x3,y3] = meshgrid(5:0.25:8,-8:0.25:8);
f = @(x,y) x.^2 - 3*y.^3;
figure
hold on
plot3(x1,y1,f(x1,y1),'b','LineWidth',2)
plot3(x2,y2,f(x2,y2),'k','LineWidth',2)
plot3(x3,y3,f(x3,y3),'b','LineWidth',2)
xlabel('x')
ylabel('y')
zlabel('z')
view(-142,14)
 

Attachments

  • untitled.png
    untitled.png
    5.1 KB · Views: 505
  • #3
I reread your question and realized you might have meant you want to only change the color of the function when the function value is in some interval (instead of the function being evaluated in an interval as in my last post). In this case, you can just use logical indexing to pick out the relevant function values.

Notice the use of surfc here, which plots the surface and also adds a contour plot underneath.

Code:
[x,y] = meshgrid(-8:0.25:8);
f = @(x,y) x.^2 - 3*y.^3;
z = f(x,y);
q = find(z>0 & z<2);
figure
hold on
surfc(x,y,z)
plot3(x(q),y(q),z(q),'*r')
xlabel('x')
ylabel('y')
zlabel('z')
view(-143,42)
 

Attachments

  • untitled.png
    untitled.png
    32.5 KB · Views: 488
  • #4
Thank you very much for your time
The second part is what I want where I want to highlight a specific region.
I would love to ask some questions :
1-Can I add the color of the region to the color bar or not?
2- Can I change the steps of The numbers of the color bar instead of 10 20 30 I want them to take steps of 5 or some variable ?

Thank you for your time and I hope the questions make a bit more sense than last time
 

FAQ: Highlighting a Specific Region on a Matlab Plot with Contour Question

What is the purpose of highlighting a specific region on a Matlab plot with contour?

The purpose of highlighting a specific region on a Matlab plot with contour is to visually emphasize a particular area of interest in the data. This can help to draw attention to important features or trends in the data and make it easier to interpret the results.

How do I select a specific region on a Matlab plot for highlighting?

To select a specific region on a Matlab plot for highlighting, you can use the ginput function to select a set of points that define the boundary of the region. Alternatively, you can use the imfreehand function to draw a freehand shape around the region.

Can I customize the appearance of the highlighted region on a Matlab plot?

Yes, you can customize the appearance of the highlighted region on a Matlab plot by changing the line style, color, and thickness of the contour lines. You can also fill the region with a specific color or pattern to make it stand out more.

How can I add contour labels to the highlighted region on a Matlab plot?

To add contour labels to the highlighted region on a Matlab plot, you can use the clabel function. This function allows you to specify the contour levels for which you want to add labels, as well as the format and location of the labels.

Is it possible to highlight multiple regions on a single Matlab plot?

Yes, it is possible to highlight multiple regions on a single Matlab plot. You can use the hold on command to add multiple contour plots to the same figure, each one highlighting a different region of interest. You can also use different colors or line styles to differentiate between the highlighted regions.

Similar threads

Replies
32
Views
3K
Replies
11
Views
3K
Replies
3
Views
2K
Replies
3
Views
7K
Replies
1
Views
1K
Replies
6
Views
2K
Back
Top