Graphics, Figures & Tablespgfplots axes label in si unit

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

pgfplots axes label in si unit

Post by gallioleo »

Hi Latex Community,
i look for an way to write si units on the axes labels of pgfplots. See MWE below. The goal is to replace for instance 10^3 by 1k and so on. Ideally is this computed by siunitx.

Code: Select all

\documentclass[
        12pt,
        DIV=14,
        final
]{scrreprt}


\usepackage[english]{babel}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{graphicx}

% some pgf settings
\pgfplotsset{compat=1.6}
\pgfplotsset{grid style={dotted,gray}}
\pgfplotsset{major grid style={dashed,gray}}
\pgfplotsset{every axis plot/.append style={line width=1.25pt}}
\pgfplotstableset{comment chars=\#} 


% Start Document
\begin{document}

\begin{filecontents*}{example.csv}
1e-3, 0.1
1e-1, 0.5
2e1, 1.0
50e3, 22
\end{filecontents*}

\begin{figure}[!htp]
	\begin{tikzpicture}
		\begin{semilogxaxis}[%
			width					= \textwidth,
			grid					= both,
			legend style			= {at={(0.05,0.95)}, anchor=north west, font=\scriptsize},
			filter discard warning	= false,
			ylabel					= {Y-Label},
			xlabel					= {X-Label},
			no markers
		]
			% Dataset
			\addplot table [x index=0, y index=1, col sep=comma, unbounded coords=discard] {example.csv};
			\addlegendentry{Dataset}

		\end{semilogxaxis}
	\end{tikzpicture}
\end{figure}

\end{document}
As example a photo montage from the desired labelling:
desired_labals.png
desired_labals.png (37.01 KiB) Viewed 5725 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

pgfplots axes label in si unit

Post by gallioleo »

Hi,

in the meantime i found a solution for linear axes, but with logarithmic i still fail, cause '\tick' contains for logarithmic plots the natural logarithm, that means i must calculate e^\tick based on the context. For linear plots i can directly use \xtick.

Does anybody has an idea, how to switch the calculation based on the pgf environment?

Code: Select all

\documentclass[
        fontsize=12pt,
        DIV=14,
]{scrreprt}


\usepackage[english]{babel}
\usepackage{pgfplots}
\usepackage{graphicx}
\usepackage[binary-units=true, detect-all]{siunitx}[=v2]

% siunitx settings
%   https://latex.org/forum/viewtopic.php?f=45&t=34710
\DeclareSIUnit{\noop}{\relax}	% needed for conversion to si w/o unit \SI{100e3}{\noop}

% some pgf settings
\pgfplotsset{compat=1.17}
\pgfplotsset{
	grid style={dotted,gray},
	major grid style={dashed,gray},
	every axis plot/.append style={line width=1.25pt},
	scaled ticks=false
}

% pgf ticks to si-units
\pgfplotsset{xticklabel = {\SI[round-mode=figures,exponent-to-prefix=true,scientific-notation=engineering,zero-decimal-to-integer=true]{\tick}{\noop}}}
\pgfplotsset{yticklabel = {\SI[round-mode=figures,exponent-to-prefix=true,scientific-notation=engineering,zero-decimal-to-integer=true]{\tick}{\noop}}}



% Start Document
\begin{document}


\begin{figure}[!htp]
	\begin{tikzpicture}
		\begin{semilogxaxis}[%
			width					= \textwidth,
			grid					= both,
			legend style			= {at={(0.05,0.95)}, anchor=north west, font=\scriptsize},
			filter discard warning	= false,
			ylabel					= {Y-Label},
			xlabel					= {X-Label},
			no markers
		]
			% Dataset
			\addplot table {
				1e-3 0.1
				1e-1 0.5
				2e1 1.0
				50e3 22
			};
			\addlegendentry{Dataset}

		\end{semilogxaxis}
	\end{tikzpicture}
\end{figure}


\begin{figure}[!htp]
	\begin{tikzpicture}
		\begin{axis}[%
			width					= \textwidth,
			grid					= both,
			legend style			= {at={(0.05,0.95)}, anchor=north west, font=\scriptsize},
			filter discard warning	= false,
			ylabel					= {Y-Label},
			xlabel					= {X-Label},
			no markers
			]
			% Dataset
			\addplot table {
				10000 1
				20000 2
				30000 3
				40000 4
			};
			\addlegendentry{Dataset}

		\end{axis}
	\end{tikzpicture}
\end{figure}


\end{document}

Thanks for your help.
latexplots
Posts: 1
Joined: Thu Feb 10, 2022 11:21 pm

pgfplots axes label in si unit

Post by latexplots »

Just add labels with xtick and xtiklabels, replacing original the number with the ones you want.

Code: Select all

\begin{axis}
  [xtick={-6,-4,-2,0,2,4,6},
   xticklabels={A,B,C,D,E,F,G}]
   \addplot[
You can easily set up you graphics using the Tool in

https://latexplots.netlify.app/

Best regards
gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

pgfplots axes label in si unit

Post by gallioleo »

Nice page you mention, but i search for an general solution. Arbitrary waveform data in, and axes in si. But now i have an solution:

Code: Select all

\documentclass[
        fontsize=12pt,
        DIV=14
]{scrreprt}


\usepackage[english]{babel}
\usepackage{pgfplots}
\usepackage{graphicx}
\usepackage[binary-units=true, detect-all]{siunitx}[=v2]


% some pgf settings
\pgfplotsset{compat=1.17}
\pgfplotsset{
	grid style={dotted,gray},
	major grid style={dashed,gray},
	every axis plot/.append style={line width=1.25pt}
}


% Compute e^x
\newcommand{\fpuexp}[1]{\pgfkeys{/pgf/fpu=true, /pgf/fpu/output format=fixed}\pgfmathparse{pow(2.718281828,#1)}\pgfkeys{/pgf/fpu=false}\let\fpuexpresult\pgfmathresult}
% settings for siunitx v2
%   http://www.nagel-net.de/Latex/DOKU/siunitx.pdf
%   https://latex.org/forum/viewtopic.php?f=45&t=34710
\DeclareSIUnit{\noop}{\relax}
\newcommand{\siSetLogAxes}{\fpuexp{\tick}\SI[round-mode=places,exponent-to-prefix=true,scientific-notation=engineering,zero-decimal-to-integer=true,round-precision=0]{\fpuexpresult}{\noop}}
\newcommand{\siSetLinAxes}{\SI[round-mode=figures,exponent-to-prefix=true,scientific-notation=engineering,zero-decimal-to-integer=true,round-precision=2]{\tick}{\noop}}
% setup pgfplots environment
\pgfplotsset{%
	% prepare for convert
	scaled ticks=false,
	log ticks with fixed point,
	% x-lin, y-lin
	every linear axis/.append style={xticklabel=\siSetLinAxes},
	every linear axis/.append style={yticklabel=\siSetLinAxes},
	% x-lin, y-log
	every semilogy axis/.append style={xticklabel=\siSetLinAxes},
	every semilogy axis/.append style={yticklabel=\siSetLogAxes},
	% x-log, y-lin
	every semilogx axis/.append style={xticklabel={\siSetLogAxes}},
	every semilogx axis/.append style={yticklabel = {\siSetLinAxes}},
	% x-log, y-log
	every log axis/.append style={xticklabel={\siSetLogAxes}},
	every log axis/.append style={yticklabel={\siSetLogAxes}}
}


% Start Document
\begin{document}

\begin{figure}[!htp]
	\begin{tikzpicture}
		\begin{semilogxaxis}[%
			width					= \textwidth,
			grid					= both,
			legend style			= {at={(0.05,0.95)}, anchor=north west, font=\scriptsize},
			filter discard warning	= false,
			ylabel					= {Y-Label},
			xlabel					= {X-Label},
			no markers
			]
			% Dataset
			\addplot table {
				1e-3 -100000
				1e-1 1
				2e1 100000
				50e3 200000
			};
			\addlegendentry{Dataset}

		\end{semilogxaxis}
	\end{tikzpicture}
\end{figure}


\begin{figure}[!htp]
	\begin{tikzpicture}
		\begin{axis}[%
			width					= \textwidth,
			grid					= both,
			legend style			= {at={(0.05,0.95)}, anchor=north west, font=\scriptsize},
			filter discard warning	= false,
			ylabel					= {Y-Label},
			xlabel					= {X-Label},
			no markers
			]
			% Dataset
			\addplot table {
				10000 1
				20000 2
				30000 3
				40000 4
			};
			\addlegendentry{Dataset}

		\end{axis}
	\end{tikzpicture}
\end{figure}

\end{document}

gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

pgfplots axes label in si unit

Post by gallioleo »

Hi, now i have an addtional question, how can i aggregate settings from pgfplots.

I like to define this as own switch:
\pgfplotsset{%
% prepare for convert
scaled ticks=false,
log ticks with fixed point,
% x-lin, y-lin
every linear axis/.append style={xticklabel=\siSetLinAxes},
every linear axis/.append style={yticklabel=\siSetLinAxes},
% x-lin, y-log
every semilogy axis/.append style={xticklabel=\siSetLinAxes},
every semilogy axis/.append style={yticklabel=\siSetLogAxes},
% x-log, y-lin
every semilogx axis/.append style={xticklabel={\siSetLogAxes}},
every semilogx axis/.append style={yticklabel = {\siSetLinAxes}},
% x-log, y-log
every log axis/.append style={xticklabel={\siSetLogAxes}},
every log axis/.append style={yticklabel={\siSetLogAxes}}
}


as switch f.e. axes si.

that i can write as optional argument an not use the global options.
\begin{figure}[!htp]
\begin{tikzpicture}
\begin{semilogxaxis}[%
no markers,
si axes
]
....
\end{semilogxaxis}
\end{tikzpicture}
\end{figure}


Anybody an idea?
Post Reply