3

Consider the following code:

\documentclass{standalone}
\usepackage{tikz,pgfplots,siunitx}
\pgfplotsset{compat=newest}
\begin{document} 
\begin{tikzpicture}

%% 1st and 2nd waveforms
\begin{axis}[
    scale only axis,
    axis y line*=left,
    axis x line*=bottom,
    xlabel={Time [\si{s}]},
    ylabel={Current [\si{\A}]},
    grid=major
]
\addplot [blue, line width = 1, smooth] 
table[x=t,y=ch1,col sep=comma]{myDataFile.csv};
\addplot [green, line width = 1, smooth] 
table[x=t,y=ch2,col sep=comma]{myDataFile.csv};
\end{axis}
%% Here starts the 3rd waveform
\begin{axis}[
    scale only axis,
    axis y line*=right,
    axis x line*=bottom,
    xlabel={Time [\si{s}]},
    ylabel={Voltage (Normalised)}
    %grid=major
]
\addplot [red, line width = 1, smooth]
table[x=t,y=ch3,col sep=comma]{myDataFile.csv};
\end{axis}
   
\end{tikzpicture}
\end{document}

The myDataFile.csv file is the following:

%myDataFile.csv%
t,ch1,ch2,ch3
-4,-2,0.0625,0.756
-3,-1.5,0.125,-0.141
-2,-1,0.25,-0.909
-1,-0.5,0.5,-0.841
0,0,1,0
1,0.5,2,0.841
2,1,4,0.909
3,1.5,8,0.141
4,2,16,-0.758

The thing is that the 3rd waveform is translated (up about 6 units) and also stretched, so that it extends the whole vertical span determined by the minimum value of ch1 or ch2, the smaller of the two, and the maximum value of ch1 or ch2, the larger of the two.

In the example above, ch3 (red) is a sine function plotted from t=-4 to t=+4, so it is bounded from -1 to 1 in the vertical direction. As you can see, it is not plotted correctly. (ch1 is y=-t/2, and ch2 is 2^t)

enter image description here

I am following the code in this answer, about plotting two functions on one "canvas".

From what I see in the inline picture of this linked answer, the same effect applies there too.

How can I avoid this and plot ch3 as it is?

To elucidate my point, I want something that looks like the following (plotted in Desmos):

Desired output

Notice how the sine wave spans in the vertical direction compared with the 2^t curve. It is really from -1 to +1 and its "x-axis" coincides with that of the other two curves. But, it describes a different physical quantity and therefore "deserves" a different label and ticks, to be plotted on the right edge.

Moreover, how can I instruct pgfplots to stretch it in the vertical direction by, say, 2 times each value entry? I found a question about scaling a data set, but I don't know how to do it for data imported from a csv file as in the case here.

3
  • 1
    Regarding scaling, the same method as Jake's second suggestion works for files as well. With \addplot table you can provide the table either as a filename, or directly in the .tex file as in Jake's example, but y expr=\thisrow{column name}*10 works for both. Apr 14, 2021 at 18:28
  • 1
    I honestly don't quite understand what you mean about the stretching and translation. The sine function looks a bit odd because you have very few sample points and you use smooth. If it should be a pure sine function, you can always do \addplot [blue, domain=-4:4,samples=50] {sin(deg(x))}; Apr 14, 2021 at 18:34
  • @TorbjørnT. I don't want to add an anonymous function (such as sin(deg(x))), I need to plot data exported from an oscilloscope. And I uploaded an additional image together with an explanation so I hope the readers can now better understand my question. And thanks for the code snippet about scaling the numerical entries!
    – tush
    Apr 14, 2021 at 21:51

1 Answer 1

2

You need to set the y-limits to the same values for both axes, so add ymin=-4, ymax=17. There are several ways to do that, you can add it to both axes separately, you can do \pgfplotsset{common axis settings/.style={ymin=-4, ymax=17}} and then add common axis settings to both axis options, or simply add \pgfplotsset{ymin=-4, ymax=17} between \begin{tikzpicture} and the first \begin{axis}.

I'd probably remove smooth, which will use bezier curves between sample points instead of straight lines, and the x-ticks and label from the second axis (which currently are duplicated).

As mentioned in the comment, you can use the method for scaling plots described by Jake in the answer you link to, I demonstrated that in code below as well.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots,siunitx}
\pgfplotsset{compat=newest}
\begin{document} 
\begin{tikzpicture}
\pgfplotsset{
  ymin=-4,
  ymax=17,
  every axis plot/.append style={
    line width = 1,
    % if you want to add markers as in the Desmos plot, uncomment this
%      mark=*,
%      mark size=2
     }
  }
%% 1st and 2nd waveforms
\begin{axis}[
    scale only axis,
    axis y line*=left,
    axis x line*=bottom,
    xlabel={Time [\si{s}]},
    ylabel={Current [\si{\A}]},
    grid=major
]
\addplot [blue] table[x=t,y=ch1,col sep=comma] {myDataFile.csv};
\addplot [green] table[x=t,y=ch2,col sep=comma] {myDataFile.csv};
\end{axis}

%% Here starts the 3rd waveform
\begin{axis}[
    scale only axis,
    axis y line*=right,
    axis x line*=bottom,
    xtick=\empty,
%    xlabel={Time [\si{s}]},
    ylabel={Voltage (Normalised)},
    ylabel style={red}
]
\addplot [red] table[x=t,y=ch3,col sep=comma] {myDataFile.csv};
\addplot [red, densely dashed] table[x=t,y expr=\thisrow{ch3}*2,col sep=comma] {myDataFile.csv};
\end{axis}
   
\end{tikzpicture}
\end{document}
7
  • 1
    I'm sure it's possible to find those values automatically, but it's too late in the evening for me to try right now :) Apr 14, 2021 at 22:18
  • That's great. Thank you very much indeed!
    – tush
    Apr 14, 2021 at 22:22
  • Could you help me, in a comment, and write down a setting to change the x-to-y ratio for these two plot? I want it be something like 1-to-3 height to width aspect ratio, and not the default 1-to-1.
    – tush
    Apr 15, 2021 at 8:24
  • @tush e.g. width=8cm, height=5cm in the pgfplotsset? Apr 15, 2021 at 8:26
  • Yes, something like this, but the output was clipped from the top.
    – tush
    Apr 15, 2021 at 8:27

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.