3

I am wondering if it is possible to plot a graph where there would be two series of data, each with its different x axis. For example, I would like to produce something similar to that: enter image description here

But so far, I only was able to plot this:

enter image description here

This is the code i used:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\usepackage{pgfplotstable, graphicx, tikz, float}
\usetikzlibrary{patterns}
\floatstyle{plaintop}
\newfloat{graphic}{thp}{lop}
\floatname{graphic}{\textsc{Graphique}}
\begin{document}
\pgfplotstableread[col sep=semicolon, header=true]{
periodes;ISF
nan;nan
;2.03200
1933-1937;2.19150
1938-1942;2.68000
1943-1947;3.11400
1948-1952;3.54250
1953-1957;3.60000
1958-1962;2.92750
1963-1967;2.32000
1968-1972;1.79800
1973-1977;1.80350
1978-1982;1.82700
1983-1987;2.02100
1988-1992;1.98250
1993-1997;2.01650
1998-2002;2.07300
2003-2007;1.95150
2008-2012;nan
}\loadedtable

\begin{graphic}[H]
\caption[position=above]{Comparaison ISF et DF}
\center
\begin{tikzpicture}[scale=.75]
\begin{axis}[
scale only axis,
    ytick align=center,
    ytick pos=left,
    xtick align=center,
    ymin=0,
    xtick pos=left,
    width=\textwidth,
    height=10cm,
    xlabel={\textit{Périodes}},
    ylabel={\textit{ISF/DF}},
    ymajorgrids,
    xticklabels from table = {\loadedtable}{periodes},
    xticklabel style={rotate=45,anchor =east, font=\tiny},
    legend pos=outer north east,
]
    \addplot table[x expr={\coordindex} ,y expr= {\thisrow{ISF}}] \loadedtable; \addlegendentry{ISF};
    \end{axis}
    
    
\end{tikzpicture}

\end{graphic}
\end{document}

And let's say that my other series of data would be the following:

nan;nan
;2,50300
1916-1920;2,82650
1921-1925;3,07550
1926-1930;3,23000
1931-1935;2,98950
1936-1940;2,50300
1941-1945;2,13650
1946-1950;1,98550
1951-1955;1,99350
1956-1960;2,03850
1961-1965;nan

How can I make both appear on the same graph with a secondary X axis?

Thank you.

  • Quick question: in the first data set, the y-values have the usual dot, but in the second, there are commas. Should these commas also be dots? – user121799 Apr 2 '18 at 22:06
1

This is not a complete solution. In principle, one might use the trick of just adding two axes, but here the problem is axis x line*=top for some reasons gets ignored. One can still cheat by moving the tickslabels up, but that's not too elegant I suppose.

\documentclass[12pt]{article} % inspired by https://tex.stackexchange.com/a/199003/121799
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}
\usepackage{float}
\floatstyle{plaintop}
\newfloat{graphic}{thp}{lop}
\floatname{graphic}{\textsc{Graphique}}
\begin{document}
\pgfplotstableread[col sep=semicolon, header=true]{
periodes;ISF
;2.03200
1933-1937;2.19150
1938-1942;2.68000
1943-1947;3.11400
1948-1952;3.54250
1953-1957;3.60000
1958-1962;2.92750
1963-1967;2.32000
1968-1972;1.79800
1973-1977;1.80350
1978-1982;1.82700
1983-1987;2.02100
1988-1992;1.98250
1993-1997;2.01650
1998-2002;2.07300
2003-2007;1.95150
}\loadedtable

\pgfplotstableread[col sep=semicolon, header=true]{
periodes;ISF
;2.03200
1916-1920;2.82650
1921-1925;3.07550
1926-1930;3.23000
1931-1935;2.98950
1936-1940;2.50300
1941-1945;2.13650
1946-1950;1.98550
1951-1955;1.99350
1956-1960;2.03850
}\secondtable

\begin{graphic}[H]
\caption[position=above]{Comparaison ISF et DF}
\center
\begin{tikzpicture}[scale=.75]
\begin{axis}[axis y line*=right,
%      axis x line*=top, %<- gets ignored
    scale only axis,
    ymin=0,ymax=4,
    ytick align=center,
    ytick pos=left,
    xtick align=center,
%    xtick pos=left,
    width=\textwidth,
    height=10cm,
    xmin=-1,
    xlabel={\textit{Périodes}},
    ylabel={\textit{ISF/DF}},
    ymajorgrids,
    legend pos=south west,
    xticklabels from table = {\loadedtable}{periodes},
    xticklabel style={rotate=45,anchor =east, font=\tiny},
    legend style={at={(0.9,0.9)},anchor=north}]
    \addplot table[x expr=\coordindex,y expr= {\thisrow{ISF}}] \loadedtable;
    \addlegendentry{ISF};
    \end{axis}

\begin{axis}[axis y line*=right,
%      axis x line*=top, %<-get's ignored!
    scale only axis,
    ymin=0,ymax=4,
    width=\textwidth,
    height=10cm,
    legend style={at={(0.9,0.8)},anchor=north},
    xlabel={\textit{Périodes}},
    xlabel style={yshift=12cm},
    ylabel={},
    xticklabels from table = {\secondtable}{periodes},
   xticklabel style={rotate=45,anchor =west,
   font=\tiny,yshift=7.2cm,xshift=7.2cm},
   xtick style={yshift=10cm} 
]
    \addplot[red,mark=square] table[x expr=\coordindex ,y expr= {\thisrow{ISF}}] \secondtable;
    \addlegendentry{DF}; 
    \end{axis}
\end{tikzpicture}
\end{graphic}
\end{document}

enter image description here

| improve this answer | |

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.