29

From the original data, we know that they are nearly 0.087 rather that 0.087 x 10^-2. Why is there scientific notation (10^-2)? It looks ridiculous. Please help me figure it out. Thanks very much!

MWE:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\definecolor{mycolor1}{rgb}{1,0,1}

\pgfplotstableread{
X           Y1
1    0.0873514639102964 
2    0.0873656441069913 
3    0.0873509320240032 
4    0.0873490368814644 
5    0.0873430960617957 
6    0.0873334626756388 
7    0.0873430596012905 
8    0.0873485872405474 
9    0.0873519301953318 
10  0.0873474824394306
11  0.08735183859007    
12  0.0873408783047386
13  0.0873439016123011
14  0.0873424452271129
15  0.0873452848079798
16  0.0873467228778811
17  0.087348210873026   
18  0.0873464310394191
19  0.0873469131127228
20  0.0873449430894595
}\datatable

\pgfplotsset{
    width=9cm,
    height=6cm,
    scale only axis,
    xmin=0.5, xmax=20.5,
    tick align = outside,
    yticklabel style={/pgf/number format/fixed},  
}
\begin{tikzpicture}

\begin{axis}[
    xtick={1,...,20},
    ymin=0.08715,
    ymax=0.08740,
    ytick={0.08715,0.08720,0.08725,0.08730,0.08735,0.08740},
    yticklabels={0.08715,0.08720,0.08725,0.08730,0.08735,0.08740},
    xlabel={$x$},
    ylabel={$y$},
    y axis line style={mycolor1},
    ytick style={mycolor1},
    yticklabel style=mycolor1,
    yticklabel style={/pgf/number format/fixed},
    axis x line*=bottom,
    axis y line*=left]
\addplot [
    color=mycolor1,
    solid,
    line width=1.5pt,
    mark size=2.7pt,
    mark=square*,
    mark options={solid,fill=mycolor1}]
    table [x=X, y=Y1]{\datatable};
\label{plot1} % So we can recall the plot style later on
\end{axis}
\end{tikzpicture}%
\end{document}

enter image description here

  • @Jake Yes, it is. – user22986 Jun 19 '13 at 17:11
  • Actually, for the previous figures, I used the matlab2tikz. While later I use the template generated by matlab2tikz when I became a little bit fimilar with the tikz language, that is to say, I just replace the data for the same type plot. – user22986 Jun 19 '13 at 18:15
  • Ah, okay! So the hard coded tick labels (yticklabels={0.08715...) didn't come from matlab2tikz? – Jake Jun 19 '13 at 18:19
  • Yes. It is added by me. – user22986 Jun 19 '13 at 18:25
  • Right! I've edited my answer to show how you can get the same output completely without hard coding the values. – Jake Jun 19 '13 at 18:37
48

If the tick labels are hard coded using yticklabels={...}, you need to switch off the axis multiplier using

\pgfplotsset{scaled y ticks=false}

before the tikzpicture.

An even better approach is to not hard code the labels at all, but use the number parser to format the labels. If you set

yticklabel style={
        /pgf/number format/fixed,
        /pgf/number format/precision=5
},
scaled y ticks=false

you'll get the same output as if you had set the labels using

yticklabels={0.08715,0.08720,0.08725,0.08730,0.08735,0.08740}

This has the advantage that if you change the axis range, the labels will automatically be updated.

You don't even need to set the tick positions manually using ytick={...: PGFPlots automatically chooses the same positions:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\definecolor{mycolor1}{rgb}{1,0,1}

\pgfplotstableread{
X           Y1
1    0.0873514639102964 
2    0.0873656441069913 
3    0.0873509320240032 
4    0.0873490368814644 
5    0.0873430960617957 
6    0.0873334626756388 
7    0.0873430596012905 
8    0.0873485872405474 
9    0.0873519301953318 
10  0.0873474824394306
11  0.08735183859007    
12  0.0873408783047386
13  0.0873439016123011
14  0.0873424452271129
15  0.0873452848079798
16  0.0873467228778811
17  0.087348210873026   
18  0.0873464310394191
19  0.0873469131127228
20  0.0873449430894595
}\datatable

\pgfplotsset{
    width=9cm,
    height=6cm,
    scale only axis,
    xmin=0.5, xmax=20.5,
    tick align = outside,
    yticklabel style={/pgf/number format/fixed},  
}
\begin{tikzpicture}

\begin{axis}[
    xtick={1,...,20},
    ymin=0.08715,
    ymax=0.08740,
    xlabel={$x$},
    ylabel={$y$},
    y axis line style={mycolor1},
    ytick style={mycolor1},
    yticklabel style=mycolor1,
    yticklabel style={
            /pgf/number format/fixed,
            /pgf/number format/precision=5,
            /pgf/number format/fixed zerofill
        },
        scaled y ticks=false,
    axis x line*=bottom,
    axis y line*=left]
\addplot [
    color=mycolor1,
    solid,
    line width=1.5pt,
    mark size=2.7pt,
    mark=square*,
    mark options={solid,fill=mycolor1}]
    table [x=X, y=Y1]{\datatable};
\label{plot1} % So we can recall the plot style later on
\end{axis}
\end{tikzpicture}%
\end{document}
  • It works. Thanks for your help again and again. – user22986 Jun 19 '13 at 1:15
  • Does /pgf/number format/precision=5 mean the value of increment is 0.00005 or the y labes dislplay with 5 decimal points? If it is the latter, how it get the same resuls as yticklabels={0.08715,0.08720,0.08725,0.08730,0.08735,0.08740}. – user22986 Jun 19 '13 at 18:58
  • @user22986: It's the latter, it specifies how many decimal values to round to. If you set precision=6, you'll get 0.087150. – Jake Jun 19 '13 at 19:00
  • Why are the y labels is {0.08715,0.08720,0.08725,0.08730,0.08735,0.08740} not {0.08715,0.08716,0.08717,...,0.08740}? I did not see where set the increment. Is the 0.00005 the default value for the precision=5? – user22986 Jun 19 '13 at 19:06
  • @user22986: PGFPlots tries to find a good number of tick positions. In this case, it just happened to choose the same tick positions. The value of precision doesn't influence where the ticks go, it just influences how the labels are printed. Try setting ymin=0.05, ymax=0.1, you'll see that PGFPlots chooses different tick values. – Jake Jun 19 '13 at 19:08

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.