Highlighting a Specific Region on a Matlab Plot with Contour Question

  • Context: MATLAB 
  • Thread starter Thread starter eahaidar
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around how to highlight a specific region on a Matlab plot of a function of two variables, particularly focusing on visualizing function values within a certain range. Participants explore methods for achieving this through contour plots and logical indexing, as well as addressing related questions about color mapping and color bar customization.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant asks how to highlight a specific region on a plot where the function values fall between two specified values.
  • Another participant suggests that the original question may not be about contours but rather about plotting different intervals of the function in various colors, proposing a method to overlay separate plots.
  • A later reply offers an alternative approach using logical indexing to highlight function values within a specified range, mentioning the use of the 'surfc' function to combine surface and contour plots.
  • The original poster expresses gratitude and clarifies that they want to highlight a specific region, asking additional questions about modifying the color bar and its steps.
  • One participant notes that editing the colormap is complex and recommends consulting the Matlab documentation for guidance on customizing colormaps.

Areas of Agreement / Disagreement

There is no consensus on the best method to highlight the region, as participants propose different approaches and interpretations of the original question. The discussion remains unresolved regarding the specifics of implementing the desired features in Matlab.

Contextual Notes

Participants express varying levels of familiarity with Matlab's colormap functionalities, indicating that some aspects of the discussion may depend on individual experience with the software.

eahaidar
Messages
69
Reaction score
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
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: 556
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: 549
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
 

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K