2

Usually, when a colorbar is attached to a graph, the colorbar shows color changes in relation to the values in the y-axis. Is there a way to display color changes according to the x-axis values? For example, in the colorbar you can see how the color changes according to the milliliters?

enter image description here

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage[italian]{babel}
\usepackage[
  a4paper,
  margin=15mm,
  bindingoffset=2mm,
  heightrounded,
]{geometry}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{float}
\usepackage{pgfplots}
\usepackage{xcolor}
\usepgfplotslibrary{external}
\tikzexternalize
\pgfplotsset{width=7cm,compat=1.14}

\begin{document}
\begin{flushleft}
\begin{tikzpicture}
\begin{axis}[
     colorbar,
     width=12cm,
     xmin=0, xmax=21, 
     ymin=0, ymax=14,
     xlabel=ml,
     ylabel=pH,
     xmajorgrids=true,
     ymajorgrids=true,
     grid style=dashed,
     ytick={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14},
     xtick={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},
    ]

\pgfplotsset{
colormap={redblue}{color(0pt)=(white); color(62pt)=(white); color(63pt)=(magenta); color(100pt)=(magenta)}
}
    \addplot+[scatter,color=black,ultra thick]
    coordinates {
    (1,3.61)(2,3.99)(3,4.16)(4,4.33)
    (5,4.52)(6,4.64)(7,4.80)(8,4.91)(9,5.06)(10,5.24)(10.5,5.38)(11,5.49)(11.5,5.67)(12,5.82)(12.2,5.99)(12.4,6.09)(12.6,6.46)(12.8,6.98)(13,7.35)(13.2,10.92)(13.4,11.22)(13.6,11.37)(13.8,11.50)(14,11.58)
    (14.5,11.78)(15,11.83)(15.5,11.91)(16,11.97)(17,12.08)(18,12.13)(19,12.18)(20,12.22)
    };
\end{axis}
\end{tikzpicture}
\end{flushleft}
\end{document}
3

This can be achieved by using the tikz option point meta=x, which you can put with the other options after \begin{axis}.

If you also want your colorbar to be horizontally below the x-axis, you can use the option colorbar horizontal instead of colorbar.

  • For the record: point meta is a PGFPlots key (and not a TikZ key). – Stefan Pinnow May 15 '17 at 18:08

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.