How to change plot background color for some datapoints?

I would like to produce a plot as shown below. The feature I am looking are the different background colors for some of the datapoints. How can I do this?
Thank you!

 Accepted Answer

You can use rectangular regions with fill(), or you can use rectangle() with a FaceColor . Either way, you can draw those background areas first, or if you draw them after the plot be sure to uistack() them to the beneath the plot lines.

5 Comments

Can you please suggest any different solution for this problem because it gets slow for large dataset.
rectangle() cannot be used for creating multiple objects in a single call, so it does not scale up.
fill() calls patch() to do its work, but it calls it in a way that does not automatically construct multiple faces for disjoint regions. If you provide multiple x, y coordinates to fill() then it creates one patch per set of coordinates, which does not scale all that well.
The more efficient method for creating multiple regions is to call patch() more directly.
patch(Xmatrix, Ymatrix)
creates one visible region for each column in Xmatrix paired with the corresponding column of Ymatrix . In each column, if the column does not end in nan or inf, then then face will automatically be closed back to the first point in the column. To draw a mix of face sizes, pad the shorter columns with nan. Because nan also signals not to close the face, to draw a mix of face sizes, make sure you duplicate the original point before you pad with nan.
If you are drawing something long and detailed such as a map, then give some thought as to whether the details will be visible at the resolution you are plotting at. You might want to consider using reducepatch() . You can pass in a face/vertices structure and reduce that without having first displayed the structure.
if you're using a grid, how would you make the grid appear overtop the rectangle?
Grids are considered part of an axes. You can have axes appear on top by setting the axes Layer property to 'top' instead of the default 'bottom'

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!