如何使用 pgfplots 绘制布尔(数字)变量?

如何使用 pgfplots 绘制布尔(数字)变量?

我从事电力系统工作,总是需要绘制电压和电流信号。有时我需要绘制布尔信号。我专门使用 pgfplots 和 groupplots 来完成这项工作,如第一张图所示(标题为葡萄牙语),使用以下代码(不包括标题):

\pgfplotstableread{FaltaBCplots.dat}{\datosB}
\raggedleft
\begin{tikzpicture}
\begin{groupplot}[group style={group size=1 by 9, vertical sep=0.84cm,  group name=mygroup},/tikz/background rectangle/.style={draw=none},
legend columns=-1,
cycle list name=Renzo04,
height=2.86cm, width=16.0cm,
xlabel={$t~[\si{\ms}]$},
%hide y axis,
legend style={mark options={scale=1.35},/tikz/every even column/.append style={column sep=1.0ex}},
axis y line=middle,
%axis x line=middle,
axis x line=bottom,
%enlarge x limits=0.05,
enlarge x limits={0.01,upper},
%enlarge x limits=0.01,
enlarge y limits={0.1,upper},
xmajorgrids,
%enlargelimits = true,
xmin=0, xmax=200,
every axis x label/.append style={at={(current axis.south  east)},anchor=west,font=\footnotesize},
every axis y label/.append style={at={(current axis.north west)},above=0mm,font=\footnotesize},
every axis title/.append style={at={(0.5,0.975)}},
xticklabel = \relax,
each nth point=1
%samples=200,
%xtick={0,60, 120, 180, 240, 300, 360},
]
\nextgroupplot[title={Tens\~oes no terminal emissor},%ylabel={\small $f_1(t)$},
legend entries={Fase A, Fase B, Fase C},
legend to name=legendas,ylabel={$v~[\si{\kV}]$}]%,ymin=-0.2,ymax=0.2]
\addplot table [x expr=\thisrow{Time}*1000, y={LSva}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LSvb}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LSvc}] {\datosB};
\nextgroupplot[title={Correntes no terminal emissor},ylabel={$i~[\si{\kA}]$}]%
\addplot table [x expr=\thisrow{Time}*1000, y={LSia}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LSib}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LSic}] {\datosB};
\nextgroupplot[title={Tens\~oes no ponto de falta},ylabel={$v~[\si{\kV}]$}]%
\addplot table [x expr=\thisrow{Time}*1000, y={LFva}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LFvb}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LFvc}] {\datosB};
\nextgroupplot[title={Corrente de falta},%ylabel={\small $f_1(t)$},
ylabel={$i~[\si{\kA}]$},legend style={draw=none},
legend entries={$i_{bc}$},legend to name=legendaIF]%,ymin=-0.2,ymax=0.2]
\addplot+[red!50!black] table [x expr=\thisrow{Time}*1000, y={IbcFault}] {\datosB};
\nextgroupplot[title={Tens\~oes no terminal receptor},ylabel={$v~[\si{\kV}]$}]%
\addplot table [x expr=\thisrow{Time}*1000, y={LRva}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LRvb}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LRvc}] {\datosB};
\nextgroupplot[title={Correntes no terminal receptor},ylabel={$i~[\si{\kA}]$},
xticklabel={}]%
\addplot table [x expr=\thisrow{Time}*1000, y={LRia}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LRib}] {\datosB};
\addplot table [x expr=\thisrow{Time}*1000, y={LRic}] {\datosB}; 
\nextgroupplot[title={Componente real das imped\^ancias usadas pelo SF},
cycle list name=C6e,
ylabel={$R~[\si{\ohm}]$},legend entries={$Z^{SF}_a$,$Z^{SF}_b$,$Z^{SF}_c$,$Z^{SF}_{ab}$,$Z^{SF}_{bc}$,$Z^{SF}_{ca}$}, 
legend to name=legendas2,yshift=-1.13cm]%
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZANRE}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZBNRE}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZCNRE}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZABRE}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZBCRE}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZCARE}] {\datosB};
\nextgroupplot[title={Componente imagin\'aria das imped\^ancias usadas pelo SF},
cycle list name=C6e,
ylabel={$X~[\si{\ohm}]$}]%
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZANIM}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZBNIM}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZCNIM}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZABIM}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZBCIM}] {\datosB};
\addplot+[mark repeat=5,mark phase=2,const plot mark right] table [x expr=\thisrow{Time}*1000, y={ZCAIM}] {\datosB};
\nextgroupplot[title={Saída do SF},
xticklabel={},ylabel={SF},ytick=data]%
\addplot+[cdef1,very thick] table [x expr=\thisrow{Time}*1000, y={Tx}] {\datosB};
\end{groupplot}
\node [above] at ($(mygroup c1r1.north) + (0,0.55cm)$) {\ref{legendas}}; 
\node [above] at ($(mygroup c1r7.north) + (0,0.6cm)$) {\ref{legendas2}}; 
\node [above] at ($(mygroup c1r4.north) - (5.7,0.7cm)$) {\ref{legendaIF}}; 
\end{tikzpicture}

接力图

最后一个图是一个布尔变量,我需要绘制一些这样的变量,但我想像第二个图那样做这项工作,其中布尔信号为蓝色,并且有一个表示不同于零的值的条和表示值=0的线。

为了提供帮助,以下仅用于数字输入的完整代码已被包含:

\documentclass[12pt]{article}
\usepackage{pstricks}
\usepackage{tikz,pgfplots}
\usepackage{pst-plot}
\usepackage{psfrag}
\usepackage{siunitx}
\usepackage{steinmetz}
\usepackage{import}
\usepackage{graphicx}
\usepackage{stmaryrd}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{flowchart}
\usepackage{xfrac}
\usepackage{anyfontsize}
\pgfplotsset{compat=newest}
\usetikzlibrary{backgrounds,pgfplots.groupplots}
\usetikzlibrary{arrows}
\usepackage{amsmath}
\usepackage[utf8]{inputenc}
\PassOptionsToPackage{english, brazilian}{babel}
\usepackage{babel}
\PassOptionsToPackage{T1}{fontenc}
\usepackage{fontenc}    

\begin{document}
\pagestyle{empty}
\pgfplotstableread{FaltaBCplots.dat}{\datosB}

\begin{tikzpicture}
\begin{groupplot}[group style={group size=1 by 9, vertical sep=0.84cm,    group name=mygroup},/tikz/background rectangle/.style={draw=none},
height=2.86cm, width=16.0cm,
xlabel={$t~[\si{\ms}]$},
legend style={mark options={scale=1.35},/tikz/every even column/.append   style={column sep=1.0ex}},
axis y line=middle,
axis x line=bottom,
enlarge x limits={0.01,upper},
enlarge y limits={0.1,upper},
xmajorgrids,
xmin=0, xmax=200,
every axis x label/.append style={at={(current axis.south    east)},anchor=west},
every axis y label/.append style={at={(current axis.north west)},above=0mm},
every axis title/.append style={at={(0.5,0.975)}},
xticklabel = \relax,
each nth point=1
]
\nextgroupplot[title={Saída do SF},
xticklabel={},ylabel={SF},ytick=data]%
\addplot[red,very thick] table [x expr=\thisrow{Time}*1000, y={Tx}]   {\datosB};
\end{groupplot}
\end{tikzpicture}
\end{document}

数据文件是数据文件。该文件的数字输入类似于:

  Time     SignalA    SignalB
  0.0          0         0
0.0005         0         0
0.0010         0         0
0.0015         1         6
0.0020         1         6
0.0025         1         6
0.0030         1         6
0.0035         1         0
0.0040         1         0
0.0045         1         0

答案1

使用一些想法这个帖子,我做了一个解决方案。我分别为非零值和零值定义了 rectangle* 和 rectangle2* 标记。我使用了两个 \addplot 来绘制一个信号。

信号 A 和 B 相同,因为数据文件中只有一个数字信号,但该图显示可以在同一个轴上绘制多个数字信号。以下代码:

\documentclass[12pt]{article}
\usepackage{tikz,pgfplots}
\usepackage{pst-plot}
\usepackage{siunitx}
\usepackage{graphicx}
\usepackage{stmaryrd}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{backgrounds,pgfplots.groupplots}
\usetikzlibrary{arrows}
\usepackage{amsmath}
\usepackage[utf8]{inputenc}
\PassOptionsToPackage{english, brazilian}{babel}
\usepackage{babel}
\PassOptionsToPackage{T1}{fontenc}
\usepackage{fontenc}    

\pgfkeysdef{/pgf/mark color}{\def\pgf@mark@color{#1}}
\pgfkeysdef{/pgf/colorA}{\def\colorA{#1}}

\pgfdeclareplotmark{rectangle*}{%
\pgfpathmoveto{\pgfqpoint{\pgfplotmarksize}{3\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpoint{0\pgfplotmarksize}{3\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpoint{0\pgfplotmarksize}{-3\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpoint{\pgfplotmarksize}{-3\pgfplotmarksize}}%
\pgfpathmoveto{\pgfqpoint{\pgfplotmarksize}{3\pgfplotmarksize}}%
\pgfsetfillcolor{\pgf@mark@color}
\pgfusepathqfill
}
\pgfdeclareplotmark{rectangle2*}{%
\pgfpathmoveto{\pgfqpoint{\pgfplotmarksize}{0.25\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpoint{0.0\pgfplotmarksize}{0.25\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpoint{0.0\pgfplotmarksize}{-0.25\pgfplotmarksize}}%
\pgfpathlineto{\pgfqpoint{\pgfplotmarksize}{-0.25\pgfplotmarksize}}%
\pgfpathmoveto{\pgfqpoint{\pgfplotmarksize}{0.25\pgfplotmarksize}}%
\pgfsetfillcolor{\pgf@mark@color}
\pgfusepathqfill
}

\begin{document}
\pagestyle{empty}
\pgfplotstableread{FaltaBCplots.dat}{\datosB}

\begin{tikzpicture}
\begin{groupplot}[group style={group size=1 by 9, vertical sep=0.84cm,     group name=mygroup},/tikz/background rectangle/.style={draw=none},
height=5.0cm, width=12.0cm,
xlabel={$t~[\si{\ms}]$},
axis y line=middle,
axis x line=bottom,
enlarge x limits={0.01,upper},
enlarge y limits={0.1,upper},
xmajorgrids,
ymin=1,
ymax=7,
xmin=0, xmax=200,
every axis x label/.append style={at={(current axis.south    east)},anchor=west},
every axis y label/.append style={at={(current axis.north west)},above=0mm},
every axis title/.append style={at={(0.5,0.975)}},
each nth point=1
]
\nextgroupplot[title={Saída do SF},ylabel={SF},
ytick={2,3,4,5,6,7},yticklabels={\textcolor{blue}{A},\textcolor{red} {B},C,AB,BC,CA}]%
\addplot[only marks,mark=rectangle*,mark size=1,mark color=blue,shift={(axis direction cs:0,1)}] table [x expr=\thisrow{Time}*1000, y expr=\thisrow{Tx}>0] {\datosB};
\addplot[only marks,mark=rectangle2*,mark size=1,mark color=blue,shift={(axis direction cs:0,1)}] table [x expr=\thisrow{Time}*1000, y expr=\thisrow{Tx}<1] {\datosB};
\addplot[only marks,mark=rectangle*,mark size=1,mark color=red,shift={(axis direction cs:0,2)}] table [x expr=\thisrow{Time}*1000, y expr=\thisrow{Tx}>0] {\datosB};
\addplot[only marks,mark=rectangle2*,mark size=1,mark color=red,shift={(axis direction cs:0,2)}] table [x expr=\thisrow{Time}*1000, y expr=\thisrow{Tx}<1] {\datosB};
\end{groupplot}
\end{tikzpicture}
\end{document}

生成:

数字输出

相关内容