# Trouble including a Matlab surf plot (pdf or png) with pgfplots

I have successfully used \addplot graphics before. Now I have a surf plot in Matlab, which I exported as png and pdf and I would like to include the image with \addplot3 graphics. I found some questions on the topic here, here, and here, all of which did not help me. I tried several times to get a correct set of points from pgfplotscsconversion, but either my figure looks something like in the first link or the size is simply srewed up - no matter which format I use. I followed the conversion instructions in the second link, which was supposed to give the correct figure size, but it seems the axis box is simply too small (or the image too large). This is my MWE:

\documentclass[a4paper,10pt]{scrartcl}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[3d box,xmin=0,xmax=1,ymin=0,ymax=1,zmin=1,zmax=3]
graphics [
points={
(0.42545,0.84598,3) => (165.418,269.808)
(0.62718,0.73739,3) => (221.628,273.02)
(0.36528,0.67204,1.8154) => (179.872,164.615)
(0.81564,0.23471,1) => (334.851,96.36)
}]
\end{axis}
\end{tikzpicture}

\end{document}


Without the min/max values, it looks even worse. I fear it is related to the third post, but I'm hoping that there is a solution without fiddling around manually...

Edit: Here is an image:

• Could you provide the image (or one like it, if it contains sensitive information)? – Jake Mar 22 '13 at 16:09
• By trial and error I found that xmin=0.5,xmax=1.7,ymin=0.3,ymax=1.4,zmin=1,zmax=3 makes the whole thing fit. But those are not the correct x and y values! – Anke Mar 22 '13 at 16:29
• My first question is whether you used any 3rd party packages to create it in matlab? The reason for my asking is because otherwise matlab2tikz is your answer. – HCAI Mar 22 '13 at 21:07
• It seems as if the resulting projection is correct, only the limits are sub-optimal, right? Pgfplots takes the bounding box from the points key to determine the limits. Could you add debug=visual right in front of points (perhaps combined with \begin{axis}[clip=false])? This should visualize how the points argument fits to the graphics. With the screenshot above, I could not map it to the image; but perhaps tex.sx has resized the graphic. – Christian Feuersänger Mar 23 '13 at 20:21

How about using matlab2tikz? That's always been my best bet for these things. You create your plot in matlab as normal and then, having downloaded the package matlab2tikz to your current working directory type >>matlab2tikz('filename.tex') and a file is created written in tikz commands.

Example

% This file was created by matlab2tikz v0.3.2.
% Copyright (c) 2008--2013, Nico Schlömer <nico.schloemer@gmail.com>
%
%   http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
% where you can also make suggestions and rate matlab2tikz.
%
%
%
\documentclass{article}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
width=4.52083333333333in,
height=3.378125in,
scale only axis,
xmin=0.5, xmax=2.5,
xtick={\empty},
ymin=-0.872306147353593, ymax=16.8315076281438,
ylabel={Normalised cfu (Y) values}
]
color=black,
dashed,
forget plot
]
table{
1 1.12905875646794
1 2.78718445646597
};
color=black,
dashed,
forget plot
]
table{
2 1.36133715554353
2 3.39777143317797
};
color=black,
dashed,
forget plot
]
table{
1 0
1 0
};
color=black,
dashed,
forget plot
]
table{
2 0
2 0
};
color=black,
solid,
forget plot
]
table{
0.925 2.78718445646597
1.075 2.78718445646597
};
color=black,
solid,
forget plot
]
table{
1.925 3.39777143317797
2.075 3.39777143317797
};
color=black,
solid,
forget plot
]
table{
0.925 0
1.075 0
};
color=black,
solid,
forget plot
]
table{
1.925 0
2.075 0
};
color=blue,
solid,
forget plot
]
table{
0.85 0
0.85 1.12905875646794
1.15 1.12905875646794
1.15 0
0.85 0
};
color=blue,
solid,
forget plot
]
table{
1.85 0
1.85 1.36133715554353
2.15 1.36133715554353
2.15 0
1.85 0
};
color=red,
solid,
forget plot
]
table{
0.85 0
1.15 0
};
color=red,
solid,
forget plot
]
table{
1.85 0
2.15 0
};
color=blue,
only marks,
mark=+,
mark options={solid,draw=red},
forget plot
]
table{
1 2.86116610278157
1 2.95124846970011
1 2.96462408742799
1 2.97672923606257
1 2.97796965154811
1 2.99660242862322
1 3.0037675940822
1 3.01411361097206
1 3.02378221975569
1 3.03465537879915
1 3.05104691875102
1 3.0524887902421
1 3.054698367375
1 3.06946423244171
1 3.123401396294
1 3.18701681450716
1 3.22331347185298
1 3.26431457826656
1 3.26575600527331
1 3.28891193278492
1 3.29126780809468
1 3.29994977231928
1 3.30151454616189
1 3.51369680474291
1 3.55594005913051
1 3.580267849604
1 3.62139284445829
1 3.63331706393713
1 3.69712860434564
1 3.71789360239462
1 3.73137135176868
1 3.73793814785515
1 3.74613321323258
1 3.74713290237321
1 3.75915881837724
1 3.77368294360208
1 3.78414412300685
1 3.84867955908334
1 3.90365624369993
1 3.90753006730174
1 3.91592893219147
1 3.97164083228678
1 3.98212699077419
1 3.99994581987529
1 3.99996287392214
1 4.00373730182012
1 4.02157837509262
1 4.02822619449464
1 4.05222199937739
1 4.18447335976829
1 4.18677023062065
1 4.29946448151493
1 4.31505394260622
1 4.32393762989871
1 4.34436165598829
1 4.35944927796665
1 4.39399815332052
1 4.46042061381864
1 4.46382363320185
1 4.49589099528811
1 4.52114499865807
1 4.52742866833533
1 4.59350844545217
1 4.59496487831338
1 4.61146788071285
1 4.63334374533791
1 4.78194759348968
1 4.83985818575815
1 4.85513436268938
1 4.92123603955527
1 4.93285176189552
1 5.05004769917023
1 5.16969391058426
1 5.22313998874544
1 5.29553454518465
1 5.33766965745357
1 5.36644047293616
1 5.52122597723003
1 5.56570565964821
1 5.60712009690023
1 5.66300423208743
1 5.66514259686913
1 5.67534726927919
1 5.81240592380042
1 5.92522478062696
1 5.93381941413441
1 5.98427109770311
1 5.98565402583205
1 6.05515692299313
1 6.05809430218476
1 6.09131101441445
1 6.11965752158963
1 6.13042419196115
1 6.28887153147068
1 6.32977252857695
1 6.33808014284925
1 6.45676918355347
1 6.48502196897216
1 6.59035226484124
1 6.59884332318236
1 6.60336173876099
1 6.67009552407771
1 6.73110910942222
1 6.75296641706414
1 6.77094593491614
1 6.95828178345678
1 6.99917574058471
1 7.18124875343567
1 7.2160622220336
1 7.25184300053345
1 7.27072859615507
1 7.53978854529375
1 7.62683747307804
1 8.30937998979063
1 8.83438781965087
1 9.22480466629532
1 10.6774780199582
1 11.2782346683741
1 11.3395674314469
1 11.8331754433414
1 13.3372479331164
1 13.3668568887691
1 14.4998735338549
1 15.4828226776902
1 15.6170772950205
1 16.0267888201666
};
color=blue,
only marks,
mark=+,
mark options={solid,draw=red},
forget plot
]
table{
2 3.41321096568804
2 3.42441728383389
2 3.47006454701832
2 3.5083260493326
2 3.50993022181865
2 3.5224280440296
2 3.53097156483447
2 3.53199517316025
2 3.53271118180697
2 3.54044801269578
2 3.57635424914675
2 3.5896987787456
2 3.64453768214226
2 3.65919183094817
2 3.66146964549055
2 3.67456647665076
2 3.7169973585756
2 3.73419637021325
2 3.73904009252507
2 3.77120490977482
2 3.77272560202542
2 3.77497681063978
2 3.79225323954729
2 3.79298102605436
2 3.80760997040253
2 3.81147340948309
2 3.81908053833612
2 3.96875014304215
2 3.99485534417404
2 4.01731506252257
2 4.06765568544008
2 4.0873257520362
2 4.11331236080408
2 4.13325633569113
2 4.26661484034241
2 4.36772600865394
2 4.40303481390946
2 4.40607151364257
2 4.46600165504838
2 4.47111977439536
2 4.49622909097077
2 4.53094088464027
2 4.54231297219618
2 4.57118373068546
2 4.6446735402234
2 4.6647201051236
2 4.71154608850202
2 4.80403096612948
2 4.82138190204484
2 4.96852570578322
2 5.07419215874747
2 5.08033573542001
2 5.10684604665064
2 5.14377910249464
2 5.17463377309759
2 5.17667049399468
2 5.36212407214132
2 5.40448313210525
2 5.41350356223682
2 5.41626341824251
2 5.41817161403
2 5.53622130790774
2 5.61664680822599
2 5.71410241240439
2 5.73419059729061
2 5.76038943550156
2 5.81282928328695
2 5.85167879792264
2 5.88710771337601
2 5.95097380392967
2 5.99247085551671
2 6.01852287751298
2 6.12793948220513
2 6.13691798442411
2 6.1524157717636
2 6.15824150074784
2 6.18114223490642
2 6.21273886630019
2 6.31339722911058
2 6.34381711049789
2 6.52578119369999
2 6.59982567229256
2 6.62614371942598
2 6.89054180919679
2 6.90561121762275
2 6.9983257531296
2 7.00336367711781
2 7.24601684774342
2 7.46677804595626
2 7.474430435742
2 7.5054467862249
2 7.74350903028379
2 7.87327847780469
2 8.01233531618229
2 8.02823255086298
2 8.27567062753628
2 8.57461049625301
2 8.74788905030809
2 8.93719593956683
2 9.30958954675193
2 9.94300146504975
2 9.98413037495241
2 11.0057097811966
2 11.084099718654
2 11.7427852334254
2 12.4827210667149
};
\end{axis}
\end{tikzpicture}%
\end{document}

• Thanks for the suggestion. I do use matlab2tikz - just not for surf plots. I tried and the script is fine, but pdflatex can't handle the files. They are too big :( That's why I switched to pdf exports. – Anke Mar 25 '13 at 12:09
• Ahh I see, that's been problem for me too. And doubly so because matlab does not export the correct size pdf for me (always find part of the plot hanging over the edge of the page). I have to do it by trial and error. Does this happen for you too? – HCAI Mar 25 '13 at 12:16
• Yes, sounds like it. I'm thinking about just leaving the axes completely blank right now. There is a nice scipt `plot2svg´, which I have used before. I used that to export my Matlab figures and then converted them to pdf with inkscape. But for surf plots, it's virtually impossible to fit those into some axis by pgfplots... – Anke Mar 25 '13 at 12:21