7

I am trying to transfer this image from here into Tikz.

Plancks law

Can you please help me with the "rainbow"? As a starting point I found this code here:

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
    \begin{tikzpicture}[samples=100, scale=1.15]
    \begin{axis}[
        xmin=0,
        xlabel={$\omega$ [\si{\hertz}]},
        ymin=0,
        ymax=pi,
        ylabel={$\rho (\omega; T)$ [\si{\joule\per\cubic\meter}]},
        ytick=\empty,
        no markers,
        grid=both,domain=0.1:40,
        style={ultra thick}]
    \pgfplotsinvokeforeach{3000, 4000, 5000}
    {
        \addplot+
        {(x^3)/((pi^2)*(exp(2000*x/(#1))-1))};
        \addlegendentryexpanded{$T = #1 [\si{\kelvin}]$}
    }
    \end{axis}
    \end{tikzpicture}
\end{document}

which produces this: Tikz_Planck

EDIT: I found a related question regarding the rainbow with vertical shading here: Can somebody make this fit to the code in the answers section?

\documentclass{article}
\usepackage[named]{xcolor}
\usepackage{pgffor}
\usepackage{tikz}
\usetikzlibrary{shadings}

\pgfdeclareverticalshading{rainbow}{100bp}
{color(0bp)=(red); color(25bp)=(red); color(35bp)=(yellow);
color(45bp)=(green); color(55bp)=(cyan); color(65bp)=(blue);
color(75bp)=(violet); color(100bp)=(violet)}
\begin{document}
\begin{tikzpicture}
\shade[shading=rainbow,shading angle=270] (0,0) rectangle (5cm,\textheight);
\end{tikzpicture}
\end{document}

producing: rainbow with vertical shading

Thank you for your help!

4

The fillbetween library might be helpful:

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
    \begin{tikzpicture}[samples=100, scale=1.15]
    \begin{axis}[
        xmin=0,
        xlabel={$\omega$ [\si{\hertz}]},
        ymin=0,
        ymax=pi,
        ylabel={$\rho (\omega; T)$ [\si{\joule\per\cubic\meter}]},
        ytick=\empty,
        no markers,
        grid=both,domain=0.1:40,
        style={ultra thick}]

       \addplot+ [forget plot,name path=A] {(x^3)/((pi^2)*(exp(2000*x/(5000))-1))};
       \addplot [forget plot,name path=B,samples=2] {0};
        \addplot [forget plot,blue] fill between [of=A and B,soft clip={domain=5:6}];
        \addplot [forget plot,green] fill between [of=A and B,soft clip={domain=6:7}];
        \addplot [forget plot,yellow] fill between [of=A and B,soft clip={domain=7:8}];   
        \addplot [forget plot,orange] fill between [of=A and B,soft clip={domain=8:9}];             
        \addplot [forget plot,red] fill between [of=A and B,soft clip={domain=9:10}];


    \pgfplotsinvokeforeach{3000, 4000, 5000}
    {
        \addplot+ 
        {(x^3)/((pi^2)*(exp(2000*x/(#1))-1))};
        \addlegendentryexpanded{$T = #1 [\si{\kelvin}]$}
    }


    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • Tip: add forget plot to the first four \addplots, then they won't influence the cycle list or legend. – Torbjørn T. Jul 23 at 11:17
  • @TorbjørnT. Thank you! Very good idea! – sive Jul 23 at 11:20
4

fillbetween isn't really necessary, \closedcycle appended to the end of an \addplot lets you fill the area below a plot. Obviously you'll need to decide on better colours and domains for the filled regions, but as an example:

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
    \begin{tikzpicture}[
      samples=100,
      declare function={
        planck(\x,\T)=(\x^3)/((pi^2)*(exp(2000*\x/(\T))-1));
      }]
    \begin{axis}[
        xmin=0,
        xlabel={$\omega$ [\si{\hertz}]},
        ymin=0,
        ymax=pi,
        ylabel={$\rho (\omega; T)$ [\si{\joule\per\cubic\meter}]},
        ytick=\empty,
        no markers,
        grid=both,domain=0.1:40,
        style={ultra thick}]

    \begin{scope}[every axis plot/.append style={forget plot, draw=none, fill}]
      \addplot [blue, domain=5:6] {planck(x,5000)} \closedcycle;
      \addplot [green, domain=6:7] {planck(x,5000)} \closedcycle;
      \addplot [red, domain=7:8] {planck(x,5000)} \closedcycle;
    \end{scope}
    \pgfplotsinvokeforeach{3000, 4000, 5000}
    {
        \addplot {planck(x,#1)};
        \addlegendentryexpanded{$T = #1 [\si{\kelvin}]$}
    }

    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

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.