0

I'd like to be able to modify the position of specific labels in a difractogram, most are ok, but some are positioned on top of each other and those are the ones I'd like to move.

\documentclass{article}
\usepackage{pgfplots} % Visualize/Graph data External TikZ/PGF support (thanks to Andreas Nautsch)
\usepackage{tikz}
\usetikzlibrary{positioning,arrows,shapes,snakes,
               automata,backgrounds,
               petri,topaths,shapes,calc}
\usepackage{siunitx}
\usepackage{xcolor}
\PassOptionsToPackage{utf8}{inputenc}
  \usepackage{inputenc}
  \PassOptionsToPackage{spanish,es-lcroman}{babel}
    \usepackage{babel}
    \usepackage{graphicx} 
    \usepackage{listings}
\pagestyle{empty}

\begin{document}

\begin{figure} % Plot longtest.csv
    % \vspace{-5mm}
    \centering
    \pgfplotsset{scaled x ticks=false}
    \begin{tikzpicture}
    \begin{axis}[
    % visualization depends on={ifthenelse(\coordindex==1,2mm,0) \as \nncshift},
    yticklabel = {
    \pgfmathparse{\tick/1000} % Divide the y coordinate/1000
    \pgfmathprintnumber{\pgfmathresult}\,k % Use the result of the division as thousands k https://tex.stackexchange.com/questions/200873/pgfplots-axis-abbreviation
    },
    ytick={1000,2000,3000,4000},
    % yticklabels={2k,4k},
    height=10cm,
    width=5cm, % Scale the plot to \linewidth
    grid=major, % Display a grid
    grid style={dashed,gray!30}, % Set the style
    xlabel=$2\theta$, % Set the labels
    ylabel=Intensidad,
    x unit=\si{\degree}, % Set the respective units
    y unit=cuentas,
    %xtick distance=1800,
    % xtick={0,1800,3600,7200,10800,14400},
    % ytick distance=20,
    xmin=20,
    xmax=90,
    ymin=0,
    ymax=2000,
    legend columns=1,
    legend style={
        draw=none,
        at={(1,1)},
        anchor=north east
    }, % Put the legend below the plot
    ]
    \addplot+[
    red,
    mark=none,
    ]
    table[x index=0,y index=2, col sep=comma] {Bi5000A_200oC_ProfilePhase.csv};
    \addlegendentry{Bi 1}
    \addplot+[
    green,
    mark=none,
    ]
    table[x index=0,y index=5, col sep=comma] {Bi5000A_200oC_ProfilePhase.csv};
    \addlegendentry{Bi 2}   
    % \legend{Datos observados,Bismuto 00-044-1246}
    \addplot+[
    black,
    % font=\tiny,
    only marks,
    mark=none,
    nodes near coords,
    % every node near coord/.append style={font=\footnotesize,rotate=90, anchor=west, every shadow/.style={opacity=1}},
    every node near coord/.append style={font=\footnotesize,rotate=90, anchor=west},
    % every node near coord/.append style={font=\tiny},
    point meta=explicit symbolic
    ]
    table[x index=1,y index=5, meta index=21, col sep=comma] {Bi5000A_200oC_PeakList.csv};
    \end{axis}
    \end{tikzpicture}
    \caption{Bi Difractogram}
    \label{fig:longtest}
\end{figure}
\end{document}

Here are the two csv files needed to run the code: Bismuth and Peak List

I found this answer How to avoid overlapping, and I think it has the bases for what I want to do, which is:

I have a to make a lot of these graphs, so making one by one the labels is out of question, there are a ton of them. What I'd really like to do is sort of adding an x shift column and a y shift column, perhaps 0 and 0 in all the ones I do not want to shift, and only modifying the labels I really need to shift.

I also found that I can introduce code directly in my csv labels column to be interpreted by latex, such as \contour{white}{(003)} perhaps this could be another approach as well.

Any help or guidance will be highly appreciated.

This is an example of the graph I need, and some of the labels that overlap

  • You also could edit the pdf output in inkscape, importing it in the option poppler/cairo the select the inporting objet and apply ungroup, after some computation all parts of the drawing will be editable you can select one and move it using the mouse cursor, also you can add another vector objets like links, also you can edit the colors and opacity, the format is converted to .svg but to use in your latex document just save it as pdf, you could import it using graphicx package, it will conserve vector properties.., including transparency. – J Leon V. Oct 4 '18 at 15:51
0

Indeed this can be done by providing xshift and yshift in the data file. I added these two columns with values 0 everywhere except for point No. 21, where I wrote a yshift value of 20.

For details please have a look at the comments in the code.

There are other ways to achieve the same, too, but I would guess that all have one downside. Regardless if you provide shift values absolutely or in axis units, good shift values depend on the size of the plot and used font size. So be sure that these don't change any more before "fitting" the shift values.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
    \usetikzlibrary{pgfplots.units}
    \pgfplotsset{scaled x ticks=false}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        yticklabel={
            \pgfmathparse{\tick/1000}               % Divide the y coordinate/1000
            \pgfmathprintnumber{\pgfmathresult}\,k  % Use the result of the division as thousands k https://tex.stackexchange.com/questions/200873/pgfplots-axis-abbreviation
        },
        ytick={1000,2000,3000,4000},
        height=5cm,
        width=10cm,
        grid=major,
        grid style={dashed,gray!30},
        xlabel=$2\theta$,
        ylabel=Intensidad,
        x unit=\si{\degree},
        y unit=cuentas,
        xmin=20,
        xmax=90,
        ymin=0,
        ymax=2000,
        legend columns=1,
        legend style={
            draw=none,
        },
        no markers,
        table/col sep=comma,
    ]
        \addplot+ [red] table [x index=0,y index=2] {Bi5000A_200oC_ProfilePhase.csv};
            \addlegendentry{Bi 1}

        \addplot+ [green] table [x index=0,y index=5] {Bi5000A_200oC_ProfilePhase.csv};
            \addlegendentry{Bi 2}

        \addplot+[
            black,
            only marks,
            mark=none,
            nodes near coords,
            nodes near coords style={
                font=\footnotesize,
                rotate=90,
                anchor=west,
            % -----------------------------------------------------------------
                % add the shift to the nodes (values interpreted in `pt')
                xshift=\xShift,    
                yshift=\yShift,
            },
            % state where the shift is coming from
            % (please note that xshift and yshift are crossed,
            %  which is necessary because of the `rotate=90' above)
            visualization depends on={\thisrow{xshift} \as \yShift},
            visualization depends on={\thisrow{yshift} \as \xShift},
            % -----------------------------------------------------------------
            point meta=explicit symbolic,
        ] table [x index=1,y index=5, meta index=21] {Bi5000A_200oC_PeakList.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • Thanks a lot this worked perfectly, just for the sake of completion, I just added a minus sign in this line yshift=-\yShift,, so any change in the CSV file is reflected accordingly to the most usual convention of signs in a common xy plane. – Hector M. Dominguez Oct 4 '18 at 20:58

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.