我制作了以下图表。我希望不同图例的图标/符号不仅仅是一条小线。我希望它是图形中带有填充颜色的正方形。代码如下:
\newcommand{\TwoDfill}[3]{
\begin{axis}[
smooth,
stack plots=y,
area style,
enlarge x limits=false,
ybar,
title={\Large{#2}},
bar width=.3cm,
width=.95\textwidth,
height=\textwidth,
legend style={at={(0.5,1)},
anchor=north,legend columns=-1},
symbolic x coords={1961--1972,1973--1978,1979--1998,1999--2006,2007--2010,2011+},
xtick=data,
ymin=0,ymax=#3,
ylabel={Number of housings},
]
\addplot table[x=interval,y=clA]{#1}\closedcycle;
\addplot table[x=interval,y=clB]{#1}\closedcycle;
\addplot table[x=interval,y=clC]{#1}\closedcycle;
\addplot table[x=interval,y=clD]{#1}\closedcycle;
\addplot table[x=interval,y=clE]{#1}\closedcycle;
\addplot table[x=interval,y=clF]{#1}\closedcycle;
\addplot table[x=interval,y=clG]{#1}\closedcycle;
\legend{Class A, Class B, Class C, Class D, Class E, Class F, Class G,}
\end{axis}
}
\pgfplotstableread[row sep=\\,col sep=&]{
interval & clA & clB & clC & clD & clE & clF & clG\\
1961--1972 & 39 & 225 & 6997 & 21265 & 11513 & 3490 & 707\\
1973--1978 & 23 & 153 & 6331 & 10597 & 2381 & 306 & 46\\
1979--1998 & 49 & 555 & 10603 & 3408 & 247 & 30 & 15\\
1999--2006 & 4211 & 4211 & 3008 & 4211 & 4 & 2 & 1\\
2007--2010 & 507 & 6552 & 829 & 26 & 4 & 1 & 0\\
2011+ & 12350 & 1613 & 26 & 5 & 1 & 0 & 0\\
}\pchfv
\begin{tikzpicture}
\TwoDfill{\pchfv}{Single-family house - District Heating}{45000}
\end{tikzpicture}
如何在图例中制作方形符号/图标而不是线条?
答案1
您可以创建自己的风格。我所做的只是调整 pgfplots 手册第 262 页上的示例。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{% basis: example on p. 262 of the pgfplots manual
/pgfplots/area legend/.style={
/pgfplots/legend image code/.code={
\fill[##1] (0cm,0.6em) rectangle (2*\pgfplotbarwidth,-0.3em);
}, },
}
\begin{document}
\newcommand{\TwoDfill}[3]{
\begin{axis}[
smooth,
stack plots=y,
area style,
enlarge x limits=false,
ybar,
title={\Large{#2}},
bar width=.3cm,
width=.95\textwidth,
height=\textwidth,
legend style={at={(0.5,1)},
anchor=north,legend columns=-1},
symbolic x coords={1961--1972,1973--1978,1979--1998,1999--2006,2007--2010,2011+},
xtick=data,area legend, %<-added
ymin=0,ymax=#3,
ylabel={Number of housings},
]
\addplot table[x=interval,y=clA]{#1}\closedcycle;
\addplot table[x=interval,y=clB]{#1}\closedcycle;
\addplot table[x=interval,y=clC]{#1}\closedcycle;
\addplot table[x=interval,y=clD]{#1}\closedcycle;
\addplot table[x=interval,y=clE]{#1}\closedcycle;
\addplot table[x=interval,y=clF]{#1}\closedcycle;
\addplot table[x=interval,y=clG]{#1}\closedcycle;
\legend{Class A, Class B, Class C, Class D, Class E, Class F, Class G,}
\end{axis}
}
\pgfplotstableread[row sep=\\,col sep=&]{
interval & clA & clB & clC & clD & clE & clF & clG\\
1961--1972 & 39 & 225 & 6997 & 21265 & 11513 & 3490 & 707\\
1973--1978 & 23 & 153 & 6331 & 10597 & 2381 & 306 & 46\\
1979--1998 & 49 & 555 & 10603 & 3408 & 247 & 30 & 15\\
1999--2006 & 4211 & 4211 & 3008 & 4211 & 4 & 2 & 1\\
2007--2010 & 507 & 6552 & 829 & 26 & 4 & 1 & 0\\
2011+ & 12350 & 1613 & 26 & 5 & 1 & 0 & 0\\
}\pchfv
\begin{tikzpicture}
\TwoDfill{\pchfv}{Single-family house - District Heating}{45000}
\end{tikzpicture}
\end{document}
编辑:如果您担心这样图例会变得太宽,您可以按照如下方法修复此问题。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{% basis: example on p. 262 of the pgfplots manual
/pgfplots/area legend/.style={
/pgfplots/legend image code/.code={
\fill[##1] (0cm,0.6em) rectangle (0.9em,-0.3em);
}, },
}
\begin{document}
\newcommand{\TwoDfill}[3]{
\begin{axis}[
smooth,
stack plots=y,
area style,
enlarge x limits=false,
ybar,
title={\Large{#2}},
bar width=.3cm,
width=.95\textwidth,
height=\textwidth,
legend style={at={(0.5,1)},
anchor=north,legend columns=2,transpose legend},
symbolic x coords={1961--1972,1973--1978,1979--1998,1999--2006,2007--2010,2011+},
xtick=data,area legend, %<-added
ymin=0,ymax=#3,
ylabel={Number of housings},
]
\addplot table[x=interval,y=clA]{#1}\closedcycle;
\addplot table[x=interval,y=clB]{#1}\closedcycle;
\addplot table[x=interval,y=clC]{#1}\closedcycle;
\addplot table[x=interval,y=clD]{#1}\closedcycle;
\addplot table[x=interval,y=clE]{#1}\closedcycle;
\addplot table[x=interval,y=clF]{#1}\closedcycle;
\addplot table[x=interval,y=clG]{#1}\closedcycle;
\legend{Class A, Class B, Class C, Class D, Class E, Class F, Class G,}
\end{axis}
}
\pgfplotstableread[row sep=\\,col sep=&]{
interval & clA & clB & clC & clD & clE & clF & clG\\
1961--1972 & 39 & 225 & 6997 & 21265 & 11513 & 3490 & 707\\
1973--1978 & 23 & 153 & 6331 & 10597 & 2381 & 306 & 46\\
1979--1998 & 49 & 555 & 10603 & 3408 & 247 & 30 & 15\\
1999--2006 & 4211 & 4211 & 3008 & 4211 & 4 & 2 & 1\\
2007--2010 & 507 & 6552 & 829 & 26 & 4 & 1 & 0\\
2011+ & 12350 & 1613 & 26 & 5 & 1 & 0 & 0\\
}\pchfv
\begin{tikzpicture}
\TwoDfill{\pchfv}{Single-family house - District Heating}{45000}
\end{tikzpicture}
\end{document}
答案2
您可以尝试使用类似以下的方法:
\addlegendimage{color=black,mark=*,only marks,line width=2pt}