我想要生成像这样的带有小计的条形图(这里是菱形):
我尝试使用第二个轴来解决小计问题,但数据点并不在各个 x 坐标的中间。
\documentclass[
12pt,
a4paper,
]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[dvipsnames,table,xcdraw]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
width=0.7\textwidth,
ybar stacked,
bar width=20pt,
enlargelimits=0.15,
legend style={at={(0.5,-0.1)},
anchor=north,legend columns=0},
ylabel={Values},
symbolic x coords={A,B,C,D,E},
xtick=data,
]
\addplot+[fill=CornflowerBlue, draw=black,ybar] plot coordinates {(A,25.959952) (B,25.959952) (C,98.703228) (D,64.219624) (E,64.219624)}; % 1
\addplot+[fill=orange, draw=black,ybar] plot coordinates {(A,10.383981) (B,10.383981) (C,39.481291) (D,25.687850) (E,25.687850)}; % 2
\addplot+[fill=YellowGreen, draw=black,ybar] plot coordinates {(A,9.085938) (B,9.085938) (C,0) (D,0) (E,0)}; % 3
\addplot+[fill=Periwinkle, draw=black,ybar] plot coordinates {(A,3.634393) (B,3.634393) (C,0) (D,0) (E,0)}; % 4
\addplot+[fill=Goldenrod, draw=black,ybar] plot coordinates {(A,6.467643) (B,6.467643) (C,7.796889) (D,9.716245) (E,7.807715)}; % 5
\legend{1,2,3,4,5}
\addlegendimage{Lavender, mark=*, only marks, draw=black, mark size=2pt, yshift=-.5em}
\addlegendentry{subtotals}
\end{axis}
\begin{axis}[
width=0.7\textwidth,
axis y line*=right,
axis x line=none,
axis y line=none,
ymin=0, ymax=160,
ytick=\empty,
xtick=\empty,
symbolic x coords={A,B,C,D,E},
xtick=data
]
\addplot [Lavender,mark=*, only marks, draw=black,mark size=2pt] plot coordinates {(A,49.064310) (B,49.064310) (C,138.184519)(D,89.907473) (E,89.907473)}; % subtotals
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}