TransWikia.com

Pgfplots: skip (EDIT: cut out) some x-axis values

TeX - LaTeX Asked on November 12, 2021

I want to plot a time series representing global horizontal irradiance (GHI), but in order to save some room in the graph, I’d like to skip (EDIT: cut out, not skip) the nighttime (since GHI = 0). After many researches, both in the manual and online, I did not manage to find a way to do it.

Does anyone have pointers?

Data file (18kb):
https://mycore.core-cloud.net/index.php/s/C6shyrGVB6P3sFY

Plot:
Plot including nighttime

MWE:

documentclass{minimal}
usepackage{tikz,pgfplots}
usepgfplotslibrary{dateplot}

begin{document}

begin{center}
    begin{tikzpicture}
    pgfplotsset{
        width=0.9linewidth,
        height=6cm,
        enlarge x limits=false,
      date coordinates in=x,
    },
    pgfplotstableread[col sep=comma]{mwe_data.txt}csvdata

    begin{axis}[
      xtick = {
         2019-11-28 12:00:00,
         2019-11-29 12:00:00,
         2019-11-30 12:00:00,
         2019-12-01 12:00:00,
         2019-12-02 12:00:00,
         2019-12-03 12:00:00,
         2019-12-04 12:00:00,
         2019-12-05 12:00:00,
         2019-12-06 12:00:00
         },
      xticklabels = {{11/28}, {11/29}, {11/30}, {12/01}, {12/02}, {12/03},  {12/04}, {12/05}, {12/06}},
    ]
    addplot[blue]  table [x=date, y=ytest, col sep=comma] {csvdata};
    end{axis}
    end{tikzpicture}

      I would like to skip the nights (``flat parts'' between each bell-shaped curve). How to do it?
end{center}

end{document}

2 Answers

You could do something like the following ...
Hopefully the code is self-explanatory. If not, let me know and I'll add some more comments.

% used PGFPlots v1.17
begin{filecontents*}{2019-11-28.txt}
date,ytest
2019-11-28 07:00:00,8.02755958414918
2019-11-28 07:10:00,10.0714341969697
2019-11-28 07:20:00,18.182062993007
2019-11-28 07:30:00,31.9215635967366
2019-11-28 07:40:00,59.9968768484848
2019-11-28 07:50:00,43.8790581818182
2019-11-28 08:00:00,56.7919637365967
2019-11-28 08:10:00,85.6069067482517
2019-11-28 08:20:00,60.8201431724942
2019-11-28 08:30:00,67.0991551515152
2019-11-28 08:40:00,108.272003799534
2019-11-28 08:50:00,147.261625827506
2019-11-28 09:00:00,127.970548484848
2019-11-28 09:10:00,249.143053986014
2019-11-28 09:20:00,266.097825617716
2019-11-28 09:30:00,175.566653263403
2019-11-28 09:40:00,259.082215151515
2019-11-28 09:50:00,220.087723729604
2019-11-28 10:00:00,184.913791095571
2019-11-28 10:10:00,186.307211678322
2019-11-28 10:20:00,174.174548484848
2019-11-28 10:30:00,161.654907226107
2019-11-28 10:40:00,239.255915198135
2019-11-28 10:50:00,269.134998484848
2019-11-28 11:00:00,330.599837599068
2019-11-28 11:10:00,306.391391072261
2019-11-28 11:20:00,196.466128065268
2019-11-28 11:30:00,250.895419242424
2019-11-28 11:40:00,341.598022424242
2019-11-28 11:50:00,510.74779037296
2019-11-28 12:00:00,459.975986363636
2019-11-28 12:10:00,317.949429510489
2019-11-28 12:20:00,155.827984382284
2019-11-28 12:30:00,218.156163356643
2019-11-28 12:40:00,470.363884848485
2019-11-28 12:50:00,350.313718391608
2019-11-28 13:00:00,297.899851794872
2019-11-28 13:10:00,345.954895454545
2019-11-28 13:20:00,249.559491841492
2019-11-28 13:30:00,272.595835780886
2019-11-28 13:40:00,281.575694592075
2019-11-28 13:50:00,242.207596969697
2019-11-28 14:00:00,257.686765407925
2019-11-28 14:10:00,229.097061258741
2019-11-28 14:20:00,175.762047645688
2019-11-28 14:30:00,201.927451515152
2019-11-28 14:40:00,199.678609067599
2019-11-28 14:50:00,162.180008146853
2019-11-28 15:00:00,168.439736666667
2019-11-28 15:10:00,129.322764848485
2019-11-28 15:20:00,73.4925704032634
2019-11-28 15:30:00,33.4562387459207
2019-11-28 15:40:00,27.2548221212121
2019-11-28 15:50:00,30.1002351305361
2019-11-28 16:00:00,13.8014952785548
2019-11-28 16:10:00,5.99298729393939
end{filecontents*}
begin{filecontents*}{2019-11-29.txt}
date,ytest
2019-11-29 07:00:00,8.22343353613053
2019-11-29 07:10:00,9.10495462121212
2019-11-29 07:20:00,14.7334812820513
2019-11-29 07:30:00,27.4326361934732
2019-11-29 07:40:00,43.083562004662
2019-11-29 07:50:00,43.2562539393939
2019-11-29 08:00:00,42.5858389324009
2019-11-29 08:10:00,48.3482946270396
2019-11-29 08:20:00,55.3327293939394
2019-11-29 08:30:00,65.294122974359
2019-11-29 08:40:00,102.524040792541
2019-11-29 08:50:00,127.1736704662
2019-11-29 09:00:00,239.150707575758
2019-11-29 09:10:00,361.603814452215
2019-11-29 09:20:00,328.831635617716
2019-11-29 09:30:00,345.97325006993
2019-11-29 09:40:00,357.99968030303
2019-11-29 09:50:00,372.845608088578
2019-11-29 10:00:00,390.571766829837
2019-11-29 10:10:00,405.603939393939
2019-11-29 10:20:00,418.199873776224
2019-11-29 10:30:00,431.443011818182
2019-11-29 10:40:00,438.386245081585
2019-11-29 10:50:00,441.688725757576
2019-11-29 11:00:00,448.251020839161
2019-11-29 11:10:00,454.550526317016
2019-11-29 11:20:00,456.137909114219
2019-11-29 11:30:00,458.420281818182
2019-11-29 11:40:00,460.692877575758
2019-11-29 11:50:00,459.33933993007
2019-11-29 12:00:00,456.890065151515
2019-11-29 12:10:00,451.382753566434
2019-11-29 12:20:00,444.060885034965
2019-11-29 12:30:00,436.250066200466
2019-11-29 12:40:00,426.142813636364
2019-11-29 12:50:00,416.812155477856
2019-11-29 13:00:00,405.859154428904
2019-11-29 13:10:00,389.548715151515
2019-11-29 13:20:00,372.751736270396
2019-11-29 13:30:00,354.381773030303
2019-11-29 13:40:00,337.545432797203
2019-11-29 13:50:00,319.499625757576
2019-11-29 14:00:00,299.772427599068
2019-11-29 14:10:00,275.693884545455
2019-11-29 14:20:00,251.035318811189
2019-11-29 14:30:00,224.904456060606
2019-11-29 14:40:00,201.454696783217
2019-11-29 14:50:00,176.488658578089
2019-11-29 15:00:00,153.540760606061
2019-11-29 15:10:00,128.508215850816
2019-11-29 15:20:00,104.884847855478
2019-11-29 15:30:00,81.2881136829837
2019-11-29 15:40:00,57.403333939394
2019-11-29 15:50:00,37.3887196969697
2019-11-29 16:00:00,18.6037069207459
2019-11-29 16:10:00,5.55498918181818
end{filecontents*}
documentclass[border=5pt]{standalone}
usepackage{pgfplots}
    usepgfplotslibrary{
        dateplot,
        groupplots,
    }
begin{document}
begin{tikzpicture}
    begin{groupplot}[
        group style={
            group size=9 by 1,
            horizontal sep=1mm,
            y descriptions at=edge left,
        },
        width=0.1linewidth,
        height=4cm,
        scale only axis,
        ymin=0,
        ymax=500,
        enlarge x limits=false,
        date coordinates in=x,
        date ZERO=2019-11-28,
        xticklabel style={
            align=center,
            rotate=90,
            anchor=near xticklabel,
        },
        no markers,
        table/x=date,
        table/y=ytest,
        table/col sep=comma,
        %
        xtick=empty,       % <-- delete me when all plots finished
    ]
    nextgroupplot[
        xmin=2019-11-28 07:00,
        xmax=2019-11-28 16:00,
        xtick={%
            2019-11-28 07:00,
            2019-11-28 16:00%
        },
        % adapt to your needs
        xticklabels={%
            {\2019-11-28 07:00},
            {2019-11-28 16:00\}%
        },
    ]
        addplot table {2019-11-28.txt};
    nextgroupplot[
        xmin=2019-11-29 07:00,
        xmax=2019-11-29 16:00,
        xtick={%
            2019-11-29 07:00,
            2019-11-29 16:00%
        },
        xticklabels={%
            {\2019-11-29 07:00},
            {2019-11-29 16:00\}%
        },
    ]
        addplot table {2019-11-29.txt};
    %
    % I think you know how to continue with the rest of the plots ...
    nextgroupplot
        addplot [red] table {2019-11-28.txt};
    nextgroupplot
        addplot [red] table {2019-11-29.txt};
    nextgroupplot
        addplot [red] table {2019-11-28.txt};
    nextgroupplot
        addplot [red] table {2019-11-29.txt};
    nextgroupplot
        addplot [red] table {2019-11-28.txt};
    nextgroupplot
        addplot [red] table {2019-11-29.txt};
    nextgroupplot
        addplot [red] table {2019-11-28.txt};
    end{groupplot}
end{tikzpicture}
end{document}

image showing the result of above code

Answered by Stefan Pinnow on November 12, 2021

Perhaps this method will suit you. PGFplot can filter values by axis (x or y). And also PGFplot can make breaks in charts where there are inf or nan with parameter unbounded coords=jump. You can choose the minimum value for y for the 'nighttime' and change it to nan with filter (Of course, it is possible to specify the intervals for x, if it is known).

documentclass{minimal}
usepackage{tikz,pgfplots}
usepgfplotslibrary{dateplot}

begin{document}

begin{center}
    begin{tikzpicture}
    pgfplotsset{
        width=0.9linewidth,
        height=6cm,
        enlarge x limits=false,
      date coordinates in=x,
    },
    pgfplotstableread[col sep=comma]{mwe_data.txt}csvdata

    begin{axis}[
      unbounded coords=jump,
      filter point/.code={%
          pgfmathparse{pgfkeysvalueof{/data point/y}<7}%
          ifpgfmathfloatcomparisonpgfkeyssetvalue{/data point/x}{nan}%
          fi
      },
      xtick = {
         2019-11-28 12:00:00,
         2019-11-29 12:00:00,
         2019-11-30 12:00:00,
         2019-12-01 12:00:00,
         2019-12-02 12:00:00,
         2019-12-03 12:00:00,
         2019-12-04 12:00:00,
         2019-12-05 12:00:00,
         2019-12-06 12:00:00
         },
      xticklabels = {{11/28}, {11/29}, {11/30}, {12/01}, {12/02}, {12/03},  {12/04}, {12/05}, {12/06}},
    ]
    addplot[blue]  table [x=date, y=ytest, col sep=comma] {csvdata};
    end{axis}
    end{tikzpicture}

      I would like to skip the nights (``flat parts'' between each bell-shaped curve). How to do it?
end{center}

end{document}

The result will be such a drawing.

enter image description here

Answered by Vladimir on November 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP