4

I am plotting 11 points in a scatter plot in Matlab and replacing the x-axis labels 1 - 11 with strings using:

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);

I then convert this to a tikz file using matlab2tikz and include it in my latex file. However, the figures now have the numbers 1- 11 on the x-axis as opposed to the string labels I changed them to.

% This file was created by matlab2tikz v0.2.1.
% Copyright (c) 2008--2012, Nico Schlömer <nico.schloemer@gmail.com>
% 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}%

Does anyone know how to solve this, or even a way to manually change the tikz axis labels to text?

  • Could you edit your question to include the TikZ code produced by matlab2tikz? – Jake Jun 19 '13 at 17:38
6

You can use

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

The code (I commented out the lines width=\pdfw and height=\pdfh since the values were not provided in the code):

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.