0

I would like to use a matrix to create a legend in a plot. This matrix contains a cell whose width occupies two columns.

I have a problem with the positioning and alignment of the text of this particular cell.
The text is out of from the frame of the matrix and is not left-aligned, moreover, the text is not well positioned vertically (problem that I can solve manually using \\[...]). text width={...} and align=left don’t seem to work.

Here's my MWE :

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}
    \begin{semilogyaxis}[domain=0:4, samples=10]
        \addplot {x}; \label{graph1}
        \addplot {x^2}; \label{graph2}
        \addplot {x^(-1)}; \label{graph-1}
        \addplot {x^(-2)}; \label{graph-2}
        \coordinate (legend) at (axis description cs:1.05,1);
    \end{semilogyaxis}
    \matrix (matrix1) [draw, matrix of nodes, nodes in empty cells, anchor=north west] 
        at (legend) {
            \ref{graph1}&$x$\\
            \ref{graph2}&$x^2$\\%[1.8ex]
            &\\%[0.2ex]
            \ref{graph-1}&$x^{-1}$\\
            \ref{graph-2}&$x^{-2}$\\
        };
    \node[fit=(matrix1-3-1)(matrix1-3-2), 
        %text width={5cm}, 
        %align=left
        ]{%
            Bla bla bla bla :%
        };
\end{tikzpicture}
\end{document}

EDIT

This is what I have if I comment text width={...} and align=left : enter image description here

And this is what I have if I un comment text width={...} and align=left :enter image description here

The aim would be to have something like: enter image description here

In this particular example, the goal is easily achievable using a table, but my question is more a general question about tikz matrices.

2
  • Where do you want the text to appear? You have asked for it to appear on the third row of the matrix box, which is what it is doing. – user30471 Sep 6 '20 at 12:07
  • @Andrew I edited my question to add screenshots that illustrate my problem. – B Legrand Sep 7 '20 at 9:31
0

Do you like to have legend in the following format?

enter image description here

For this is not needed matrix7, sufivcient is to use pgfplotsmachinery forlegend`:

    legend pos=outer north east, % <---
    legend columns = 2,          % <---
    legend style = {font=\footnotesize,
                    style={column sep=0.5em}},
    legend cell align=left,      % <---

Complete MWE:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
    \begin{tikzpicture}
\begin{semilogyaxis}[
    legend pos=outer north east,
    legend columns = 2,
    legend style = {font=\footnotesize,
                    style={column sep=0.5em}},
    legend cell align=left,
%
    domain=0:4,
    samples=10
                ]
\addplot {x};
\addplot {x^2};
\addplot {x^(-1)};
\addplot {x^(-2)};
%
\legend{$x$, $x^2$, $x^{-1}$, $x^{-2}$}
\end{semilogyaxis}
    \end{tikzpicture}
\end{document}
2
  • Thank you for your help, but my question is not so much about how to make the legend, but rather a general question about Tikz matrices. – B Legrand Sep 7 '20 at 9:39
  • If you have general question about matrix, than you as as such, not that you are going to write a legend with it for tive) \pgfplots` diagram.For this pgfplots has (far more effective) own machinery. – Zarko Sep 7 '20 at 9:49

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.