Graphics, Figures & TablesComparison plot diagram

Information and discussion about graphics, figures & tables in LaTeX documents.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Comparison plot diagram

Post by Cham »

Suppose you have two (or more) values that you want to compare, one experimental value and one theoretical value (both with their uncertainty). You would want to plot them on the same horizontal axis with their uncertainty, to see if they are compatible (i.e. if they overlap, considering the uncertainty). How could we do that in LaTeX, without importing a picture ?

I searched the internet and the Tikz examples archive and didn't found anything that do this. Any idea about how to achieve a comparison diagram in LaTeX ? Is there any dedicated package for this ?

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Comparison plot diagram

Post by Johannes_B »

To be honest, i don't know what you mean with a comparison diagram. If you really have two datapoints, what's the point of a diagram?

There are scatter plots, bar plots, linear plots ... all for different data and their representation.

pgfplots is a dedicated package for plotting data, the website ha many examples.
There are other possibilities, TiKz directly, PSTricks, Asymptote, Metapost, the latter two including importing an image that was first created by a helper program.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Comparison plot diagram

Post by Cham »

Comparison diagrams are VERY basic, and yet appear to be very uncommon on the internet. I never do these kind of diagrams myself, since they are pretty useless, in a sense. However :

at the scientific institution where I'm teaching physics, we have to ask the students to do a comparison diagram in their laboratory report. They have to graphically show at least two values on a simple horizontal axis (there is no vertical axis in these diagrams). So it's looking like this (crudely done by hand) :
diagram.jpg
diagram.jpg (13.94 KiB) Viewed 7201 times
Usually, the students do this with Excel, or in Word.

EDIT : the problem with the packages documention is that they give tons of fancy unrealistic examples but rarely give simple common examples. I'm still unable to understand how to use the pgfplotstable package just to plot a few points on an horizontal axis, without a vertical axis.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Comparison plot diagram

Post by Cham »

Here's a minimal example :

Code: Select all

\documentclass[10pt,twoside]{revtex4}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{width=7cm}
\usepackage[separate-uncertainty=true]{siunitx}
\sisetup{output-decimal-marker = {,}}

\begin{document}

\begin{center}
	\begin{tikzpicture}
	\begin{axis}[
		grid=none,
		xlabel=$\rho \; (\si{\g/\cm^3})$,
		yticklabels=none,
		title={Density comparison},
		xmin=7.0,
		xmax=8.0,
		legend pos=outer north east,
	]
	\addplot+[error bars,x dir=both,x explicit]
	coordinates {
		(7.90, 0) +- (0.09, 0)
	};
	\addlegendentry{$\rho_1 = \SI{7,90 +- 0,09}{\g/\cm^3}$};
	\addplot+[error bars,x dir=both,x explicit]
	coordinates {
		(7.5, 1) +- (0.4, 0)
	};
	\addlegendentry{$\rho_2 = \SI{7,5 +- 0,4}{\g/\cm^3}$};
	\addplot+[error bars,x dir=both,x explicit]
	coordinates {
		(7.82, 2) +- (0.05, 0)
	};
	\addlegendentry{$\rho_3 = \SI{7,82 +- 0,05}{\g/\cm^3}$};
	\end{axis}
	\end{tikzpicture}
\end{center}

\end{document}
Preview :
comparison.jpg
comparison.jpg (19.57 KiB) Viewed 7188 times
It has several issues :

1. The color should be all black. Even the small dots.
2. Each point should have a mathematical label.
3. The vertical axis shouldn't be there.
4. The vertical size should be smaller.
5. The equal sign should be aligned in the legend, without the small dot symbol.

The HUGE documentation isn't clear about how to change these.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Comparison plot diagram

Post by Cham »

This is *almost* right :

Code: Select all

\documentclass[10pt,twoside]{revtex4}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{width=7cm}
\usepackage[separate-uncertainty=true]{siunitx}
\sisetup{output-decimal-marker = {,}}

\begin{document}

\begin{center}
	\begin{tikzpicture}
	\begin{axis}[
		scale only axis=true,
		width=10cm,
		height=2cm,
		grid=none,
		xlabel=$\rho \; (\si{\g/\cm^3})$,
		ytick=\empty,
		title={Density comparison},
		xmin=7.2,
		xmax=8.0,
		ymin=-1,
		ymax=3,
		legend pos=outer north east,
		legend style={font=\footnotesize}
	]
	\addplot+[color=black,mark=o,mark size=2pt,error bars,x dir=both,x explicit]
	coordinates {
		(7.70, 0) +- (0.09, 0)
	};
	\addlegendentry{$\rho_1 = \SI{7,70 +- 0,09}{\g/\cm^3}$};
	\addplot+[color=black,mark=square,mark size=2pt,error bars,x dir=both,x explicit]
	coordinates {
		(7.5, 1) +- (0.2, 0)
	};
	\addlegendentry{$\rho_2 = \SI{7,5 +- 0,2}{\g/\cm^3}\hphantom{00}$};
	\addplot+[color=black,mark=*,mark size=2pt,error bars,x dir=both,x explicit]
	coordinates {
		(7.82, 2) +- (0.05, 0)
	};
	\addlegendentry{$\rho_3 = \SI{7,82 +- 0,05}{\g/\cm^3}$};
	\end{axis}
	\end{tikzpicture}
\end{center}

\end{document}
Preview :
diagram.jpg
diagram.jpg (18.15 KiB) Viewed 7171 times
However, I still have to change the dot color.

Also, I would prefer to remove the frame around the legend, and center it verticaly to the main diagram. Is this possible ? I didn't found any help in the documentation.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Comparison plot diagram

Post by Stefan Kottwitz »

Hi Cham,

very good minimal example! It's joyful to test it.

You can remove the y axis line by

Code: Select all

axis y line = none
and you can center the legend at the right side by

Code: Select all

legend style={at={(1,0.5)}, anchor=west, draw=none}
bot as options to the axis environment.

I got no quick idea about the colors, but if it's still important I could figure it out.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Comparison plot diagram

Post by Cham »

Thanks Stefan.

Here's what I got. I think this is it ! 8-)

Code: Select all

\documentclass[10pt,twoside,nofootinbib]{revtex4}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{width=7cm}
\usepackage[separate-uncertainty=true]{siunitx}
\sisetup{output-decimal-marker = {,}}

\begin{document}

	\begin{figure}
	\centering
	\begin{tikzpicture}
	\begin{axis}[
		scale only axis=true,
		width=10cm,
		height=2cm,
		grid=none,
		%xtick align=center,
		xlabel=$\rho \; (\si{\g/\cm^3})$,
		ytick=\empty,
		title={Density comparison},
		xmin=7.2,
		xmax=8.0,
		ymin=-1,
		ymax=3,
		axis x line = bottom,
		axis y line = none,
		legend style={at={(1, 0.5)},anchor=west,font=\footnotesize,draw=none},
	]
	\addplot+[color=black,mark options={mark=*,mark size=2pt,fill=black,scale=1},error bars,x dir=both,x explicit]
	coordinates {
		(7.5, 2) +- (0.2, 0)
	};
	\addlegendentry{$\rho_1 = \SI{7,5 +- 0,2}{\g/\cm^3}\hphantom{00}$};
	\addplot+[color=black,mark options={mark=square*,mark size=2pt,fill=black,scale=1},error bars,x dir=both,x explicit]
	coordinates {
		(7.45, 1) +- (0.09, 0)
	};
	\addlegendentry{$\rho_2 = \SI{7,45 +- 0,09}{\g/\cm^3}$};
	\addplot+[color=black,mark options={mark=triangle*,mark size=2pt,fill=black,scale=1.2},error bars,x dir=both,x explicit]
	coordinates {
		(7.82, 0) +- (0.04, 0)
	};
	\addlegendentry{$\rho_3 = \SI{7,82 +- 0,04}{\g/\cm^3}$};
	\end{axis}
	\end{tikzpicture}
		\caption{An example of comparison diagram.}
	\end{figure}

\end{document}
Preview :
diagram2.jpg
diagram2.jpg (19.59 KiB) Viewed 7162 times
Now, I wonder if the code could be optimized in some way.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Comparison plot diagram

Post by Cham »

Stefan,

in this case, I'm unable to show the secondary ticks, and to make the vertical major grid to be black and dashed. What is wrong in my code ?

I would also like to make the ticks all black, instead of gray.

Code: Select all

\documentclass[10pt,twoside,nofootinbib]{revtex4}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{width=7cm}
\usepackage[separate-uncertainty=true]{siunitx}
\sisetup{output-decimal-marker = {,}}

\begin{document}

	\begin{figure}
	\centering
	\begin{tikzpicture}
	\begin{axis}[
			scale only axis=true,
			width=10cm,
			height=2.5cm,
			xlabel=$\rho \, (\si{\g/\cm^3})$,
			ytick=\empty,
			title={Density comparison},
			xmin=7.2,
			xmax=7.8,
			ymin=0,
			ymax=3,
			xmajorgrids=true,
		%	x tick style={color=black}
		%	major grid style={dashed}
			minor tick num=1,
			%xtick align=center,
			legend style={at={(1.26, 0.5)},anchor=center,font=\footnotesize,draw=none},
		]
	\addplot+[color=black,mark options={mark=*,mark size=2pt,fill=black,scale=1},error bars,x dir=both,x explicit]
	coordinates {
		(7.5, 2) +- (0.2, 0)
	};
	\addlegendentry{$\rho_1 = \SI{7,5 +- 0,2}{\g/\cm^3}\hphantom{00}$};
	\addplot+[color=black,mark options={mark=square*,mark size=2pt,fill=black,scale=1},error bars,x dir=both,x explicit]
	coordinates {
		(7.45, 1) +- (0.09, 0)
	};
	\addlegendentry{$\rho_2 = \SI{7,45 +- 0,09}{\g/\cm^3}$};
	\end{axis}
	\end{tikzpicture}
		\caption{An example of comparison diagram.}
	\end{figure}

\end{document}
EDIT : Using \pgfplotsset{width=7cm,every tick/.style={black},major grid style={black,dashed}} in the preamble solves the ticks color and vertical grid rendering. But this is global. It doesn't help if we want to change the rendering locally (for each plot).

This pgfplots package feels stupid to me. The commands are so non-intuitive and ackward to use. And the documentation is not helping much with all its fancy and complicated examples.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Comparison plot diagram

Post by Johannes_B »

Use pgfplotsset inside the figure environment, that keeps its scope limited.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Comparison plot diagram

Post by Cham »

I just tried. It doesn't work (do nothing), whatever where I put it.

EDIT 1 : Hmm, weird. It's working in a small MWE example, but not in my main document.

EDIT 2 : It's working only if I use \pgfplotsset{major grid style={color=black,dotted}}, instead of \pgfplotsset{major grid style={black,dotted}}, while the second command line works in the MWE code.

I suspect an interaction with some other package.
Post Reply