Mathematica Add a rectangle to a plot to indicate range

AI Thread Summary
To add a rectangular region to a plot indicating a specific x-range, use the Graphics function with a Rectangle command, specifying the coordinates for the desired range. For instance, the code snippet provided demonstrates how to create a rectangle from x=4 to x=7, with the y-range set to 0 to 10. To fill the rectangle with color, simply replace "Transparent" with the desired color name. To add a dashed line indicating the maximum y-value, the Epilog option can be used, though further clarification or a sketch may be needed for precise implementation. This approach allows for clear visualization of both the x-range and the maximum y-boundary in the plot.
Whitehole
Messages
128
Reaction score
4
I have some data points that I want to plot and suppose I have the function ##f(x) = x##, with the domain having the range ##0\leq x \leq 10##. Assume that the experimental values lies in the range ##4\leq x \leq 7##, how can I put a rectangular region to cover this range behind my plot so as to show that range? The y-range should not be a trouble here since I plan to use a different option for that, so the rectangular region should just show the bound for the x-range.

For the y-range suppose the maximum value for y is 8, how can I put a dashed line to indicate the maximum y-value?

I have tried to use the epilog and prolog but they don't work, I mean just based on what I saw from some posts, but I'm really a newbie on this so it's not even worth a look. Maybe somebody can give me a basic example or guide?

Also, my data points are imported from excel, but let's just assume that the data points can be represented by the linear function above to make things simple; x,y values take on discrete values.
 
Physics news on Phys.org
Try something like
Code:
Show[Plot[x, {x, 0, 10}],
Graphics[{EdgeForm[Thin], Transparent, Rectangle[{4, 0}, {7, 10}]}]]
plot.jpg
 
DrClaude said:
Try something like
Code:
Show[Plot[x, {x, 0, 10}],
Graphics[{EdgeForm[Thin], Transparent, Rectangle[{4, 0}, {7, 10}]}]]
View attachment 205513
Is it possible for the rectangle to be filled,say by light blue color or light green?
 
Whitehole said:
Is it possible for the rectangle to be filled,say by light blue color or light green?
Yes, simply replace Transparent by the desired color.
 
DrClaude said:
Yes, simply replace Transparent by the desired color.
How should I use Epilog for the dashed line upper boundary?
 
Whitehole said:
How should I use Epilog for the dashed line upper boundary?
I don't understand what you have in mind. Could you make a sketch?
 

Similar threads

Replies
4
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
4
Views
1K
Replies
4
Views
2K
Replies
1
Views
3K
Back
Top