TransWikia.com

pgfplots: How to get coordinates, draws as a style

TeX - LaTeX Asked on April 22, 2021

enter image description here

I draw these two points

fill[cyan] (yticklabel cs:0.5) coordinate[](X2) circle(2pt); 
fill[red] (yticklabel* cs:0.5) coordinate[](X1) circle(2pt); 

So my question is: How could I have that as a style, so that I can say e.g.
begin{axis}[drawpoints, ....]
There must be something with path picture or insert path or…

It may be a curious question, but I need that for some measurement methods, and I want to be flexible where I use that method.

documentclass[a4paper]{article}
usepackage{pgfplots}

begin{document}
begin{tikzpicture}[]

begin{axis}[
clip=false, 
ylabel=yLabel,
every y tick label/.append style={draw}, 
every axis y label/.append style={draw}, 
]
addplot coordinates {(0,-500) (1,1) (2,2)};

fill[cyan] (yticklabel cs:0.5) coordinate[](X2) circle(2pt); 
fill[red] (yticklabel* cs:0.5) coordinate[](X1) circle(2pt); 
end{axis}
end{tikzpicture}
end{document}

One Answer

pgfplots has some hooks that lets you insert code at various points, see section 4.25 Miscellaneous options in the pgfplots manual.

Not entirely sure which is the best one, but before end axis seems to do the job:

documentclass[a4paper]{article}
usepackage{pgfplots}
pgfplotsset{
  drawpoints/.style={
    before end axis/.code={
      fill[cyan] (yticklabel cs:0.5) coordinate (X2) circle(2pt); 
      fill[red] (yticklabel* cs:0.5) coordinate (X1) circle(2pt); 
    }
  }
}
begin{document}
begin{tikzpicture}
begin{axis}[
drawpoints,
clip=false, 
ylabel=yLabel,
every y tick label/.append style={draw}, 
every axis y label/.append style={draw}, 
]
addplot coordinates {(0,-500) (1,1) (2,2)};


end{axis}
end{tikzpicture}
end{document}

Correct answer by Torbjørn T. on April 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP