3

I'm plotting spectral data, sometimes with logarithmic axes and sometimes linear but with values in dB. The usual way for plotting these kind of diagrams (at least in my field) is to have all the lines start at the lower part of the graph, despite their values (negative, positive). For logarithmic comb plots, this can easily be achieved using log origin = infty. However, how can I use something like this for plots with negative values (in linear scale)? Currently, I'm shifting all points up by the smallest value and then shifting all yticklabels. However, this is cumbersome and not really an automatic solution. I have to find the smallest value by hand in order to find the optimal shift. Can this be improved? I've found this answer, but this is about logarithmic plots. I don't really understand the code, so I don't know if this can easily be modified.

Here is a MWE with what I'm trying to achieve:

\documentclass{article}

\usepackage{pgfplots} 

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            [
                % I'm using this:
                yticklabel = {\pgfmathparse{\tick-130}\pgfmathprintnumber{\pgfmathresult}},
                y filter/.expression = {y + 130},
                ymin = 0
                % I would like to have this:
                % comb origin = -infty % or something
            ]
            \addplot[ycomb] coordinates {
                (1, -6)
                (2, -80)
                (3, -85)
                (4, -120)
                (5, -120)
                (6, -120)
                (7, -120)
                (8, -120)
                (9, -120)
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

And the result looks like this: Sample Output

If I don't do this shift, then the result looks like this (since all combs start at y = 0): Wrong output

  • I read this question several times and still do not understand what you are asking. Could you please consider making your question clearer? E.g. by adding a sketch of the target output? (The way I read your question you already have achieved your target output.) – marmot Mar 23 at 1:13
  • Yes, what I am showing is the target output. I would like to have a key for linear comb plots to change the starting line (baseline?) for them, similar to log origin. I will add a picture of the unwanted output to the question. – pschulz Mar 23 at 9:17
1
+50

I think that you can use one "very big number" as replacement of infinity. Here I use 1000 for the value of \verybignumber. Then I apply your code with \verybignumber in place of 130 and I let pgfplots to set ymin.

\documentclass[border=7pt]{standalone}
\usepackage{pgfplots}
\def\verybignumber{1000}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}
      [
        yticklabel = {\pgfmathparse{\tick-\verybignumber}\pgfmathprintnumber{\pgfmathresult}},
        y filter/.expression = {\verybignumber+y},
      ]
      \addplot[ycomb] coordinates {
        (1, -6)
        (2, -80)
        (3, -85)
        (4, -120)
        (5, -120)
        (6, -120)
        (7, -120)
        (8, -120)
        (9, -120)
      };
    \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

  • Ah, interesting. I thought I tried something like this, but the explicit setting of ymin messes things up. I used this because the lines started 'hovering' over the lower axis line, but with a number very different from the lowest value this works. Thanks! – pschulz Mar 24 at 20:27
  • I made a key of this for my settings: comb origin infty/.style = { yticklabel = {\pgfmathparse{\tick-1000}\pgfmathprintnumber{\pgfmathresult}}, y filter/.expression = {y + 1000} } – pschulz Mar 24 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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