3

i have the following Code where i use the smooth function, but the graph generated is not smooth. If i use Excel, i do get the smooth graph as expected. Anyone who can help me figure out how to correct my code to give the expected result?

 \begin{figure}
    \centering
    \begin{tikzpicture}
    \begin{axis}[legend style={nodes={scale=0.8, transform shape}}, 
    legend image post style={mark=*},
    label style={font=\normalsize},
    tick label style={font=\normalsize},
    title style={font=\normalsize},
    xmajorgrids=true,
    ymajorgrids=true,
    xlabel= z \text{[mm]},
    ylabel= k ]
    \addplot +[draw=blue, ultra thick, smooth, tension=0, mark=none] table [x=x, y=y] {function_of_c_1.dat};
    \addlegendentry{z=2mm}
    \end{axis}
    \end{tikzpicture}
    \caption{\label{couplingfactorasafunctionofc} Coupling factor as a function of c}
\end{figure}

Here is the .dat file:

x   y
1   0.44101973
2   0.567269415
1.1 0.667737335
3.1 0.736072572
4.2 0.768144162
5.3 0.776626612
6.4 0.763111283
7.5 0.740153175
8.6 0.712124574
9.7 0.683406282
10.8    0.658323703
11.9    0.630468591
13  0.608243096
14.1    0.580554051
15.2    0.557490458
16.3    0.534336253
17.4    0.513333722
18.5    0.492471051
19.6    0.472899091
20.7    0.454521589
21.8    0.436689002
22.9    0.423646555
24  0.403809178
25.1    0.388417979
26.2    0.374742467

Thanks a lot!

  • 2
    Your LaTeX graph is correct. The Excel graph is not a x,y graph... – Paul Gaborit Nov 4 at 10:48
  • If you plot it with gnuplot with \addplot +[no markers, raw gnuplot] gnuplot { plot 'Coupling_function_of_c_1.dat' smooth sbezier; }; you reproduce the excel result to some extent. You need to run with -shell-escape and remove the header x y of the data file for that. – Schrödinger's cat Nov 4 at 10:56
  • Because a) Your data points are not in order b) The excel graph is not an XY plot c) who decided excel should re-order you data or apply a particular level of falseness to the curve? – Aubrey Blumsohn Nov 4 at 11:01
  • How do i run with -shell-escape? I tried adding this command in in PdfLatex instead of -interaction=nonstopmode found in configure Texstudio. It didn't work! Do i Need to enter it elsewhere? @Schrödinger's cat – Buzz Nov 4 at 11:44
  • @others I have shown the expected result, how can i plot that result in Latex with the data available? – Buzz Nov 4 at 11:51
0

Look at the horizontal axis in the Excel plot. The labels there make no sense as numbers. Excel thinks they are just labels.

You probably selected a line chart in Excel (since a line is what you think you want) but x-y scatter is what you really want. Then Excel interprets the (x,y) pairs as coordinates of points.

(You can try switching the chart type in Excel, but you might have to redraw the graph from scratch.)

TeX gets it right.

0

For your data pgfplots interprets the values in the x column as the x coordinate of the graph. Excel on the other hand seems to interpret the values as labels and just plots the y values sequentially.

I am not sure if that is correct but there might be a use case for that, for example if x and y are variables measured in pairs in a sequence and x is not the variable that represents the sequence (and the real sequence variable is somehow not explicit).

So in case you want to create a similar plot in pgfplots you can do that by reading the xtick labels from the file (instead of automatically generating numbers between xmin and xmax) and generating and increasing the x coordinate automatically instead of reading it from the file.

Reading xtick labels can be done with xticklabels from table and generating the x coordinates can be done with x expr=\coordindex+1.

For xticklabels from table you have to load the table first with \pgfplotstableread, which requires the pgfplotstable package. This command reads the data file into a macro which you can use for both the xticklabels and the plotting itself.

You also need to set the y index specifically, in this case column index 1. To get all the labels from the file and not just every five or so you can use xtick=data. There are some more modifications in the MWE below but they are just to polish the graph a bit.

MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{amsmath}

\begin{document}

\pgfplotstableread{function_of_c_1.dat}{\functionofcone}

\begin{tikzpicture}
\begin{axis}[legend style={nodes={scale=0.8, transform shape}},
width=\textwidth,
height=8cm,
legend image post style={mark=*},
label style={font=\normalsize},
tick label style={font=\normalsize},
title style={font=\normalsize},
xmajorgrids=false,
ymajorgrids=true,
xlabel= r,
ylabel= Coupling factor,
xtick=data,
xticklabels from table={\functionofcone}{x},
x tick label style={rotate=90,anchor=east},
xlabel near ticks,
ytick distance=0.1,
ymin=0,
ymax=0.9 ]
\addplot [mark=none,color=orange,thick] table[x expr=\coordindex+1,y index=1] {\functionofcone};
\addlegendentry{z=2mm}
\end{axis}
\end{tikzpicture}

\end{document}

Excel original:

enter image description here

pgfplots version:

enter image description here

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.