4

I'm trying to plot a data matrix using pgfplots and a surface plot.

The method described in this question worked well for me: Using pgfplots, how do I arrange my data matrix for a surface plot so that each cell in the matrix is plotted as a square?

Each square should be quadratic, so I tried to set the sizes by using "x = 1cm, y = 1cm", as described in this answer: https://tex.stackexchange.com/a/31028/29619

Unfortunately, this results in a mangled plot and a warning.

This code compiles to a flat plot with non-square rectangles:

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}

\pgfplotstableread{
    0 0 0.77
    0 1 0

    1 0 0
    1 1 0
}\data

\begin{tikzpicture}
   \begin{axis}[
    width=5cm,
    title={\texttt{Minimal purity plot}},
    view={0}{90},
    colormap={bluenew}{rgb255(0cm)=(255,255,255); rgb255(1cm)=(40,40,240)},
    colorbar,
    colorbar style={
            at={(1.5, 0.5)},
            anchor=west,
            xticklabel pos=right,
    },
    xmin=0,
    xmax=1,
    ymin=0,
    ymax=1,
    xtick=data,
    ytick=data,
    point meta min=0,
    point meta max=1,
    xlabel=x,
    ylabel=y]
\addplot3 [surf, shader=flat corner] table {\data};
\end{axis}
\end{tikzpicture}
\end{document}

When I add "x = 1cm, y = 1cm" to the axis settings, a warning occurs about a missing size for set. If I add "z = 1cm" as well, it looks like a 3D-box again.

It ought to look like in the MWE, but with different sizes. Is there a way to do this?

Changing the width in the axis description did not help.

EDIT Thanks to percusse's suggestion, the cells are now squares. I'd still like to have different plots with consistent cell sizes accross plots.

\pgfplotstableread{
    0 0 0.77
    0 1 0.5
0 2 0

    1 0 0
    1 1 0.3
1 2 0

2 0 0.9
2 1 0
2 2 0
}\datan

\begin{tikzpicture}
\begin{axis}[
title={\texttt{Second working plot}},
    view={0}{90},
    colormap={bluenew}{rgb255(0cm)=(255,255,255); rgb255(1cm)=(40,40,240)},
    colorbar,
    colorbar style={
            at={(1.5, 0.5)},
            anchor=west,
            xticklabel pos=right,
    },
    xmin=0,
    xmax=2,
    ymin=0,
    ymax=2,
    xtick=data,
    ytick=data,
    point meta min=0,
    point meta max=1,
    xlabel=x,
    ylabel=y,
axis equal]
\addplot3 [surf, shader=flat corner] table {\datan};
\end{axis}
\end{tikzpicture}

In this second example, the cells are much smaller, since more of them are fitted in the same plot. I'd like the cell sizes to be consistent and the plot size to change instead.

  • 1
    Are you looking for axis equal? – percusse Aug 5 '13 at 13:30
  • @percusse Almost! axis equal helps in making the fields quadratic. I'd still like to set them to a specific size for better comparability between plots. – mektah Aug 5 '13 at 13:35
  • 1
    Hmm Can you elaborate on what you mean by specific size ? I'm not too familiar with the terminology so it would be great if you can include a quick sketch of your goal – percusse Aug 5 '13 at 13:42
3

You can set x=1cm, y=1cm, z=0cm to get a 2D plot with fixed unit vectors:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\pgfplotsset{
    colormap={bluenew}{rgb255(0cm)=(255,255,255); rgb255(1cm)=(40,40,240)},
    colorbar,
    colorbar style={
            at={(rel axis cs:1,0)},
            anchor=south west,
            xticklabel pos=right,
            try min ticks=3,
            height=1.5cm,
            x=0.25cm
    },
    xtick=data,
    ytick=data,
    point meta min=0,
    point meta max=1,
    x=1.5cm, y=1.5cm,z=0cm,
    xlabel=x,
    ylabel=y
}
\pgfplotstableread{
    0 0 0.77
    0 1 0

    1 0 0
    1 1 0
}\data

\begin{tikzpicture}
   \begin{axis}[
    title={\texttt{Minimal purity plot}}]
\addplot3 [surf, shader=flat corner] table {\data};
\end{axis}
\end{tikzpicture}

\pgfplotstableread{
    0 0 0.77
    0 1 0.5
0 2 0

    1 0 0
    1 1 0.3
1 2 0

2 0 0.9
2 1 0
2 2 0
}\datan

\begin{tikzpicture}
\begin{axis}[
title={\texttt{Second working plot}}]
\addplot3 [surf, shader=flat corner] table {\datan};
\end{axis}
\end{tikzpicture}
\end{document}

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.