Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am a beginner in LaTeX, trying to use it for industrial documents, such a measurements report. One example is the true position graph of the MWE. The circle represents the true position tolerance. Data point outside of the circle do not meet the requirements.

I would like to improve it in the following way:

  1. Specify the limits of the graph automatically (i.e. by extending the max value found either in X or Y columns of a certain factor, the same for the X axis and Y axis) fom the actual data, and not manually in the following lines:

    xmin = -0.35, xmax =  0.35,
    ymin = -0.35, ymax =  0.35,
    
  2. In a similar way, the vertical and horizontal lines created by the following lines should adapt automatically to the graph size.

     \addplot+[sharp plot, mark=none, black] coordinates {(-0.50,0)  (0.5,0)};
     \addplot+[sharp plot, mark=none, black] coordinates {(0,-0.5)  (0,0.5)};
    
  3. If possible, assign the color of the dots, not on the Y value, but on the polar distance to the center.

  4. Align vertically the color bar labels i.e. on the decimal separator, as opposed to the current position.

  5. As a bonus, assign a neutral (i.e. blue) color to all dots that meet the tolerance, (are inside or on the circle) , and color code only the ones that do not meet the requirements...

\documentclass{article}
\usepackage[T1]{fontenc} 
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage[active,tightpage]{preview} %produces the plot only when activated
\PreviewEnvironment{tikzpicture} %produces the plot only when activated
\pgfplotsset{compat=1.5.1}
\usepackage{filecontents}

\begin{document}
\thispagestyle{empty}
\begin{filecontents}{Data.dat}
X       Y      
0.059   0.207      
-0.030  0.198      
-0.035  0.042      
0.064   0.130      
-0.118  0.133      
-0.128  0.195      
-0.143  0.022      
-0.148  0.182      
-0.079  -0.005     
-0.081  0.124      
-0.091  0.126      
-0.090  0.261      
-0.049  -0.037     
-0.045  0.063      
-0.048  0.124      
-0.051  0.199      
-0.040  -0.063     
-0.025  0.035      
-0.028  0.117      
-0.057  0.270      
-0.020  -0.075   
-0.022  0.025
-0.024  0.138
-0.33   0.288
0.012   -0.111
0.069   0.004
0.014   0.101
-0.021  -0.293
\end{filecontents}


\pgfplotstableread{Data.dat}\mytable % Creating a macro containin the data of external file

\tikzset{every mark/.append style={scale=0.7}} %Adjusting size of marks in plot

\begin{tikzpicture}
\begin{axis}[scale= 2, width=8cm,height=8cm,
%axis on top,
ticklabel style={/pgf/number format/.cd, fixed, precision=2,use comma},
  xmin = -0.35, xmax =  0.35, %Plot X size
  ymin = -0.35, ymax =  0.35, % Plot Y size
    xlabel = {True position (mm), X axis},
    ylabel = {True position (mm), Y axis},
title= Pin tip location (solder side),
    colorbar, colorbar style={ticklabel style={/pgf/number format/.cd, fixed, fixed zerofill, precision=2,use comma}},
]

\addplot[scatter, only marks, mark=*]
 table {\mytable};
 \draw[red] (axis cs:0,0) circle[radius=0.1];
\addplot+[sharp plot, mark=none, black] coordinates {(-0.50,0)  (0.5,0)}; 
\addplot+[sharp plot, mark=none, black] coordinates {(0,-0.5)  (0,0.5)}; 
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this question
    
you have asked several questions. Try and make the question more general and edit, for example: "Automatic data recognition and plot processing in pgfplots". This is pretty much what you are after, by a quick scouring. –  zeroth Jul 19 '12 at 12:08
    
Your first question needs enlargelimits,axis equal instead of the given options, the second issue is a wrong application and you can use one of the solutions in how-can-i-add-a-zero-line-to-a-plot. The third-fifth questions are also asked on this site before. Please search for items together with the [pgfplots] string which will search only in pgfplots tagged questions. Basically you want to create a new column or do some vector lenght calculation. If you get stuck on one of these ask them seperately. –  percusse Jul 19 '12 at 12:30
    
@zeroth I have edited the title as suggested. I originally thought that combining all my concerns into a single set of questions would be easier for a fine expert to answer all of htem at once, as Jake did later. –  Yves Jul 19 '12 at 19:38
    
@Yves, yes, it is simply not the policy of the site. It is more intented to be a lookup site which requires that questions are not too localized. :) I wish you a pleasent stay here at TeX stack exchange! :) –  zeroth Jul 19 '12 at 20:44
add comment

1 Answer 1

up vote 3 down vote accepted
  1. I assume you want to keep the origin in the centre of the plot, so enlargelimits, axis equal won't help here. We'll need to calculate the greatest absolute value in the table and use that to set the xmin, xmax, etc. I've written a macro \findmaxAbs{<table>}{<first column name>}{<second column name>}{<fraction>} that finds the maximum absolute value, increases it by <fraction> and stores it in \maxAbs. You can then use that to set the limits.

  2. As percusse said, you can use one of the approaches from How can I add a zero line to a plot?.

  3. The colour is defined by the meta value, which you can set to anything you like using meta expr=< expression >. In this case, you could use meta expr={sqrt((\thisrow{X})^2+(\thisrow{Y})^2)} to get the euclidean distance. By setting point meta min=0.1, the colour bar will start from 0.1.

  4. That's trickier, I think that should be a separate question.

  5. You can use the scatter/@pre marker code/.append code for this. There's a nice example in the manual.

\documentclass{article}
\usepackage[T1]{fontenc} 
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{colormaps}
\usepackage[active,tightpage]{preview} %produces the plot only when activated
\PreviewEnvironment{tikzpicture} %produces the plot only when activated
\pgfplotsset{compat=1.5.1}
\usepackage{filecontents}

\newcommand{\findmax}[3]{
    \pgfplotstablesort[sort key={#2},sort cmp={float >}]{\sorted}{#1}%
    \pgfplotstablegetelem{0}{#2}\of{\sorted}%
    \pgfmathsetmacro#3{\pgfplotsretval}
}

\newcommand{\findmin}[3]{
    \pgfplotstablesort[sort key={#2},sort cmp={float <}]{\sorted}{#1}%
    \pgfplotstablegetelem{0}{#2}\of{\sorted}%
    \pgfmathsetmacro#3{\pgfplotsretval}
}

\newcommand{\findmaxAbs}[4]{
    \findmax{#1}{#2}{\maxX}
    \findmax{#1}{#3}{\maxY}
    \findmin{#1}{#2}{\minX}
    \findmin{#1}{#3}{\minY}
    \pgfmathsetmacro\maxAbs{(1+#4)*max(abs(\maxX),abs(\maxY),abs(\minX),abs(\minY))}
}




\begin{document}
\thispagestyle{empty}
\begin{filecontents}{Data.dat}
X       Y      
0.059   0.207      
-0.030  0.198      
-0.035  0.042      
0.064   0.130      
-0.118  0.133      
-0.128  0.195      
-0.143  0.022      
-0.148  0.182      
-0.079  -0.005     
-0.081  0.124      
-0.091  0.126      
-0.090  0.261      
-0.049  -0.037     
-0.045  0.063      
-0.048  0.124      
-0.051  0.199      
-0.040  -0.063     
-0.025  0.035      
-0.028  0.117      
-0.057  0.270      
-0.020  -0.075   
-0.022  0.025
-0.024  0.138
-0.33   0.288
0.012   -0.111
0.069   0.004
0.014   0.101
-0.021  -0.293
\end{filecontents}


\pgfplotstableread{Data.dat}\mytable % Creating a macro containin the data of external file


\findmaxAbs{\mytable}{X}{Y}{0.1}

\tikzset{mark size=2} %Adjusting size of marks in plot
\makeatletter
\begin{tikzpicture}
\begin{axis}[scale= 2, width=8cm,height=8cm,
%axis on top,
ticklabel style={/pgf/number format/.cd, fixed, precision=2,use comma},
    xmin=-\maxAbs, xmax=\maxAbs,
    ymin=-\maxAbs, ymax=\maxAbs,
    extra y ticks       = 0,
      extra y tick labels = ,
      extra y tick style  = { grid = major },
        extra x ticks       = 0,
      extra x tick labels = ,
      extra x tick style  = { grid = major },
    axis equal,enlargelimits=0,
    xlabel = {True position (mm), X axis},
    ylabel = {True position (mm), Y axis},
    title= Pin tip location (solder side),
    colorbar, colorbar style={ticklabel style={/pgf/number format/.cd, fixed, fixed zerofill, precision=2,use comma}},
    point meta=explicit,
    colormap={errormap}{color(0cm)=(yellow); color(1cm)=(red)},
    scatter/@pre marker code/.append code={%
        \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
        \pgfmathtruncatemacro\iserror{\pgfplotspointmeta>0.1}
        \ifnum\iserror=0
            \def\markopts{fill=blue, draw=blue}%
        \else
            \def\markopts{}%
        \fi
        \expandafter\scope\expandafter[\markopts]
        \pgfkeys{/pgf/fpu=false}
    },%
    scatter/@post marker code/.append code={%
        \endscope
    },
    point meta min=0.1
]

\addplot[scatter, only marks, mark=*]
 table [meta expr={sqrt((\thisrow{X})^2+(\thisrow{Y})^2)}] {\mytable};
 \draw[red] (axis cs:0,0) circle[radius=0.1];
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
    
The "enlarge limits, axis equal" was definitely more elegant than my original code, but as you pointed out, did not make the plot completely square. I had managed on my own to find a way to restrict to a single color the data points inside the circle, but there was no way for me to find the complete solution... With a very elegant result. Thanks again. I had no idea it would be a major challenge to align vertically the labels of the color bars. Anyway, this is no longer necessary, with your code, as they align naturally, due to the elimination of the negative values. –  Yves Jul 19 '12 at 19:43
    
Glad I could help! The method I used for making the points in the circle a different colour is a bit of a sledgehammer, and I'm not entirely convinced it's the right approach. Would you mind sharing what approach you used? –  Jake Jul 19 '12 at 19:45
1  
I did this in the office and do not have the code with me. I will find out tomorrow. I did experiment a lot reading the manual. I seem to remember I set a "min meta" to the radius of the tolerance circle. I am inclined to trust your code far better than mine anyhow.... –  Yves Jul 19 '12 at 19:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.