2

I would like to copy the style of a graph in pgfplots that looks like this: enter image description here

There are examples for graphs with multiple y-Ordinates in the pgfplots manual but they do not exactly give the output I expect. Here is my code:

\begin{tikzpicture}
\begin{axis}[
scale only axis,
xlabel={Input voltage [V]},
ylabel={left axis},
axis y line*=left,
grid=both,
xmin=0,
xmax=1.8,
ymin=-5,
ymax=90,
width=5cm,
height=5cm,
mark size=2.0pt,
]
\addplot[blue, mark=o] table [x=Vin, y=fg_pc, col sep=comma] {csv/fg_Adc_DC_sweep.csv}; 
\end{axis}

\begin{axis}[
scale only axis,
xmin=0,
xmax=1.8,
axis x line=none,
axis y line*=right,
ymin=-6,
ymax=2,
grid=both,
ylabel={right axis},
width=5cm,
height=5cm,
]
\addplot[orange, mark=o] table [x=Vin, y=Adc, col sep=comma] {csv/fg_Adc_DC_sweep.csv};
\end{axis}

\end{tikzpicture}

Note that the values in my csv file are not the same as on the first picture. Also note that I need to set the width and height of both axis explicitly, otherwise the two y-axis do not have the same size. This code compiles as follows: enter image description here

The problems I have:

1) How can I match the grids like in the first picture?

2) How can i change the color of the axis. I tried to follow the solution here. That example compiles without error but does not change the outlook of my plot at all. Is there a better and easier solution?

3) How can I get the second y label to right side too?

Thanks a lot for any advice!

1 Answer 1

4

For 3), add

\pgfplotsset{compat=1.3} 

to the preamble. (Or some higher version number, the current version is 1.16.)

Regarding the size, you can make a style where you set common options for the two axes, then you avoid code duplication. See example below.

For 2), set e.g.

y axis line style={blue},
y tick label style={blue},

to make the axis line and ticklabels blue. In the example below I made these part of the style mentioned above, and made the colour name an argument to the style.

1) is perhaps the most tricky, but you just need a little bit of math. I haven't tried automating this. Say the left axis runs from -10 to 90, with ticks at 0,20, ..., 80. That means the first tick is 10% of the range along the axis, and the step between ticks is 20% of the range.

So to get the ticks, and therefore the grid, to line up, you need to make sure the right axis follows the same rule. In your case the right axis runs from -6 to 2. The range is 8, so 10% is 0.8, and the first tick should be at -5.2. The step between ticks is 20%, i.e. 1.6.

Hence, with

ymin=-10,
ymax=90,
ytick={0,20,...,80},

on the left, you need

ymin=-6,
ymax=2,
ytick={-5.2,-3.6,...,2},

on the right.

I don't have your datafiles of course (it's always best if you can make an example that doesn't rely on files we don't have access to), so I just used a random function.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
  myaxis/.style={
    scale only axis,
    width=5cm,
    height=5cm,
    xmin=0,
    xmax=1.8,
    y axis line style={#1},
    y tick label style={#1},
    }
}
\begin{axis}[
myaxis={blue},
xlabel={Input voltage [V]},
ylabel={left axis},
axis y line*=left,
grid=both,
ymin=-10,
ymax=90,
ytick={0,20,...,80},
mark size=2.0pt,
]
\addplot[blue, mark=o] {rnd*50};
\end{axis}

\begin{axis}[
myaxis={red},
axis x line=none,
axis y line*=right,
ymin=-6,
ymax=2,
ytick={-5.2,-3.6,...,2},
ylabel={right axis},
]
\addplot[orange, mark=o] {rnd*3-2};
\end{axis}

\end{tikzpicture}
\end{document}
1
  • I needed the option separate axis lines May 20 at 9:12

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.