Restrict grid to particular segment

If I create a grid function for a particular function space on a segment of the grid, still the complete grid is visualized, although the grid function is only shown on the segment it is defined on.

Is there a way to only include the segment of the grid where the grid function is defined on in the visualisation?

If you export the grid function and open it in the Gmsh GUI, you can specify the segments for visualisation. In the menu, go to Tools → Visibility, in the tab List, choose Physical Groups in the dropdown box at the bottom, and select the surfaces you wish to show.