根据数据表在条形图上绘制一条线

根据数据表在条形图上绘制一条线

我有以下代码:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\bein{document}
\begin{figure}
 \centering
\makeatletter
\pgfplotsset{
 calculate offset/.code={
 \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
 \pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
 \pgfkeys{/pgf/fpu=false}
 },
 every node near coord/.style={
 /pgfplots/calculate offset,
 yshift=-\testmacro
 }
}
\pgfplotstableread{
1 829 794 552 401
2 1011 1104 843 620
3 658 755 546 428
}\dataset
\pgfplotstableread{
1 425 404 319 227
2 511 575 411 341
3 327 386 298 211
}\correct
\begin{tikzpicture}
\begin{axis}[ybar,
 bar width=10pt,
 enlarge x limits={abs=0.9},
 width=12cm,
 height=6cm,
 ymin=0,
 ymax=1200, 
 ylabel={Frequency},
 xtick=data,
 xticklabels = {
 Gating1,
 Gating2,
 Gating3,
 },
 xlabel=Stimuli Condition,
 xticklabel style={yshift=-10ex},
 major x tick style = {opacity=0},
 minor x tick num = 1,
 minor tick length=1ex,
 every node near coord/.append style={
 anchor=east,
 rotate=90
 }
 ]
\addplot[draw=black,fill=blue!20, nodes near coords=Active] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=red!20, nodes near coords=Interactive] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=green!20, nodes near coords=Passive] table[x index=0,y index=3] \dataset;
\addplot[draw=black,fill=yellow!20, nodes near coords=None] table[x index=0,y index=4] \dataset;
\end{axis}
\begin{axis}
[ybar,
 enlarge x limits={abs=0.9},
 width=12cm,
 height=6cm,
 ymin=0,
 ymax=1200, 
 xtick=data,
 xticklabels = {
 },
 ]
\addplot[draw=blue,ultra thick,only marks,mark=-,mark size=25pt] table[x index=0,y index=1] \correct;
\addplot[draw=blue,ultra thick,only marks,mark=-,mark size=25pt] table[x index=0,y index=2] \correct;
\addplot[draw=blue,ultra thick,only marks,mark=-,mark size=25pt] table[x index=0,y index=3] \correct;
\addplot[draw=blue,ultra thick,only marks,mark=-,mark size=25pt] table[x index=0,y index=4] \correct;

\end{axis}
\end{tikzpicture}
\caption{Frequency and Proportion of Statement Responses by Stimuli Type and Gating Condition}\label{Statements}
\end{figure}
\end{document}

并且条形图部分本身是完美的,颜色、间距等都很好。我在添加线条时遇到了问题。第二个数据框“正确”,应该位于条形图上方,并绘制一条带有曲线的线条。相反,它位于右侧,作为图表上的一组额外数据,并给出水平线。我希望最终得到这样的结果,线条位于条形图上方,但希望线条更平滑。在此处输入图片描述

在此处输入图片描述

答案1

这是你想要的?

演示

\documentclass{standalone}
%\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{document}
\makeatletter
\pgfplotsset{
 calculate offset/.code={
 \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
 \pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
 \pgfkeys{/pgf/fpu=false}
 },
 every node near coord/.style={
 /pgfplots/calculate offset,
 yshift=-\testmacro
 }
}
\pgfplotstableread{
1 829 794 552 401
2 1011 1104 843 620
3 658 755 546 428
}\dataset
\pgfplotstableread{
1 425 404 319 227
2 511 575 411 341
3 327 386 298 211
}\correct

\begin{tikzpicture}
\begin{axis}[ybar,
 bar width=10pt,
 enlarge x limits={abs=0.9},
 width=12cm,
 height=6cm,
 ymin=0,
 ymax=1200, 
 ylabel={Frequency},
 xtick=data,
 xticklabels = {
 Gating1,
 Gating2,
 Gating3,
 },
 xticklabel style={yshift=-10ex},
 xlabel=Stimuli Condition,
 xlabel style={yshift=-10ex},
 major x tick style = {opacity=0},
 minor x tick num = 1,
 minor tick length=1ex,
 every node near coord/.append style={
 anchor=east,
 rotate=90
 }
 ]
\addplot[draw=black,fill=blue!20, nodes near coords=Active] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=red!20, nodes near coords=Interactive] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=green!20, nodes near coords=Passive] table[x index=0,y index=3] \dataset;
\addplot[draw=black,fill=yellow!20, nodes near coords=None] table[x index=0,y index=4] \dataset;
\end{axis}
\begin{axis}
[enlarge x limits={abs=0.9},
 width=12cm,
 height=6cm,
 ymin=0,
 ymax=1200, 
 xtick=data,
 xticklabels = {
 },
 ]
\addplot[draw=blue,ultra thick, xshift=-18pt] table[x index=0,y index=1] \correct;
\addplot[draw=red,ultra thick,xshift=-6pt] table[x index=0,y index=2] \correct;
\addplot[draw=green,ultra thick,xshift=6pt] table[x index=0,y index=3] \correct;
\addplot[draw=yellow,ultra thick,xshift=18pt] table[x index=0,y index=4] \correct;

\end{axis}
\end{tikzpicture}
\end{document}

使用 修改解决方案\pgfplotstabletranspose。我使用坐标 (A) 到 (C) 存储起点。xshift 将数据点置于条形图的中心,xscale 是 WAG。我尝试计算比例因子,但没有成功。

演示2

\documentclass{standalone}
%\usepackage[utf8]{inputenc}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}

\newlength{\tempdima}

\begin{document}
\makeatletter
\pgfplotsset{
 calculate offset/.code={
 \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
 \pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
 \pgfkeys{/pgf/fpu=false}
 },
 every node near coord/.style={
 /pgfplots/calculate offset,
 yshift=-\testmacro
 }
}
\pgfplotstableread{
1 829 794 552 401
2 1011 1104 843 620
3 658 755 546 428
}\dataset
\pgfplotstableread{
1 425 404 319 227
2 511 575 411 341
3 327 386 298 211
}\correct
%\pgfplotstabletypeset{\correct}
\pgfplotstabletranspose[colnames from=0]{\transpose}{\correct}%
%\pgfplotstabletypeset{\transpose}
\begin{tikzpicture}
\begin{axis}[ybar,
 bar width=10pt,
 enlarge x limits={abs=0.9},
 width=12cm,
 height=6cm,
 ymin=0,
 ymax=1200, 
 ylabel={Frequency},
 xtick=data,
 xticklabels = {
 Gating1,
 Gating2,
 Gating3,
 },
 xticklabel style={yshift=-10ex},
 xlabel=Stimuli Condition,
 xlabel style={yshift=-10ex},
 major x tick style = {opacity=0},
 minor x tick num = 1,
 minor tick length=1ex,
 every node near coord/.append style={
 anchor=east,
 rotate=90
 }
 ]
\addplot[draw=black,fill=blue!20, nodes near coords=Active] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=red!20, nodes near coords=Interactive] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=green!20, nodes near coords=Passive] table[x index=0,y index=3] \dataset;
\addplot[draw=black,fill=yellow!20, nodes near coords=None] table[x index=0,y index=4] \dataset;
\coordinate (A) at (axis cs: 0,0);
\coordinate (B) at (axis cs: 1,0);
\coordinate (C) at (axis cs: 2,0);
\end{axis}
\begin{axis}
[enlarge x limits={abs=0.9},
 width=12cm,
 height=6cm,
 ymin=0,
 ymax=1200, 
 hide axis
 ]
\addplot[draw=black,ultra thick,shift=(A),xshift=4pt,xscale=0.2] table[x index=0,y index=1] \transpose;
\addplot[draw=black,ultra thick,shift=(B),xshift=4pt,xscale=0.2] table[x index=0,y index=2] \transpose;
\addplot[draw=black,ultra thick,shift=(C),xshift=4pt,xscale=0.2] table[x index=0,y index=3] \transpose;

\end{axis}
\end{tikzpicture}
\end{document}

相关内容