0

I'm trying to figure out a way to insert a shaded band in a plot window. The aim is to highlight various regions inside a plot, and hence couple it to an xline() and a color.

Picture attached for an example.

Example

I presume there's a way to work with fill() and use a fixed ratio of the y-axis to maintain consistent height. But maybe there's an existing function I'm unaware about, or an alternative way of doing it?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
yzt
  • 1
  • 2
  • I see this question has been closed, but the linked questions do not really address your situation. What you want to achieve is possible, but AFAIK not simple. You need to set up callback functions when the plot is zoomed/resized/etc. The callback function needs to delete the existing fill()'s, then make new ones. To do that you will need to store the handles returned by fill(), perhaps in the UserData property of the axes. I would also suggest setting HandleVisibility on those handles to 'off' so they don't show up in any legends. Related: https://stackoverflow.com/questions/4940561/ – sentiment Sep 01 '22 at 09:08
  • As an addendum to my already-long comment (ran out of characters), it is probably faster/better to manipulate the XData and YData properties of the objects created by fill(), rather than destroying the objects and making new ones every time. I haven't tried any of this, but believe it would work. Still though, not simple. – sentiment Sep 01 '22 at 09:09
  • @sentiment how do the duplicates not answer the question? The controls you mention are for interactivity on the figures, but nothing in the question suggests the OP wants that. As things stand, all we can say from this brief question, is that the OP wants to shade a specific region in a plot, which is covered by the duplicates: define your region (directly or by lines) and fill the internal area. – Adriaan Sep 01 '22 at 09:20
  • @Adriaan If the OP is not concerned with interactivity then you have a good point insofar as simpler options exist, but the duplicates do not actually help at all. The OP already knows about fill(), and the duplicates are not otherwise relevant. If you are correct, then OP's simplest path is to set up the plot (without the bands), get the axes's xlim() and ylim() values, and use those to construct fill commands. The duplicates do not discuss any of that. – sentiment Sep 01 '22 at 09:28
  • 1
    @sentiment then we end up in a rabbit-hole. The OP hasn't mentioned any code, i.e. we have no clue where they're stuck. Is it getting an axis limit? Plotting a patch using `fill()`? Setting the transparency of a patch? etc. etc. So, in the end all we've got to go on is the title + image, which *are* covered in the dupes: shading a specific area between given lines. If the duplicates don't answer their question, the OP should read [ask] and then [edit] the question to contain a [mre], in order for us to narrow down where the problem is. – Adriaan Sep 01 '22 at 09:33
  • @Adriaan I mean, we also have their actual question to go on. Which says that they're trying to break up a plot into sections of x values (probably to mark periods of time). They clearly know that fill() exists, but not how to make it do what they want (which the duplicates do not help with) or whether there is a premade way to do it (often true in Matlab; again duplicates don't help). I don't know what code you are expecting them to have provided. If the question hadn't been closed after 20 minutes, perhaps someone could have asked a few clarifying questions and actually helped them. – sentiment Sep 02 '22 at 03:00
  • @sentiment closing is a way to let the asker know that they should improve their question. It's not deleted, it can still be edited and comments are still possible. In other words, the OP can still interact with the post and and edit it in case they do not agree with the closure. Given the OP posted this and seemingly ran away before they could clarify what they want, I presume either the duplicate answered their question, or they couldn't be bothered to invest more effort. – Adriaan Sep 02 '22 at 05:21

0 Answers0