2

I have a U-I-diagramm in physics of a yellow LED. I use a yellow line, because the LED is yellow. For better reading of the diagram I add a black dashed line over the yellow line. Here is my approach:

\documentclass[varwidth, margin=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{pgf, tikz}
\usetikzlibrary{datavisualization}
\begin{document}

\begin{tikzpicture}
    \datavisualization [
        school book axes,
        x axis = {
            grid,
            ticks={step=1, minor steps between steps=9},
            attribute=U,
            label={[node style={fill=blue!20}]{$U$ in Volt}},
            unit length=2cm
            },
        y axis = {
            grid,
            ticks={
                step=0.01,
                tick typesetter/.code={%
                %https://tikz.dev/math-numberprinting
                    \pgfmathprintnumber[fixed, fixed zerofill, precision=2]{##1}                    }
                },
            attribute=I,
            label={[node style={fill=red!20}]{$I$ in Ampere}},
            unit length=100cm
            },
        visualize as line/.list={LEDyellow2, LEDyellow},
        legend={below, rows=2},
        LEDyellow={
            %label in legend={text={LED yellow}},
            style={yellow}
        },
        LEDyellow2={
            label in legend={text={LED yellow}},
            style={black, dashed}
        },
        ]
        data [set=LEDyellow, separator=\space]{
            U    I
            0    0
            1.7   0
            1.8   0.004
            1.9   0.01
            2.0   0.02
            2.1   0.08
            %2.2  0.20
        }
        data [set=LEDyellow2, separator=\space]{
            U    I
            0    0
            1.7   0
            1.8   0.004
            1.9   0.01
            2.0   0.02
            2.1   0.08
            %2.2  0.20
        };
\end{tikzpicture}

\end{document}

enter image description here

Questions:

  1. In my workaround to make a black-yellow dashed line I printed two datavisualisations over each other. Is there a better way to achieve this? A disadvantage of my workaround is that the legend doesn't show the yellow-black dashed line.
  2. In my approach I use two data sets (LEDyellow and LEDyellow2). Is it possible to reuse the first dataset? This is not only interesting in my case. I think on a case where the first datavisualisation does a "line" and then the second datavisualisation does a "smooth line" of the same data set.
  3. What is the reason for the printing order? My first try was the order visualize as line/.list={LEDyellow, LEDyellow2} because it is logical to print first the pure yellow line and then the dashed black line over the yellow line. But the reverse order brings the desired result.
  4. The grid goes into the numbers on the U- and I-axis. Why is that the default behaviour? Howto correct this?

Thanks.

2
  • As for the yellow-black line, I would have said: use a postaction, but sadly, it seems that this does not work with datavisualization. Would a PGFPlots solution work for you? Commented Apr 27, 2023 at 22:38
  • 1
    @JasperHabicht Thank you for your idea with the postaction. Qrrbrbirlbel was able to find an answer with this tip.
    – Michael.H
    Commented Apr 28, 2023 at 15:18

1 Answer 1

3

The comment about postaction was the right one, it just needs to be applied to the actual paths and not to the scope where it will later be applied.

This needs the nomorepostaction key to prevent an infinite recursion.

I've added padding min values to both axes to adjust the grid.

Code

\documentclass[varwidth, margin=10pt]{standalone}
\usepackage[ngerman]{babel}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\makeatletter
\tikzset{nomorepostaction/.code=\let\tikz@postactions\pgfutil@empty}
\makeatother
\begin{document}
\tikz
\datavisualization [
  school book axes, clean ticks,
  x axis = {
    grid, padding min = -2pt,
    ticks={step=1, minor steps between steps=9},
    attribute=U,
    label={[node style={fill=blue!20}]{$U$ in Volt}},
    unit length=2cm
  },
  y axis = {
    grid, padding min = 0pt,
    ticks={
      step=0.01,
      tick typesetter/.code={%
      %https://tikz.dev/math-numberprinting
        \pgfmathprintnumber[fixed, fixed zerofill, precision=2]{##1}}
    },
    attribute=I,
    label={[node style={fill=red!20}]{$I$ in Ampere}},
    unit length=100cm
  },
  visualize as line/.list={LEDyellow2, LEDyellow},
  legend={below, rows=2},
  LEDyellow={
    label in legend={text={LED yellow}},
    style={yellow, every path/.append style={postaction={nomorepostaction, black, dashed}}},
  },
]
  data [set=LEDyellow, separator=\space]{
      U    I
      0    0
      1.7   0
      1.8   0.004
      1.9   0.01
      2.0   0.02
      2.1   0.08
      %2.2  0.20
  };
\end{document}

Output

enter image description here

1
  • Thanks for your answer. In the line visualize as line/.list={LEDyellow2, LEDyellow} LEDyellow2 is now without function. I think clean ticks does nothing in the case of "school book axes".
    – Michael.H
    Commented Apr 28, 2023 at 19:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .