rlkamalapurkar / bodeplot

LaTeX package to plot Bode diagrams
LaTeX Project Public License v1.3c
6 stars 1 forks source link

Use SI prefix for axis #8

Closed AndreaDiPietro92 closed 1 year ago

AndreaDiPietro92 commented 1 year ago

it would be very useful to be able to use the SI prefix (milli , micro, kilo, mega) for the x-axis.

This is what i made with pgfplots (using this link https://tex.stackexchange.com/questions/183225/engineering-notation-scientific-as-tick-labels-with-pgfplots-maybe-via-siunit) and using the option of siunitx:

\sisetup{
    exponent-mode = engineering,
    prefix-mode = combine-exponent,
    drop-zero-decimal,
    round-mode = places,
}%

image

rlkamalapurkar commented 1 year ago

I am unable to reproduce your plot, but the package is already set up to transfer pgf keys to various environments. For example, if you are using a command of the form xticklabel=\mycommand{\tick} to generate the plot you showed, you can do that with

\documentclass{standalone}
\usepackage{bodeplot,siunitx}
\usepgfplotslibrary{units}
\begin{document}
\begin{BodeMagPlot}[
  axes/{xticklabel=\mycommand{\tick}}
  ]{0.01}{100}
  \addBodeZPKPlots{magnitude}{%
    z/{0,{-0.1,-0.5},{-0.1,0.5}},
    p/{{-0.5,-10},{-0.5,10}},
    k/10%
  }
\end{BodeMagPlot}
\end{document}

In the code above, the xticklabel=\mycommand{\tick} option is passed through to the semilogxaxis environment, and then pgf does its thing as usual.

Is this what you were looking for?

AndreaDiPietro92 commented 1 year ago

Yes, I was looking for something like this but doesent work. This is my code

\sisetup{
    exponent-mode = engineering,
    prefix-mode = combine-exponent,
    drop-zero-decimal,
    round-mode = places,
}%
\DeclareSIUnit{\noop}{\relax}% added for get no unit
\newcommand{\mynum}[1]{ % for log axis
        % =\pgfmathparse{e^\tick}\pgfmathresult : ! Dimension too large. ; - use exp(x)
        \pgfkeys{/pgf/fpu}% else dimension too large!
        \pgfmathparse{exp(#1)}%
        \edef\tmp{\pgfmathresult}%
        % \pgfmathprintnumberto{\pgfmathresult}{\tmp}% nope, gives 1Y5.0012e-1]
        \pgfmathfloattofixed{\pgfmathresult}% also outputs in \pgfmathresult
        \edef\tmp{\pgfmathresult}%
        \pgfkeys{/pgf/fpu=true}%
        \typeout{tick #1, tmp \tmp}%
        \qty{\pgfmathresult}{\noop} %cambiare \noop con la quantità desiderata \V ex
    }

\newcommand{\mynume}[1]{ %for no log axis
        \pgfkeys{/pgf/fpu}% else dimension too large!
        \pgfmathparse{#1}%
        \edef\tmp{\pgfmathresult}%
        % \pgfmathprintnumberto{\pgfmathresult}{\tmp}% nope, gives 1Y5.0012e-1]
        \pgfmathfloattofixed{\pgfmathresult}% also outputs in \pgfmathresult
        \edef\tmp{\pgfmathresult}%
        \pgfkeys{/pgf/fpu=true}%
        \typeout{tick #1, tmp \tmp}%
        \qty{\pgfmathresult}{\dB} %cambiare \noop con la quantità desiderata \dB ex
    }

\begin{BodeMagPlot}[axes/xticklabel=\mynum{\tick},
tikz/{%
frequency unit=Hz%
},
  ]{0.01}{100000}
  \addBodeZPKPlots{magnitude}{
    z/{0,{-0.1,-0.5},{-0.1,0.5}},
    p/{{-0.5,-10},{-0.5,10}},
    k/{10}%
  }
\end{BodeMagPlot}

but i get this image

rlkamalapurkar commented 1 year ago

This works, just need to use base 10 logarithm. I just changed \pgfmathparse{exp(#1)}% in your macro to \pgfmathparse{10^#1}%

\documentclass{standalone}
\usepackage{bodeplot,siunitx,pgfplots}
    \pgfplotsset{compat=1.18}

\sisetup{
    exponent-mode = engineering,
    prefix-mode = combine-exponent,
    drop-zero-decimal,
    round-mode = places,
}

\DeclareSIUnit{\noop}{\relax} % no unit

\newcommand{\mynum}[1]{ % for log axis
    \pgfkeys{/pgf/fpu=true}
    \pgfmathparse{10^#1}
    \pgfmathfloattofixed{\pgfmathresult}
    \pgfkeys{/pgf/fpu=false}
    \qty{\pgfmathresult}{\noop}
}

\newcommand{\mynume}[1]{ % for no log axis
    \pgfkeys{/pgf/fpu=true}
    \pgfmathparse{#1}
    \pgfmathfloattofixed{\pgfmathresult}
    \pgfkeys{/pgf/fpu=false}
    \qty{\pgfmathresult}{\noop}
}

\begin{document}
    \begin{BodeMagPlot}[
        axes/{
            xticklabel=\mynum{\tick},
            yticklabel=\mynume{\tick}
        },
        tikz/{frequency unit=Hz},
    ]{0.01}{100000}
        \addBodeZPKPlots{magnitude}{
            z/{0,{-0.1,-0.5},{-0.1,0.5}},
            p/{{-0.5,-10},{-0.5,10}},
            k/{1}%
        }
    \end{BodeMagPlot}
\end{document}
AndreaDiPietro92 commented 1 year ago

Thank you. It's perfect! You should put this stuff in your next release. 😃

AndreaDiPietro92 commented 1 year ago

ps. Be careful, there is a different placement of numbers between my implementation and yours. In mine the numbers are centered, in yours the numbers are placed further to the right of the tick. Mine: image Yours: image

rlkamalapurkar commented 1 year ago

Weird, I wonder why! I will try to find out.

As for including it in the next release, if I can think of a way to do it that is flexible and easy to use, I will add it. At the very least, I can put this example in the package documentation.