Graphics, Figures & Tablespgfplots | Additional Ticks on Abscissa

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
thinkpadT
Posts: 25
Joined: Wed Sep 26, 2012 5:46 pm

pgfplots | Additional Ticks on Abscissa

Post by thinkpadT »

Hi everyone,

I have plotted a spectrum showing "Wavelength" vs "Diffraction Efficiency" and wanted to draw vertical lines indicating some special wavelengths (as shown in the attached image). Here is my written code followed by the data (used "lambda-au.txt").

Code: Select all

\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
	\begin{axis} [
  grid =major,
  %grid style={dashed},
% thick,
xlabel= Wavelength  $\lambda $ \lbrack $ nm $\rbrack ,
%xtick = {3,5,...,31},
xmax=210,
xmin=20,
ylabel=Diffraction Efficiency, % \lbrack a.u.\rbrack,x
%yticklabels= , %supress the ylabe numbering
%ymax=9,
ymin=0,
mark = none,
legend style = {legend pos=north east,font=\tiny} %place the legend box in the right place  at ={(0.85, 0.65)}
  ]

  \pgfplotstableread{lambda-au.txt} 
  \teff      %effs=efficiencies
  \addplot [color = blue, mark=none, ] table[y=one] from \teff;
  \addlegendentry{m = -1};
    \addplot [color = violet, mark=none]  table[y=two] from \teff;
   \addlegendentry{m = -2};
      \addplot [color = red, mark=none]  table[y=three] from \teff;
  \addlegendentry{m = -3};
  \addplot [color = orange, mark=none]  table[y=four] from \teff;
  \addlegendentry{m = -4};
  \addplot [color = green, mark=none]  table[y=five] from \teff;
  \addlegendentry{m = -5};
     \end{axis}
\end{tikzpicture}
\end{document}
eff-800.jpg
eff-800.jpg (46.5 KiB) Viewed 10988 times
Until now I couldn't find a useful method to realize my ideas. Any help from you guys would be appreciated very much.

Thx in advance.
-Fawad
Attachments
lambda-au.txt
(13.85 KiB) Downloaded 521 times
Last edited by localghost on Thu Jan 10, 2013 9:11 pm, edited 4 times in total.

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

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

pgfplots | Additional Ticks on Abscissa

Post by cgnieder »

It would be easier to answer if you'd provided a complete (i.e. compilable) example. Maybe accessing the axis coordinate system (see section 4.16 Custom Annotations of the pgfplots manual) could be a way?

Code: Select all

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
 \begin{axis}
  \addplot { x^2 } ;
  \draw (axis cs:-1,-2) --++(axis direction cs:0,20) ;
  \draw[red] (rel axis cs:.25,0) -- (rel axis cs:.25,1) ;
 \end{axis}
\end{tikzpicture}
\end{document}

\end{document}
Regards
site moderator & package author
feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

pgfplots | Additional Ticks on Abscissa

Post by feuersaenger »

Hi Fawad,

the approach of clemens can be used here, but pgfplots has support for this use-case:

pgfplots comes with its "extra x ticks" feature which allows to add special ticks at designated positions. You can configure grid lines for these special ticks and special styles for the associated tick labels.

With this feature, a possible answer could be

Code: Select all

\documentclass[a4paper]{article}

\usepackage{pgfplots}

\pgfplotsset{compat=newest}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\begin{axis} [
	extra x ticks={75,115},
	extra x tick style={
		grid style={thick,black,},
		xtick align=outside,
		tick style={thick,black,},
		xticklabel style={
			font=\small,
		},
	},
	grid =major,
	%grid style={dashed},
	% thick,
	xlabel= Wavelength  $\lambda $ \lbrack $ nm $\rbrack ,
	%xtick = {3,5,...,31},
	xmax=210,
	xmin=20,
	ylabel=Diffraction Efficiency, % \lbrack a.u.\rbrack,x
	%yticklabels= , %supress the ylabe numbering
	%ymax=9,
	ymin=0,
	mark = none,
	legend style = {legend pos=north east,font=\tiny} %place the legend box in the right place  at ={(0.85, 0.65)}
  ]

  \pgfplotstableread{lambda-au.txt}
  \teff      %effs=efficiencies
  \addplot [color = blue, mark=none, ] table[y=one] from \teff;
  \addlegendentry{m = -1};
	\addplot [color = violet, mark=none]  table[y=two] from \teff;
   \addlegendentry{m = -2};
	  \addplot [color = red, mark=none]  table[y=three] from \teff;
  \addlegendentry{m = -3};
  \addplot [color = orange, mark=none]  table[y=four] from \teff;
  \addlegendentry{m = -4};
  \addplot [color = green, mark=none]  table[y=five] from \teff;
  \addlegendentry{m = -5};
	 \end{axis}
\end{tikzpicture}
\end{document}
P.png
P.png (21.75 KiB) Viewed 10977 times
The key tick style applies only to the small tick lines, for which xtick align=outside applies.

Best regards

Christian
thinkpadT
Posts: 25
Joined: Wed Sep 26, 2012 5:46 pm

Re: pgfplots | Additional Ticks on Abscissa

Post by thinkpadT »

Hi cgnieder,
i think i forgot to copy the first few lines of my code; sorry for the inconvinience. And thanks for the suggested solution.


Hi feuersaenger,

thank you very much, this looks very nice. thats what i was searching for. Is it possible to put the labels on the upper x-axis? And additionally, is it possible to write on labels for example 160 H5 at 160 nm?

Thank you in everyone helping :)
have a good one.
-Fawad
feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

pgfplots | Additional Ticks on Abscissa

Post by feuersaenger »

Hi,

yes, you can use xticklabel pos=right to place it at the upper limit and you can use xticklabel=CODE to format the label to your needs.

Here is an example:

Code: Select all


    \documentclass[a4paper]{article}

    \usepackage{pgfplots}

    \pgfplotsset{compat=newest}
    \begin{document}
    \thispagestyle{empty}
    \begin{tikzpicture}
    \begin{axis} [
            extra x ticks={75,115},
            extra x tick style={
                    grid style={thick,black,},
                    xtick align=outside,
                    tick style={thick,black,},
                    xticklabel style={
                            font=\small,
                    },
					% ------ CF
					xticklabel pos=right,
					xticklabel={$\pgfmathprintnumber{\tick}$ H5},
            },
            grid =major,
            %grid style={dashed},
            % thick,
            xlabel= Wavelength  $\lambda $ \lbrack $ nm $\rbrack ,
            %xtick = {3,5,...,31},
            xmax=210,
            xmin=20,
            ylabel=Diffraction Efficiency, % \lbrack a.u.\rbrack,x
            %yticklabels= , %supress the ylabe numbering
            %ymax=9,
            ymin=0,
            mark = none,
            legend style = {legend pos=north east,font=\tiny} %place the legend box in the right place  at ={(0.85, 0.65)}
      ]

      \pgfplotstableread{lambda-au.txt}
      \teff      %effs=efficiencies
      \addplot [color = blue, mark=none, ] table[y=one] from \teff;
      \addlegendentry{m = -1};
            \addplot [color = violet, mark=none]  table[y=two] from \teff;
       \addlegendentry{m = -2};
              \addplot [color = red, mark=none]  table[y=three] from \teff;
      \addlegendentry{m = -3};
      \addplot [color = orange, mark=none]  table[y=four] from \teff;
      \addlegendentry{m = -4};
      \addplot [color = green, mark=none]  table[y=five] from \teff;
      \addlegendentry{m = -5};
             \end{axis}
    \end{tikzpicture}
    \end{document}
P.png
P.png (22.15 KiB) Viewed 10937 times
Kind regards

Christian
thinkpadT
Posts: 25
Joined: Wed Sep 26, 2012 5:46 pm

Re: pgfplots | Additional Ticks on Abscissa

Post by thinkpadT »

Hi,

Thanks a lot, that's what i was looking for.

-Fawad
Post Reply