我需要为图中的每种颜色添加图例。例如,在图中插入一个包含颜色及其名称的框:
我怎样才能做到这一点?
梅威瑟:
\documentclass{standalone}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{positioning}
\pgfplotsset{compat=1.16,width=0.98\textwidth}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{microtype}
\usepackage{lmodern}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\definecolor{darkgray176}{RGB}{176,176,176}
\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xtick style={color=black},
y grid style={darkgray176},
ytick style={color=black},
yticklabels={,,},
xticklabels={,,},
xtick align=inside,
ytick align=inside,
grid = none
]
\addplot [colormap/viridis, only marks, scatter, scatter src=explicit]
table [x=x, y=y, meta=colordata]{%
x y colordata
5.1 3.5 0.0
4.9 3 0.0
4.7 3.2 0.0
4.6 3.1 0.0
5 3.6 0.0
5.4 3.9 0.0
4.6 3.4 0.0
5 3.4 0.0
4.4 2.9 0.0
4.9 3.1 0.0
5.4 3.7 0.0
4.8 3.4 0.0
4.8 3 0.0
4.3 3 0.0
5.8 4 0.0
5.7 4.4 0.0
5.4 3.9 0.0
5.1 3.5 0.0
5.7 3.8 0.0
5.1 3.8 0.0
5.4 3.4 0.0
5.1 3.7 0.0
4.6 3.6 0.0
5.1 3.3 0.0
4.8 3.4 0.0
5 3 0.0
5 3.4 0.0
5.2 3.5 0.0
5.2 3.4 0.0
4.7 3.2 0.0
4.8 3.1 0.0
5.4 3.4 0.0
5.2 4.1 0.0
5.5 4.2 0.0
4.9 3.1 0.0
5 3.2 0.0
5.5 3.5 0.0
4.9 3.6 0.0
4.4 3 0.0
5.1 3.4 0.0
5 3.5 0.0
4.5 2.3 0.0
4.4 3.2 0.0
5 3.5 0.0
5.1 3.8 0.0
4.8 3 0.0
5.1 3.8 0.0
4.6 3.2 0.0
5.3 3.7 0.0
5 3.3 0.0
7 3.2 1.0
6.4 3.2 1.0
6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.0
5.8 2.7 2.0
6.8 3.2 2.0
6.7 3.3 2.0
6.7 3 2.0
6.3 2.5 2.0
6.5 3 2.0
6.2 3.4 2.0
5.9 3 2.0
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
你可以利用可以访问颜色图的颜色这一事实。这是通过
color of colormap=#1 of viridis
键,其中的参数表示使用哪种颜色。0
表示最小值和1000
最大值。由于您的元值范围从 0 到 2,这意味着 0 映射到 0,1 映射到 500,2 映射到 1000。然后您可以手动创建图例。
\documentclass{standalone}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{matrix,positioning,shadows.blur}
\pgfplotsset{compat=1.16,width=0.98\textwidth}
\begin{document}
\begin{tikzpicture}[crect/.style={inner xsep=1em,inner ysep=0.5ex,
fill,color of colormap=#1 of viridis},crect/.default=0]
\definecolor{darkgray176}{RGB}{176,176,176}
\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xtick style={color=black},
y grid style={darkgray176},
ytick style={color=black},
yticklabels={,,},
xticklabels={,,},
xtick align=inside,
ytick align=inside,
grid = none
]
\addplot [colormap/viridis, only marks, scatter, scatter src=explicit]
table [x=x, y=y, meta=colordata]{%
x y colordata
5.1 3.5 0.0
4.9 3 0.0
4.7 3.2 0.0
4.6 3.1 0.0
5 3.6 0.0
5.4 3.9 0.0
4.6 3.4 0.0
5 3.4 0.0
4.4 2.9 0.0
4.9 3.1 0.0
5.4 3.7 0.0
4.8 3.4 0.0
4.8 3 0.0
4.3 3 0.0
5.8 4 0.0
5.7 4.4 0.0
5.4 3.9 0.0
5.1 3.5 0.0
5.7 3.8 0.0
5.1 3.8 0.0
5.4 3.4 0.0
5.1 3.7 0.0
4.6 3.6 0.0
5.1 3.3 0.0
4.8 3.4 0.0
5 3 0.0
5 3.4 0.0
5.2 3.5 0.0
5.2 3.4 0.0
4.7 3.2 0.0
4.8 3.1 0.0
5.4 3.4 0.0
5.2 4.1 0.0
5.5 4.2 0.0
4.9 3.1 0.0
5 3.2 0.0
5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
6.2 2.8 2.0
6.1 3 2.0
6.4 2.8 2.0
7.2 3 2.0
7.4 2.8 2.0
7.9 3.8 2.0
6.4 2.8 2.0
6.3 2.8 2.0
6.1 2.6 2.0
7.7 3 2.0
6.3 3.4 2.0
6.4 3.1 2.0
6 3 2.0
6.9 3.1 2.0
6.7 3.1 2.0
6.9 3.1 2.0
5.8 2.7 2.0
6.8 3.2 2.0
6.7 3.3 2.0
6.7 3 2.0
6.3 2.5 2.0
6.5 3 2.0
6.2 3.4 2.0
5.9 3 2.0
};
\end{axis}
\matrix[matrix of nodes,matrix anchor=north east,
cells={nodes={anchor=west,sharp corners}},
fill=white,rounded corners,blur shadow]
at ([xshift=-1ex,yshift=-1ex]current axis.north east)
{
|[crect=0]| & some entry \\
|[crect=500]| & another entry \\
|[crect=1000]| & new entry \\
};
\end{tikzpicture}
\end{document}