3
$\begingroup$

Let's see the following test case

data = Table[{RandomReal[{-10, 10}], RandomReal[{-10, 10}]}, {i, 1, 50}];
l0 = ListPlot[data, PlotStyle -> {Blue, PointSize[0.01]}];
p0 = Plot[x*Sin[x], {x, -10, 10}, PlotStyle -> {Red, Thick}];
s0=Show[{l0, p0}, Frame -> True, FrameLabel -> {"x", "y"}, Axes -> False,
        GridLines -> {{1.5, 7}, {}},
        GridLinesStyle -> Directive[Black,Thickness[0.004], Dashed], 
        Method ->  {"GridLinesInFront" -> True}, PlotRange -> {{0, 10}, {-10,10}}, 
        ImageSize -> 550]

which produces

plot

First a minor point: Even though that I define inside Show the plot range to be {{0,10},{-10,10}}, we see that in the plot both axes exceed the defined limits (i.e., the -0.5 < x < 10.5 instead of [0,10]).

Now my questions:

(a). I would like to add at the position of the vertical lines on the x-axis custom labels like text "P1" and "P2".

OR

(b) Force the program to add the numbers 1.5 and 7 in the horizontal x-axis but without changing the range, I mean to keep the (0,2,4,6,8,10) and add two more inside.

Any suggestions?

$\endgroup$
  • 1
    $\begingroup$ Your minor point can be solved using PlotRangePadding->0 $\endgroup$ – Verbeia Sep 26 '14 at 11:52
  • 1
    $\begingroup$ Add option FrameTicks -> {{1.5, 7}~Join~Range[0, 10, 2], Automatic} $\endgroup$ – Junho Lee Sep 26 '14 at 11:57
6
$\begingroup$
Show[{l0, p0},
 Frame -> True,
 FrameLabel -> {"x", "y"},
 FrameTicks -> {{Automatic, None},
   {{0, {1.5, Style["P1\n1.5", Red, 14]}, 2, 4,
     6, {7, Style["P2\n7.0", Red, 14]}, 8, 10}, None}},
 Axes -> False,
 GridLines -> {{1.5, 7}, {}},
 GridLinesStyle -> Directive[Black, Thickness[0.004], Dashed],
 Method -> {"GridLinesInFront" -> True},
 PlotRange -> {{0, 10}, {-10, 10}},
 PlotRangePadding -> 0,
 ImageSize -> 550]

enter image description here

To solve your "minor point" I have added the option PlotRangePadding -> 0.

$\endgroup$
  • $\begingroup$ Do you know about the SE Uploader palette? I ask because I've noticed your graphics don't look very clean. $\endgroup$ – RunnyKine Sep 26 '14 at 12:15
  • $\begingroup$ Yes it works however by editing manually the ticks all the minor ticks are not shown any more. Why? Can this be fixed? $\endgroup$ – Vaggelis_Z Sep 26 '14 at 12:51
  • 1
    $\begingroup$ @Vaggelis_Z The same problem appears in the Doxumentation. A certain fix: Replace the last None in my Frameticks-option with Automatic. Now you see the minor ticks at the top of the frame. BTW, thanks for acceptance :) $\endgroup$ – eldo Sep 26 '14 at 13:05
  • $\begingroup$ @RunnyKine Thank you so much. SE Uploader installation went very well and - after watching the video - I could make it work easily. A very clever app :) $\endgroup$ – eldo Sep 26 '14 at 17:59
4
$\begingroup$

Perhaps,

s0 = Show[{l0, p0}, Frame -> True, FrameLabel -> {"x", "y"}, 
  Axes -> False, GridLines -> {{1.5, 7}, {}}, 
  GridLinesStyle -> Directive[Black, Thickness[0.004], Dashed], 
  Method -> {"GridLinesInFront" -> True}, 
  PlotRange -> {{0, 10}, {-10, 10}}, ImageSize -> 550, 
  FrameTicks -> {{0, {1.5, Style[1.5 , Red, Bold]}, 2, 4, 
     6, {7, Style[7, Red, Bold]}, 8}, Automatic}, 
  Epilog -> {Text[Framed["P1"], {1.5, 0}, {-1, 1}], 
    Text[Framed["P2"], {7, 0}, {-1, 1}]}]

enter image description here

$\endgroup$
  • $\begingroup$ I have the same comment as in eldo's answer. All the minor ticks of the x-axis are no longer visible. Why? $\endgroup$ – Vaggelis_Z Sep 26 '14 at 12:52

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.