新版本

新版本

这正是我想要的。

Pgfplots?Metapost?Tikz?

由于我现在无法使用其中任何一个,所以我必须选择其中一个进行研究。哪一个?哪个代码最短?哪个图像质量最好?

如果你为我编写代码本身,我不介意。:-)

答案1

解决方案如下pstricks

\documentclass[x11names, border=3pt]{standalone}
\usepackage{pstricks-add}
\usepackage{auto-pst-pdf}

\begin{document}

\begin{pspicture*}(-4,-1.5)(6,3)
\psset{dotsize=5pt, arrowinset=0}
\dotnodes(-1,2.5){A}(1.5,1.5){B}
\pnodes(-1,0){H}(1.5,0){K}
\pnodes{A}(-2.5,2.5)(0.6,2.5)
\pnodes{B}(0.2,0)(0.2,1.5)(2.8,1.5)
\psframe[linewidth=0.3pt, fillstyle=solid, fillcolor=SlateGray4!60](B0)(A1)
\ncline{|-|}{A0}{A1}
\ncline{|-|}{B1}{B2}
{\psset{linewidth=1.8pt}
\ncline{A0}{A1}
\ncline{B1}{B2}
\psaxes[yAxis=false, ticks=none, labels=none]{->}(0,0)(-4,0)(6,0)}
\psset{linestyle=dotted}
\ncline{A}{H}\ncline{B}{K}
\end{pspicture*}

\end{document} 

在此处输入图片描述

答案2

仅需使用 Plain TeX 进行代码高尔夫乐趣\pdfliteral;无需任何软件包。:-)

\pdfliteral{q 0.8g 0.2w 38 0m 38 20l 42 20l 42 0l B
  1w 0g 0 0m 100 0l S 100 0m 95 1.5l 95 -1.5l b* 18 20m 42 20l S 38 10m 62 10l S
  4w 1J 30 20m 30 20l S 50 10m 50 10l S
  0.5w 0J 18 22m 18 18l S 42 22m 42 18l S 38 12m 38 8l S 62 12m 62 8l S
  [0.5 0.5] 0d 30 0m 30 20l S 50 0m 50 10l S Q}\bye

在此处输入图片描述


带注释的精简版本(相同输出):

PDF 规范使用 PostScript 风格的语言来布局 PDF 中的元素。pdfTeX 的\pdfliteral原语可以将其参数直接注入 PDF 文件中,因此我们可以使用它在非常低、简洁的层面上绘制图形。

该语言使用后缀运算符,因此通常运算符位于此分解代码中的行尾,而参数位于其前面。完整的语言规范可从 Adob​​e 免费在线获取:PDF 参考和 Adob​​e 对 PDF 规范的扩展

在此代码中,路径以m运算符开头,包含各种其他路径构建运算符(这里主要l用于行),并以一个或多个路径绘制运算符(这里主要S用于描边路径,但在一些地方,“更花哨”的运算符用于关闭、描边、填充以及它们的组合)。

\pdfliteral{
  q        % save current graphics state (for later restore)
  %% shaded overlapping region
  0.8g     % set fill color to 80% [g]rey
  0.2w     % set stroke [w]idth to 0.2pt
  38 0m    % [m]ove "pen" to coordinate (38,0) 
  38 20l   % [l]ine to (38,20)
  42 20l   % [l]ine to (42,20)
  42 0l    % [l]ine to (42,0)
  B        % fill and stroke the current path
  %% base line
  1w 0g    % set stroke width to 1pt and fill color to black
  0 0m     % move pen to the origin (0,0)
  100 0l   % line to (100,0)
  S        % stroke the current path
  %% arrowhead
  100 0m   % move pen to (100,0)
  95 1.5l  % line to (95,1.5)
  95 -1.5l % line to (95,-1.5)
  b*       % close, fill, and stroke the current path
  %% error bar base lines
  18 20m   % move pen to (18,20)
  42 20l   % line to (42,20)
  S        % stroke the current path
  38 10m   % move pen to (38,10)
  62 10l   % line to (62,10)
  S        % stroke the current path
  %% dots (zero-length lines w/ rounded line cap style)
  4w 1J    % set stroke width to 4pt and line cap style to 1 (rounded cap)
  30 20m   % move pen to (30,20)
  30 20l   % line to (30,20)
  S        % stroke the current path 
  50 10m   % move pen to (50,10)
  50 10l   % line to (50,10)
  S        % stroke the current path
  %% error bar ends
  0.5w 0J  % set stroke width to 0.5pt and line cap style to 0 (butt cap)
  18 22m   % move pen to (18,22)
  18 18l   % line to (18,18)
  S        % stroke the current path
  42 22m   % move pen to (42,22)
  42 18l   % line to (42,18)
  S        % stroke the current path
  38 12m   % move pen to (38,12)
  38 8l    % line to (38,8)
  S        % stroke the current path
  62 12m   % move pen to (62,12)
  62 8l    % line to (62,8)
  S        % stroke the current path
  %% dotted lines from dots to base line
  [0.5 0.5] 0d % set the dash pattern to "0.5pt on, 0.5pt off, phase 0"
  30 0m    % move pen to (30,0)
  30 20l   % line to (30,20) 
  S        % stroke the current path
  50 0m    % move pen to (50,0)
  50 10l   % line to (50,10)
  S        % stroke the current path 
  Q        % restore original graphics state
}\bye

答案3

不确定最短代码,但这里有一个简单的 Metapost 努力,展示了ahangle您可以用来制作误差线的技巧。

在此处输入图片描述

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

path xx, e[], box;

xx = origin -- right scaled 60mm;

e1 = (left--right) scaled 11mm shifted (42mm,7mm);
e2 = (left--right) scaled 10mm shifted (26mm,14mm);

box = unitsquare xscaled abs xpart (point 0 of e1 - point 1 of e2) 
                 yscaled abs ypart point 1 of e2
                 shifted (xpart point 0 of e1,0);

fill box withcolor .8[red,white];
draw box withcolor .4[red,white];

drawarrow xx withpen pencircle scaled 1;

ahangle := 180; % a trick to make error bars using "drawdblarrow"
ahlength := 3;

forsuffixes $=1,2: 
   drawdblarrow e$ withpen pencircle scaled 1; 
   z$ = point 1/2 of e$;
   draw z$ -- (x$,0) dashed withdots scaled 1/3;
   fill fullcircle scaled 6 shifted z$;
endfor

endfig;
end.

答案4

新版本

这使用来pic绘制带有误差线的点。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,backgrounds}
\tikzset{
 errorball/.pic={
   \draw [Bar-Bar,very thick,shorten <=-0.5\pgflinewidth,shorten >=-0.5\pgflinewidth] (-1,0) coordinate (-left) -- (1,0) coordinate (-right);
   \node[minimum size=5pt,inner sep=0pt,fill,circle] (-ball) at (0,0) {};
}}
\begin{document}
\begin{tikzpicture}
\draw [-latex,very thick] (0,0) -- (5,0);
\pic (upper) at (2,2) {errorball};
\pic (lower) at (3.5,1) {errorball};

\foreach \ball in {upper,lower}
  \draw [densely dotted] (\ball-ball) -- (\ball-ball|-0,0);
\begin{scope}[on background layer]
\fill[black!10,draw=black] (upper-right) rectangle (lower-left |- 0,0);
\end{scope}
\end{tikzpicture}
\end{document}

旧答案

关于 TikZ 的一个建议。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,backgrounds}
\begin{document}
\begin{tikzpicture}
\draw [-latex,very thick] (0,0) -- (5,0);
\draw [Bar-Bar,very thick] (1,2) -- node[fill,circle] (a) {} +(2,0);
\draw [Bar-Bar,very thick] (2.5,1) -- node[fill,circle] (b) {} +(2,0);
\draw [densely dotted] (a) -- (a|-0,0)
                       (b) -- (b|-0,0);

\begin{scope}[on background layer]
\fill[gray] (2.5,0) rectangle (3,2);
\end{scope}
\end{tikzpicture}
\end{document}

相关内容