我制作了以下图表:
我希望图例条目用实心方块表示(就像 一样ybar
)。在下面的代码中,我已经定义了我想要的 样式new ybar legend
。我该如何应用它?
生成上述内容的代码:
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}%<- use a smaller version if you have an older installation
\usepackage{color} % colors
\usepackage{xcolor}
\definecolor{cone}{RGB}{18,32,132} % blue
\definecolor{ctwo}{RGB}{255,153,0} % orange
\definecolor{ctwol}{RGB}{255, 192, 0} % lighter orange
\definecolor{cthree}{RGB}{0, 176, 80} % green
\definecolor{cfour}{RGB}{255, 0, 0} % red
\definecolor{cfive}{RGB}{0, 176, 240} % light blue
\begin{document}
\pgfplotstableread[col sep=comma]{
date, monmkt, jeez, haha, srr, others
2Q18, 90.5, 18.3, 1.1, 50.08, 13.87
3Q18, 102.75, 15.5, 1.65, 49.91, 10.11
4Q18, 107.91, 19.2, 0.86, 48.81, 8.22
1Q19, 97.23, 17.3, 0.34, 51.42, 20.58
2Q19, 81.99, 9.2, 1.12, 52.2, 24.01
3Q19, 92.76, 10.2, 1.53, 51.75, 14.95
4Q19, 109.16, 9, 1.64, 45.11, 3.98
1Q20, 117.49, 10, 2.39, 17.96, 10.97
}\charttwentyfour
\pgfplotsset{/pgfplots/new ybar legend/.style={
/pgfplots/legend image code/.code={%
\draw[##1,/tikz/.cd,yshift=-0.05cm, xshift = +0.38cm
]
(0cm,0cm) rectangle (0.6em,0.6em);},},
}
\begin{tikzpicture}
\small
\begin{axis}[
x=15mm,
bar width=3mm,
axis lines=left,
axis x line shift=0,
enlarge x limits={0.02, upper},
stack plots = y,
%
% y ticks style and label
ylabel={index},
ylabel shift = 1pt,
ymin = 1,
ytick distance = 20,
%
% x axis ticks and style
xticklabel shift=0cm,
xtick=data,
xticklabels from table={\charttwentyfour}{date},
table/x expr = \coordindex,
x tick label style = {rotate=0},
%
% legends and labels
legend cell align={left},
legend style = {fill = none, draw=none,
legend columns=2,
at={(0.5,1.15)},
anchor=north,
/tikz/every even column/.append style={column sep=2em},
},
]
%
% done with the axis, now the plots
\addplot [cone, fill=cone]
table [y=srr] from \charttwentyfour \closedcycle;
\addlegendentry{SRR};
\addplot [cfive, fill=cfive]
table [y=monmkt] from \charttwentyfour \closedcycle;
\addlegendentry{BRR};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
经过这么长时间的头脑风暴之后,终于可以轻易解决问题了。
new ybar legend
每个都添加addplot
即可。
例如:\addplot [new ybar legend, cone, fill=cone]
。