[Tex/LaTex] tikz/pgfplot — always same distance between marks

pgfplotsplottikz-pgf

Is there a way to get for all tikz-plots in one LaTeX document the same distance between the plot marks, ie the distance between the marks should be independent of sample size. It would be really great if this change wouldn't interfere with the legend like change the position of the marker in the legend etc. Thanks.

Edit: Unfortunately the distance of all data points (not in the mini example but for the real data) is not the same therefore it would be nice if I could define distance between the marks in cm.

\documentclass[tikz,border=1mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings}
\pgfplotsset{compat=1.11}
%\tikzset{mark/.style={
%        decoration={
%            markings,
%            mark= between positions 0 and 1 step 5mm with
%                {
%                %\node[circle,inner sep=2pt,fill=blue]{};
%            },
%        },
%        postaction={decorate}
%    }
%}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\addplot[
  blue,
  domain=0:3,
  samples=100,
  mark=*
  ]
{exp(x)};
\addlegendentry{exp(x)};

\addplot[
  red,
  domain=0:3,
  samples=20,
  mark=diamond
  ]
{4*x};
\addlegendentry{4x};

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

edit: Is is possible to pass to tikz such options like mark size or mark options={solid} ?

    \addplot [color=black,loosely dotted,line width=1.5pt, mark=*,mark size=3pt, mark=diamond,mark options={solid}]

edit2: I found out how I can set the marks solid in the legend but how can I do the same in for the plot? Any ideas how to get the plot marks solid in the actual plot?

\tikzset{
  nomorepostactions/.code={\let\tikz@postactions=\pgfutil@empty},
  mymark/.style={decoration={markings,
    mark= between positions 0 and 1 step (1/11)*\pgfdecoratedpathlength with{%
        \pgfuseplotmark{#1},%
      },  
    },
    postaction={decorate},
    /pgfplots/legend image post style={mark=#1,every path/.append style={nomorepostactions}},
  },
  every mark/.append style={solid,mark size=3},
}

enter image description here

Best Answer

Similar to Harish's answer but removes the need to plot twice. You basically need to stop the postaction reaching to the legend pictures because that is what is causing the funky mark placement. And that you can do with our good ol' answer to Applying a postaction to every path in TikZ

\documentclass[]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{decorations.markings}
\makeatletter
\tikzset{
  nomorepostactions/.code={\let\tikz@postactions=\pgfutil@empty},
  mymark/.style 2 args={decoration={markings,
    mark= between positions 0 and 1 step (1/11)*\pgfdecoratedpathlength with{%
        \tikzset{#2,every mark}\tikz@options
        \pgfuseplotmark{#1}%
      },  
    },
    postaction={decorate},
    /pgfplots/legend image post style={
        mark=#1,mark options={#2},every path/.append style={nomorepostactions}
    },
  },
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend style={at={(0.5,0.95)}, anchor=north},
  legend entries={$\exp(x)$,$10\sin(5x)$},
]
\addplot[blue,dashed, domain=0:3,samples=15,mymark={o}{solid}]{exp(x)};
\addplot[red,domain=0:3, dashdotted,samples=200,
           mymark={diamond*}{draw=black,fill=yellow,solid}]{10*sin(deg(5*x))};
\end{axis}
\end{tikzpicture}
\end{document}

You can also add yet another parameter for how many marker you want to have by dividing the total path length to a fixed number. Here I went with 11. (replacing it with a distance is what you wish probably but I put here as an alternative)

enter image description here

with 1 cm mark distance

enter image description here

EDIT For the added style question, the code now gives

enter image description here