向 pgfplotstable 添加密度图

向 pgfplotstable 添加密度图

我能够复制这个例子http://www.pgfplots.org/tikz/examples/graph-in-table/。现在我想问两个具体的问题:

  1. 如何在行之间添加适用于图表的垂直空间?
  2. 有没有办法在每个间隔上绘制正常密度?

谢谢!

答案1

您可以更改\arraystretch。要同步绘图,您需要重新定义yy=-\arraystretch\baselineskip。我还添加了一些以均值为中心的高斯曲线,其宽度由误差控制。

\documentclass[border=10pt]{standalone}
%%%<
\usepackage{verbatim}
\usepackage{filecontents}
\begin{filecontents}[overwrite]{data.txt}
name z p mean lci uci
Afear -0.96  0.33 -0.42 -1.28 0.44
Anofear 0.09 0.93 0.04 -0.85 0.94
B+2 0.29 0.78 0.10 -0.59 0.79
B+1   0.84  0.40  0.30 -0.40 1.00 
B1:1   2.19  0.03  0.80 0.08 1.52 
B-1   1.02  0.31  0.37 -0.33 1.07 
B-2   -0.10  0.92  -0.03 -0.72 0.65 
C+2   -1.11  0.27  -0.30 -0.83 0.23 
C+1   1.15   0.25  0.32 -0.22 0.86 
C1:1   -1.34  0.18  -0.38 -0.93 0.17 
C-1   0.43  0.67  0.12 -0.42 0.66 
C-2   -0.37  0.71  -0.10 -0.63 0.43 
D+2   0.41  0.68  0.12 -0.44 0.67 
D+1   -0.69  0.49  -0.20 -0.77 0.37 
D1:1   -1.33  0.18  -0.39 -0.97 0.19 
D-1   -1.21  0.23  -0.35 -0.92 0.22 
D-2   0.32  0.75  0.09 -0.46 0.65 
\end{filecontents}
%%%>
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{multirow}
\begin{comment}
:Title: Graph within a table
:Tags: 2D;PGFPlotstable;Styles
:Author: Jake
:Slug: graph-in-table

We would like to plot a graph within a table column, similar to
http://texample.net/tikz/examples/weather-stations-data/ .

We will use the booktabs package for good table design,
and the multirow package.

The data is read using PGFPlotstable and the plot is typeset dynamically.

This code was written by Jake on TeX.SE.
\end{comment}

% Read data file, create new column ``upper CI boundary - mean''
\pgfplotstableread{data.txt}\data
\pgfplotstableset{create on use/error/.style={
    create col/expr={\thisrow{uci}-\thisrow{mean}
    }
  }
}


% Define the command for the plot
\newcommand{\errplot}{%
  \begin{tikzpicture}[trim axis left,trim axis right]
    \begin{axis}[y=-\arraystretch\baselineskip,% <- added \arraystretch
        scale only axis,
        width             = 6.5cm,
        enlarge y limits  = {abs=0.01},%<-
        axis y line*      = middle,
        y axis line style = dashed,
        ytick             = \empty,
        axis x line*      = bottom
      ]
      % ``mean'' must be present in the datafile,
      %``error'' is the newly generated column
      \addplot+[only marks][error bars/.cd,x dir=both, x explicit]
        table [x=mean,y expr=\coordindex,x error=error]{\data};
      \pgfplotsforeachungrouped\irow in {0,...,\the\numexpr\numberofrows-1}
      {\pgfplotstablegetelem{\irow}{error}\of\data
      \let\myerror\pgfplotsretval
      \pgfplotstablegetelem{\irow}{mean}\of\data
      \let\mymean\pgfplotsretval        
      \addplot[forget plot,domain=-1.5:1.5] 
      {0.5+\irow-exp(-(x-\mymean)*(x-\mymean)/(2*\myerror*\myerror))};
      }
    \end{axis}
  \end{tikzpicture}%
}

\begin{document}
% Get number of rows in datafile
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval

% Print the table
\renewcommand\arraystretch{1.4}
\pgfplotstabletypeset[columns={name,error,z,p,mean,ci},
  % Booktabs rules
  every head row/.style = {before row=\toprule, after row=\midrule},
  every last row/.style = {after row=[3ex]\bottomrule},
  % Set header name
  columns/name/.style = {string type, column name=Name},
  % Use the ``error'' column to call the \errplot command in a multirow cell
  % in the first row, keep empty for all other rows
  columns/error/.style = {
    column name = {},
    assign cell content/.code = {% use \multirow for Z column:
    \ifnum\pgfplotstablerow=0
    \pgfkeyssetvalue{/pgfplots/table/@cell content}
    {\multirow{\numberofrows}{6.5cm}{\errplot}}%
    \else
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
    \fi
    }
  },
  % Format numbers and titles
  columns/mean/.style = {column name = Mean, fixed ,fixed zerofill, dec sep align},
  columns/z/.style    = {column name = $z$, fixed, fixed zerofill, dec sep align},
  columns/p/.style    = {column name = $p$, fixed, fixed zerofill, dec sep align},
  columns/ci/.style   = {string type, column name = 95\% CI},
  % Create the ``(x to y)'' format, use \pgfmathprintnumber with `showpos`
  % to make things align nicely
  create on use/ci/.style={
    create col/assign/.code={\edef\value{(
      \noexpand\pgfmathprintnumber[showpos,fixed,fixed zerofill]{\thisrow{lci}}
      to \noexpand\pgfmathprintnumber[showpos,fixed,fixed zerofill]{\thisrow{uci}})}
      \pgfkeyslet{/pgfplots/table/create col/next content}\value
    }
  }
]{\data}
\end{document}

在此处输入图片描述

相关内容