2
$\begingroup$

I want to make PlotRangeClipping -> True on each side of the plot (see code below) except the top side must be PlotRangeClipping -> Flase, is that possible?

Plot[{x, -x}, {x, -5, 5}, PlotRange -> {{-1, 0}, {-3, 3}}, 
 Frame -> True,PlotLegends -> 
  Placed[LineLegend[{"Y1", "Y2"}, LegendLayout -> {"Row", 1}, 
    LegendMarkerSize -> 20], {{0.5, 0.5}, {0, -1.8}}], 
 PlotRangePadding -> None, PlotRangeClipping -> False, 
 ImagePadding -> 80]  

enter image description here

Update

The problem is that I would like to place the legend at a specific position so that I used

Placed[LineLegend[{"Y1", "Y2"}, LegendLayout -> {"Row", 1}, 
        LegendMarkerSize -> 20], {{0.5, 0.5}, {0.5, -1.8}}]   

of course, the problem would be solved if I replace {{0.5, 0.5}, {0.5, -1.8}} by Above ( as shown in the answer by @Bob Hanlon) but this will not help me to place the legend at the desired position.

$\endgroup$
2
  • 1
    $\begingroup$ add the option RegionFunction -> (-1 <= # <= 0 &)? $\endgroup$
    – kglr
    May 4 at 8:15
  • $\begingroup$ may please post it as an answer so I can accept it? $\endgroup$ May 4 at 9:30
1
+50
$\begingroup$

Adding the option RegionFunction -> (-1 <= # <= 0 &) gives the desired result:

Plot[{x, -x}, {x, -5, 5}, 
 PlotRange -> {{-1, 0}, {-3, 3}}, 
 Frame -> True, 
 PlotLegends -> 
  Placed[LineLegend[{"Y1", "Y2"}, LegendLayout -> {"Row", 1}, 
    LegendMarkerSize -> 20], {{0.5, 0.5}, {0, -1.8}}], 
 PlotRangePadding -> None,
 PlotRangeClipping -> False, 
 ImagePadding -> 80, 
 RegionFunction -> (-1 <= # <= 0 &)]

enter image description here

$\endgroup$
5
  • $\begingroup$ Just one more question please, can I use it with ListLinePlot? e.g. data = Table[{x, x}, {x, -5, 5, 0.1}]; ListLinePlot[data, PlotRange -> {{-1, 0}, {-3, 3}}, Frame -> True, PlotLegends -> Placed[LineLegend[{"Y1", "Y2"}, LegendLayout -> {"Row", 1}, LegendMarkerSize -> 20], {{0.5, 0.5}, {0, -1.8}}], PlotRangePadding -> None, PlotRangeClipping -> False, ImagePadding -> 80, RegionFunction -> (-1 <= # <= 0 &)].is not working $\endgroup$ May 4 at 11:13
  • 1
    $\begingroup$ @valarmorghulis, you can use ListLinePlot[Select[-1 <= #[[1]] <= 0 &]@data,...] (similarly for ListPlot). $\endgroup$
    – kglr
    May 4 at 19:04
  • $\begingroup$ Thanks @klgr, but it did not work when I use this type of data data=Transpose[Table[{x, Sign[i] x^2 + i}, {x, -5, 5, 0.1}, {i, -5, 5, 1}]]. i tried this ListLinePlot[Select[-2 <= #[[1]] <= 2 &]@Transpose[data], PlotRange -> {{-2, 2}, {-5, 5}}, Frame -> True] $\endgroup$ May 4 at 20:19
  • 1
    $\begingroup$ try data = Transpose[Table[{x, Sign[i] x^2 + i}, {x, -5, 5, 0.1}, {i, -5, 5, 1}]]; ListLinePlot[Map[Select[-2 <= #[[1]] <= 2 &]]@data, PlotRange -> {{-2, 2}, {-5, 5}}, Frame -> True]? $\endgroup$
    – kglr
    May 4 at 21:12
  • $\begingroup$ Thanks a lot, it is working $\endgroup$ May 4 at 21:38
1
$\begingroup$

This appears to be an XY problem. Apparently, you want to display the legend above the plot. The issue is not PlotRangeClipping. Use Placed

Plot[{x, -x}, {x, -5, 5},
 PlotRange -> {{-1, 0}, {-3, 3}},
 Frame -> True,
 PlotLegends ->
  Placed[{"Y1", "Y2"}, Above]]

enter image description here

EDIT: Or you can control the ImagePadding

Plot[{x, -x}, {x, -5, 5},
 PlotRange -> {{-1, 0}, {-3, 3}},
 Frame -> True,
 PlotLegends -> Placed[{"Y1", "Y2"}, Above],
 ImagePadding -> {{80, 80}, {80, 5}}]

enter image description here

$\endgroup$
3
  • $\begingroup$ thanks @bob Hanlon, I am aware of the solution you provided, but for some reason I need to assign ImagePadding to be 80 in my plot. $\endgroup$ May 1 at 16:50
  • $\begingroup$ Recommend that you provide a more complete description of the desired end result. $\endgroup$
    – Bob Hanlon
    May 1 at 17:12
  • $\begingroup$ I would like to place the legends at specific position above the plot..Using Above places the legend randomly on the top of the plot which is not desired. $\endgroup$ May 1 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.