在 Tikz 中用文本替换轴标签

在 Tikz 中用文本替换轴标签

我在 Matlab 中的散点图中绘制了 11 个点,并使用字符串替换 x 轴标签 1 - 11:

figure;
grid on; hold on; box on;
scatter(2:10,100*d1(1,:),'rs','filled');
scatter(2:10,100*d1(2,:),'bd','filled');
x1=[1 11];
y1=[0 0];
plot(x1,y1,'-k','LineWidth',1.0)
xlabel('Specimen ID');
ylabel('Percentage Difference');
YTick
legend('(+) Direction','(-) Direction','Location','SouthEast');
ylim([-30 30]);
xlim([1 11]);
set(gca,'XTickLabel',{' ','T0','T1B','T2B','T2A','T1A','T3A','T4A','T3B','T4B',' '});
matlab2tikz('PT_force_pd.tikz','width','\pdfw','height','\pdfh','showInfo',false);

然后我使用 matlab2tikz 将其转换为 tikz 文件,并将其包含在我的 latex 文件中。但是,这些图形现在在 x 轴上有数字 1-11,而不是我将它们更改为的字符串标签。

% This file was created by matlab2tikz v0.2.1.
% Copyright (c) 2008--2012, Nico Schlömer <[email protected]>
% All rights reserved.
% 
% 
% 
\begin{tikzpicture}

\begin{axis}[%
view={0}{90},
width=\pdfw,
height=\pdfh,
scale only axis,
xmin=1, xmax=11,
xlabel={Specimen ID},
xmajorgrids,
ymin=-30, ymax=30,
ylabel={Percentage Difference},
ymajorgrids,
legend style={at={(0.97,0.03)},anchor=south east,nodes=right}]
\addplot[only marks,mark=square*,color=red] plot coordinates{ (2,2.78639981453257) (3,2.31842963017684) (4,1.14947398221063) (5,7.25340115482997) (6,2.98493302062099) (7,3.26884390452601) (8,14.1539557488726) (9,-0.960290460549314) (10,12.0273779126074) };

\addlegendentry{(+) Direction};

\addplot[only marks,mark=diamond*,color=blue] plot coordinates{ (2,1.38095764618068) (3,5.36378212382794) (4,7.89091352246246) (5,-2.71084599057245) (6,-4.93480585024889) (7,-9.94728428423853) (8,17.0552754021373) (9,2.64843195399426) (10,18.7448964435552) };

\addlegendentry{(-) Direction};

\addplot [
color=black,
solid,
line width=1.0pt,
forget plot
]
coordinates{
 (1,0)(11,0) 
};
\end{axis}
\end{tikzpicture}%

有谁知道如何解决这个问题,或者甚至知道如何手动将 tikz 轴标签更改为文本?

答案1

您可以使用

xtick=data,
xticklabels={T0,T1B,T2B,T2A,T1A,T3A,T4A,T3B,T4B},

代码(我注释掉了这些行width=\pdfwheight=\pdfh因为代码中没有提供这些值):

\documentclass{article}
\usepackage{pgfplots}  

\begin{document}
\begin{tikzpicture}

\begin{axis}[%
view={0}{90},
%width=\pdfw,
%height=\pdfh,
scale only axis,
xmin=1, xmax=11,
xlabel={Specimen ID},
xtick=data,
xticklabels={T0,T1B,T2B,T2A,T1A,T3A,T4A,T3B,T4B},
xmajorgrids,
ymin=-30, ymax=30,
ylabel={Percentage Difference},
ymajorgrids,
legend style={at={(0.97,0.03)},anchor=south east,nodes=right}]
\addplot[only marks,mark=square*,color=red] plot coordinates{ (2,2.78639981453257) (3,2.31842963017684) (4,1.14947398221063) (5,7.25340115482997) (6,2.98493302062099) (7,3.26884390452601) (8,14.1539557488726) (9,-0.960290460549314) (10,12.0273779126074) };

\addlegendentry{(+) Direction};

\addplot[only marks,mark=diamond*,color=blue] plot coordinates{ (2,1.38095764618068) (3,5.36378212382794) (4,7.89091352246246) (5,-2.71084599057245) (6,-4.93480585024889) (7,-9.94728428423853) (8,17.0552754021373) (9,2.64843195399426) (10,18.7448964435552) };

\addlegendentry{(-) Direction};

\addplot [
color=black,
solid,
line width=1.0pt,
forget plot
]
coordinates{
 (1,0)(11,0) 
};
\end{axis}
\end{tikzpicture}%
\end{document}

在此处输入图片描述

相关内容