TransWikia.com

How to shift dashed pattern in pgfplots using "dash phase" : only in the plot, not in the legend

TeX - LaTeX Asked on March 20, 2021

As mentioned in this question, one can use dash phase to shift the position of the blanks between the dashes of a plot. However, this also shifts the blanks in the legend.

Is it possible to shift the blanks only in the plot, while leaving the legend unchanged?

MWE

begin{tikzpicture}
begin{axis}[
width=3in,
height=3in
]
addplot [black, dashed]
table {%
0 0
1 1
2 0
3 -.5
};
addlegendentry{w/o dash phase}
addplot [black, dashed, dash phase = 8pt]
table {%
1 0
2 1
3 0
4 -.5
};
addlegendentry{w/ dash phase}
end{axis}
end{tikzpicture}

which gives

enter image description here

One Answer

This is the purpose of legend image post style. You can use it to reset the dash phase.

documentclass[tikz,border=3mm]{standalone}
usepackage{pgfplots}
pgfplotsset{compat=1.17}
begin{document}
begin{tikzpicture}
begin{axis}[
width=3in,
height=3in,legend image post style={dash phase=0pt},
]
addplot [black, dashed]
table {%
0 0
1 1
2 0
3 -.5
};
addlegendentry{w/o dash phase}
addplot [black, dashed, dash phase=8pt]
table {%
1 0
2 1
3 0
4 -.5
};
addlegendentryexpanded{w/ dash phase}
end{axis}
end{tikzpicture}

end{document}

enter image description here

Correct answer by user228539 on March 20, 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