在 pgfplots 中对标签进行分组或查找适用于 Windows 的 cm 字体

在 pgfplots 中对标签进行分组或查找适用于 Windows 的 cm 字体

我想xlabel在分组条形图上添加第二个TiKz添加第二个,但似乎无法解决。所以我找到了这个cm 超级字体包裹:http://ctan.org/pkg/cm-super。或者我可以下载cm fonts直接下载到我的电脑(但它们的格式我无法打开,而且我已经尝试了所有方法)http://sourceforge.net/projects/cm-unicode/

我打算制作我的图表>导出到 Adob​​e Illustrator 或 Inkscape 并在其中写入我的组标签:

% This file was created by matlab2tikz v0.3.2.
% Copyright (c) 2008--2013, Nico Schlömer <[email protected]>
% All rights reserved.
%
% The latest updates can be retrieved from
%   http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
% where you can also make suggestions and rate matlab2tikz.
%
%
%

\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\begin{document}
\definecolor{mycolor1}{rgb}{0.0416666666666667,0,0}

\begin{tikzpicture}

\begin{axis}[%
width=11.0276041666667in,
height=5.87479166666667in,
area legend,
scale only axis,
xmin=0, xmax=25,
xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40},
xticklabels={E,P,H,N,F,,E,P,H,N,F,,E,P,H,N,F,,E,P,H,N,F,},
ymin=0, ymax=2,
ylabel={CFU cm$^2$},
legend style={draw=black,fill=white,legend cell align=left},yticklabel style={/pgf/number format/fixed},
]
\addplot[ybar,bar width=0.10082380952381in,bar shift=-0.063014880952381in,fill=mycolor1,draw=black] plot coordinates{(1,0.04485625)
(2,1.881133333)
(3,1.197503086)
(4,1.842835802)
(5,0.3139072)
(6,0)
(7,0.04485625)
(8,0.002466667)
(9,0.578984568)
(10,0.003676543)
(11,0.3139072)
(12,0)
(13,0.000628571)
(14,0.001266667)
(15,0.576515432)
(16,0.001048457)
(17,0.30285028)
(18,0)
(19,0.04485625)
(20,0.001266667)
(21,0.576762346)
(22,0.002796914)
(23,0.30285028)};

\addlegendentry{4  ac.h$^{-1}$};

\addplot [
color=black,
solid,
forget plot
]
table{
0 0
25 0
};
\addplot[ybar,bar width=0.10082380952381in,bar shift=0.063014880952381in,fill=white,draw=black] plot coordinates{(1,0.04265625)
(2,0.595466667)
(3,0.499092593)
(4,0.401030556)
(5,0.170007298)
(6,0)
(7,0.04265625)
(8,0.000133333)
(9,0.306561728)
(10,0.000462346)
(11,0.170007298)
(12,0)
(13,5e-05)
(14,0.0002)
(15,0.306561728)
(16,0.000462346)
(17,0.159355735)
(18,0)
(19,0.04265625)
(20,0.0002)
(21,0.306592593)
(22,0.000777778)
(23,0.159355735)};

\addlegendentry{6 ac.h$^{-1}$};

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

像这样: 在此处输入图片描述

答案1

嗯,迟做总比不做好!:-)

该解决方案依赖于修改生成的代码matlab2tikz,因此它可能对您来说是或可能不可接受。

我将首先展示整个代码和输出,然后解释我的更改。

\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\begin{document}
\definecolor{mycolor1}{rgb}{0.0416666666666667,0,0}

\begin{tikzpicture}

\begin{axis}[%
width=11.0276041666667in,
height=5.87479166666667in,
area legend,
scale only axis,
clip=false, % do not clip items drawn outside the axis (Patient n labels)
xmin=0, xmax=25,
xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40},
xticklabels={E,P,H,N,F,,E,P,H,N,F,,E,P,H,N,F,,E,P,H,N,F,},
ymin=0, ymax=2,
ylabel={CFU cm$^2$},
legend style={draw=black,fill=white,legend cell align=left},yticklabel style={/pgf/number format/fixed},
extra x ticks={6,12,...,24}, % locations of the red lines
extra x tick style={ % style for the extra red lines
  tickwidth=6ex, % length of the extra red lines from top to bottom
  tick align=center, % draw extra red lines centered about the axis grid
  tick style={ultra thick,red}, % set line thickness and color
  xticklabel={}, % do not label the extra ticks
  xtick pos=left, % draw extra ticks only on the "left" (bottom), not the top
},
]
\addplot[ybar,bar width=0.10082380952381in,bar shift=-0.063014880952381in,fill=mycolor1,draw=black] plot coordinates{(1,0.04485625)
(2,1.881133333)
(3,1.197503086)
(4,1.842835802)
(5,0.3139072)
(6,0)
(7,0.04485625)
(8,0.002466667)
(9,0.578984568)
(10,0.003676543)
(11,0.3139072)
(12,0)
(13,0.000628571)
(14,0.001266667)
(15,0.576515432)
(16,0.001048457)
(17,0.30285028)
(18,0)
(19,0.04485625)
(20,0.001266667)
(21,0.576762346)
(22,0.002796914)
(23,0.30285028)};

\addlegendentry{4  ac.h$^{-1}$};

\addplot [
color=black,
solid,
forget plot
]
table{
0 0
25 0
};
\addplot[ybar,bar width=0.10082380952381in,bar shift=0.063014880952381in,fill=white,draw=black] plot coordinates{(1,0.04265625)
(2,0.595466667)
(3,0.499092593)
(4,0.401030556)
(5,0.170007298)
(6,0)
(7,0.04265625)
(8,0.000133333)
(9,0.306561728)
(10,0.000462346)
(11,0.170007298)
(12,0)
(13,5e-05)
(14,0.0002)
(15,0.306561728)
(16,0.000462346)
(17,0.159355735)
(18,0)
(19,0.04265625)
(20,0.0002)
(21,0.306592593)
(22,0.000777778)
(23,0.159355735)};

\addlegendentry{6 ac.h$^{-1}$};
\foreach \locn/\num in {3/1,9/2,15/3,21/4} {
  \edef\temp{\noexpand\path (xticklabel cs:0) -| (\locn,0) node[pos=0.5] {Patient \num};}
  \temp % solution from https://tex.stackexchange.com/questions/24539/
}
\end{axis}
\end{tikzpicture}%
\end{document}

在此处输入图片描述

垂直红线

我决定通过创建红线并应用适当的样式来绘制红线extra x ticks。相关代码位于环境选项列表的末尾axis,在此处重现:

extra x ticks={6,12,...,24}, % locations of the red lines
extra x tick style={ % style for the extra red lines
  tickwidth=6ex, % length of the extra red lines from top to bottom
  tick align=center, % draw extra red lines centered about the axis grid
  tick style={ultra thick,red}, % set line thickness and color
  xticklabel={}, % do not label the extra ticks
  xtick pos=left, % draw extra ticks only on the "left" (bottom), not the top
},

注释描述了每个设置的功能。当然,您可以根据自己的喜好自定义这些设置。

下方有患者标签

对于组标签(患者 1、2 等),我将其添加clip=false到选项列表中axis,以防止这些多余的标签被剪掉。

放置这些标签所需的其余代码以\foreach循环的形式位于环境的最末端axis

\foreach \locn/\num in {3/1,9/2,15/3,21/4} {
  \edef\temp{\noexpand\path (xticklabel cs:0) -| (\locn,0) node[pos=0.5] {Patient \num};}
  \temp % solution from https://tex.stackexchange.com/questions/24539/
}

我使用了以下解决方案如何\foreachaxis环境中使用pgfplots中使用?解决\foreach内部axis环境中的一些扩展困难。的扩展将从刻度标签的底部标记到\temp“患者 n”标签的所需坐标。然后,将节点放置在此路径的中点(转折点)下方。-| \pathx

相关内容