1
$\begingroup$

I have to plot on a log-log scale y=f(x) for these data

x = Table[s, {s, 0, 100, 0.1}];
y = Table[Exp[s - 1], {s, 0, 100, 0.1}];
data = Transpose@{x, y};

ListLogLogPlot[data, Joined -> True, PlotRange -> {0.1, 100}]

enter image description here

I would like to add on the same plot the vertical and horizontal lines: x=1, x=3

How to do it please?

MAT Version : 13.0

$\endgroup$
4
  • 2
    $\begingroup$ Have you seen GridLines already? $\endgroup$ Jul 19, 2022 at 20:14
  • $\begingroup$ @J. M., yes but I don't want the GridLines. $\endgroup$
    – Gallagher
    Jul 19, 2022 at 20:16
  • 1
    $\begingroup$ Show[ListLogLogPlot[{{1,3},{100,3}},Joined->True], ListLogLogPlot[{{1,1},{1,9}},Joined->True], ListLogLogPlot[{{3,1},{3,9}},Joined->True], ListLogLogPlot[data,Joined->True,PlotRange->{0.1,100}]] $\endgroup$
    – Bill
    Jul 19, 2022 at 20:41
  • 3
    $\begingroup$ Perhaps you too quickly discarded the suggestion of @J.M.? You can have just two vertical grid lines. $\endgroup$
    – Alan
    Jul 19, 2022 at 23:22

3 Answers 3

5
$\begingroup$

InfiniteLine can be used on plots. In this example we will use the "point, direction" form of InfiniteLine. The coordinates of the point passed to InfiniteLine must be adjusted when the plot scales are logarithmic.

For two vertical lines given by $x=1$ and $x=3$ and a horizontal line given by $y=2$ we can code

abscissas = {1, 3};
ListLogLogPlot[data, Joined -> True, PlotRange -> {0.1, 100},
 Epilog -> {InfiniteLine[{Log[#], 1}, {0, 1}] & /@ abscissas,
 InfiniteLine[{1, Log[2]}, {1, 0}]}]

enter image description here

My intuition was to use Log[10,x] for the scaling, but using base 10 was wrong. It's the natural log that gives the correct scaling.

For a diagonal line through $x,y=\{5,10\}$, code InfiniteLine[{Log[5], Log[10]}, {1, 1}]

Note that the direction used in InfiniteLine[pt,dir] is not logarithmically scaled.

$\endgroup$
2
  • $\begingroup$ Thank you @LouisB , please can you add in the same plot the horizontal line y=2? $\endgroup$
    – Gallagher
    Jul 20, 2022 at 0:36
  • $\begingroup$ It's great @LouisB , thank you so much. $\endgroup$
    – Gallagher
    Jul 20, 2022 at 0:48
7
$\begingroup$

I still believe that GridLines is convenient.

ListLogLogPlot[data, Joined -> True, PlotRange -> {0.1, 100}, 
 GridLines -> {{{1, Directive[Red, Opacity[1]]}, {3, 
     Directive[Red, Thick, Opacity[1]]}}, {{2, 
     Directive[Thick, Opacity[1]]}}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you @cvgmt. $\endgroup$
    – Gallagher
    Jul 20, 2022 at 1:30
3
$\begingroup$

Something like:

x = Table[s, {s, 0, 100, 0.1}];
y = Table[Exp[s - 1], {s, 0, 100, 0.1}];
data = Transpose@{x, y};

ListLogLogPlot[data, Joined -> True, PlotRange -> {0.1, 100}, 
 Epilog -> {Red, 
   Line[{{{Log@1, -3}, {Log@1, -2}}, {{Log@3, -3}, {Log@
        3, -2}}, {{-2.6, Log@4}, {-2.4, Log@4}}}]}]

enter image description here

If you want horizontal/vertical lines, simple make the lines longer:

ListLogLogPlot[data, Joined -> True, PlotRange -> {0.1, 100}, 
 Epilog -> {Red, 
   Line[{{{Log@1, -3}, {Log@1, 100}}, {{Log@3, -3}, {Log@3, 
       100}}, {{-2.6, Log@4}, {100, Log@4}}}]}]

enter image description here

$\endgroup$
2
  • $\begingroup$ @ Daniel Huber thank you, but sorry I wanted to draw the vertical lines x=1, x=3, not the points. $\endgroup$
    – Gallagher
    Jul 19, 2022 at 20:49
  • $\begingroup$ Simply make the lines longer. I added this to my anwer. $\endgroup$ Jul 20, 2022 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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