1
$\begingroup$

This question already has an answer here:

I have a list of numbers which I have named "result" and which I modify according to certain rules. At the end of the routine, I plot the list with the command:

ListPlot[result, PlotRange -> Automatic]

I would like to add the horizontal line y=1 (in red) to my plot. Is there a simple way to modify the previous command in order to attain such an effect?

Thanks a bunch for your insightful replies!

$\endgroup$

marked as duplicate by Kuba Jun 7 '18 at 6:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ Try InfiniteLine as a Prolog. $\endgroup$ – Alan Jun 7 '18 at 1:22
  • 1
    $\begingroup$ Look up Gridlines $\endgroup$ – Michael E2 Jun 7 '18 at 1:28
  • $\begingroup$ @Alan: Would you be so kind as to elaborate? $\endgroup$ – Jamai-Con Jun 7 '18 at 1:57
  • $\begingroup$ I tried "ListPlot[result, PlotRange -> Automatic, Prolog -> {Red, InfiniteLine[{0, 1}, {2, 1}]}]", but I am not getting the desired effect $\endgroup$ – Jamai-Con Jun 7 '18 at 1:58
  • 2
    $\begingroup$ ListPlot[RandomReal[2, {100, 2}], PlotRange -> Automatic, Prolog -> {Red, InfiniteLine[{0, 1}, {1000, 1}]}] $\endgroup$ – OkkesDulgerci Jun 7 '18 at 3:03
3
$\begingroup$

An issue with both InfiniteLine and Gridlines is that the inserted line can significantly overshoot the vertical axis:

enter image description here

There may be a way to correct this [EDIT: There is—see Carl Woll's comment below]; but to avoid having to bother, you could use Show to combine your plot with another plot of y = 1:

Show[ListPlot[RandomReal[2, {100, 2}]], Plot[1, {x, 0, 2}, PlotStyle -> Red]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ The GridLines extend over the whole plot range, which due to the PlotRangePadding->Automatic default setting extends to about -.05. Use PlotRangePadding->0 to avoid this. $\endgroup$ – Carl Woll Jun 7 '18 at 5:13
2
$\begingroup$

I also believe GridLines is easier

ListPlot[RandomReal[2, {100, 2}], GridLines -> {None, {1}}, GridLinesStyle -> Red]

Mathematica graphics

$\endgroup$

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