如何将条形图与图例对齐?

如何将条形图与图例对齐?

我能够使用下面提到的 tex 文件绘制条形图

\documentclass[landscape,a3paper,11pt]{article}
\usepackage{pstricks-add}
\usepackage{pgfplots, pgfplotstable}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
compat=newest,
ybar = 0.6,
ymajorgrids = true,
width=0.45\textwidth,
height=0.4\textheight,
enlarge y limits={upper, value=0.2},
ymin=0,
enlarge x limits = 0.2,
bar width=32pt,
title={ SCORE},
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=0},
ylabel={Marks},
symbolic x coords={Topic},
xtick=data,
nodes near coords,
axis lines*=left,
y axis line style={opacity=0},
yticklabels={\empty},
ytick style={draw=none},
cycle list={
    {fill=black!90,draw=black!90},
    {fill=black!70,draw=black!70},
    {fill=black!50,draw=black!50},
    {fill=black!30,draw=black!30}
},
axis on top,
major grid style=white,
ymajorgrids,
legend style={draw=none,/tikz/every even column/.append style={column sep=0.5cm}}
%nodes near coords align={horizontal},
]
\addplot coordinates {(Topic,40)};
\addplot coordinates {(Topic,50)};
\addplot coordinates {(Topic,60)};
\addplot coordinates {(Topic,70)};
\legend{My Marks,Avg Marks,Topper Marks,Total Marks}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

我想将图例与条形图对齐,如下图所示的红色突出显示的那样:

在此处输入图片描述

您能否建议如何在条形图之间添加空格,以确保它与图例对齐。

答案1

不要以为你还在用这个,但如果有人对这样做(不太方便)的方法感兴趣:可以为bar shift每个图添加一个自定义值。我通过修改 来做到这一点cycle list

如果您希望间距均匀,请bar shift从循环列表中删除所有 s,然后使用ybar=1cm(或其他长度) 代替ybar = 0.6

代码输出

\documentclass[landscape,a3paper,11pt]{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
ybar = 0.6,
ymajorgrids = true,
width=1.2\textwidth,
height=0.4\textheight,
enlarge y limits={upper, value=0.2},
ymin=0,
bar width=32pt,
title={ SCORE},
legend style={at={(0.5,-0.2)},
anchor=north,legend columns=0},
ylabel={Marks},
symbolic x coords={Topic},
xtick=data,
nodes near coords,
axis lines*=left,
y axis line style={opacity=0},
yticklabels={\empty},
ytick style={draw=none},
cycle list={
    {fill=black!90,draw=black!90,bar shift=-4.5cm,},
    {fill=black!70,draw=black!70,bar shift=-2cm,},
    {fill=black!50,draw=black!50,bar shift=1cm,},
    {fill=black!30,draw=black!30,bar shift=4cm,}
},
axis on top,
major grid style=white,
ymajorgrids,
legend style={draw=none,/tikz/every even column/.append style={column sep=0.5cm}}
%nodes near coords align={horizontal},
]
\addplot coordinates {(Topic,40)};
\addplot coordinates {(Topic,50)};
\addplot coordinates {(Topic,60)};
\addplot coordinates {(Topic,70)};
\legend{My Marks,Avg Marks,Topper Marks,Total Marks}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

只是为了好玩,采取一种完全不同的方法:

\documentclass[landscape,a3paper,11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{groupplot}[
group style={
  group size=4 by 1,
  group name=G,
},
ybar,
every axis/.append style={bar width=32pt},
width=0.28\textwidth,
height=0.4\textheight,
ymin=0,
ymax=80, % set same ymax for all groupplots
symbolic x coords={Topic},
xtick=data,
xticklabels={},
yticklabels={},
ytick style={draw=none},
ymajorgrids,
nodes near coords,
axis lines*=left,
axis line style={opacity=0},
cycle list={
    {fill=black!90,draw=black!90},
    {fill=black!70,draw=black!70},
    {fill=black!50,draw=black!50},
    {fill=black!30,draw=black!30}
},
axis on top,
major grid style=white,
legend style={
   at={(0.5,-0.2)},
   anchor=north,
   draw=none
}
]
\nextgroupplot[ylabel={Marks}]
\addplot coordinates {(Topic,40)};
\legend{My Marks}

\nextgroupplot
\pgfplotsset{cycle list shift=1}
\addplot coordinates {(Topic,50)};
\legend{Avg Marks}

\nextgroupplot
\pgfplotsset{cycle list shift=2}
\addplot coordinates {(Topic,60)};
\legend{Topper Marks}

\nextgroupplot
\pgfplotsset{cycle list shift=3}
\addplot coordinates {(Topic,70)};
\legend{Total Marks}
\end{groupplot}
\draw (G c1r1.south west) -- (G c4r1.south east);
\end{tikzpicture}
\end{center}
\end{document}

相关内容