# Scatterplot with marginal histograms using pgfplots

I've tried to use pgfplots to create a scatterplot with marginal histograms like it can be seen here:

http://proteome-software.wikispaces.com/file/view/Scatter_histograms.png/30123503/Scatter_histograms.png

Creating the histograms and the scatterplot alone works, but putting it together results in a desaster. Does pgfplots supports this kind of graph?

• Welcome to TeX.SX! You don't have to sign with your name since it automatically appears in the lower right corner of your post. – Jesse Jul 24 '14 at 14:56
• @ChristianHupfer, are you sure the link you pasted is related to this thread? The author is asking for scatter and histogram together. – juliohm Jul 24 '14 at 17:58
• @juliohm: On second glance you are right, I have removed the comment – user31729 Jul 24 '14 at 19:02

PGFPlots can calculate histograms. By using three axis environments positioned relative to each other, you can get the desired output:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[
/pgfplots/scale only axis,
/pgfplots/width=6cm,
/pgfplots/height=6cm
]

% The scatterplot
\begin{axis}[
name=main axis % Name the axis, so we can position the histograms relative to this axis
]
\addplot [only marks, mark size=1.5] table {random.dat};
\end{axis}

% The histogram for the x axis
\begin{axis}[
anchor=south west,
at=(main axis.north west),
height=2cm,
xtick=\empty
]
hist={data=x}, % By default, the y values would be used for calculating the histogram
fill=gray!50
] table {random.dat};
\end{axis}

% The histogram for the y axis
\begin{axis}[
anchor=north west,
at=(main axis.north east),
width=2cm,
ytick=\empty
]
% For swapping the x and y axis, we have to change a couple of options...
hist={handler/.style={xbar interval}}, % ... use bars instead of columns ...
x filter/.code=\pgfmathparse{rawy}, % ... interpret the x values of the histogram as y values
y filter/.code=\pgfmathparse{rawx}, % ... and vice versa.
fill=gray!50,
] table {random.dat};
\end{axis}
\end{tikzpicture}

\end{document}


random.dat:

-1.034556182675299  -1.088642639137081
-1.062642392542183  -1.730800229782827
2.112061236824446   0.4213052508263926
0.2081414092873681  0.1178499381944298
0.9996769569924666  2.315473894804006
-1.860016360692352  -1.80583336044917
1.73369575407621    1.368173161256781
-0.7429308663836777 -0.320890575343841
-0.1979146573912954 -0.4365080476459813
0.4716501224453675  0.7643018830581477
1.174380766341545   2.934973750859623
0.6901490963059546  0.5055566964399909
-0.5225274899322595 -2.336402501215872
-0.04752524566243782    0.9918382785064426
-0.02545715784606146    0.6179587619354383
-1.692445541259954  0.3233798554565113
-0.230370492420001  -0.5392176955328299
0.6038059014476774  0.6821182440059813
-1.076804609398595  -0.9837056700351312
-0.1857789391152365 -1.136939273688752
-0.258208790502279  -0.1805152973444488
-0.7195599671240704 -0.8479059081735516
1.092570499347843   2.00377759530567
-0.2696062294740929 -1.132458520464889
-0.8269924788320669 -1.927117798964017
-0.1515366074290889 0.0230071339470114
-0.7103213503616422 -1.403963958892485
0.8829530102950023  1.857490683255044
1.05866349916398    0.7198501681466614
1.096466379804495   1.086525729480579
-1.32303169638362   -0.1533202087887688
0.3739354686781606  -0.03156926687636152
1.489342072707867   2.1872117646764
-0.224900534871041  0.8163766108669269
-1.587347359907595  -2.086313437701529
1.464685243093322   1.374198533145222
-1.267338251647083  -1.195439494319655
0.1251550598405904  -0.7780312340895389
0.8180551984329997  0.3179571635755173
-0.7259672309991982 1.024286153382652
-0.6347060382424617 -1.995450039583252
-0.1275879531600163 -0.9701248657421334
-1.035154214040656  -0.4345218059923297
1.112830442579158   0.9110796108289648
0.1403940614228557  0.3930966756241305
-0.5990597080153418 -2.147831326516866
-0.7035026366060558 0.8009160824851748
1.090688799524646   0.9976237777767307
0.4195835157027016  1.612462920311422
-0.2394595550413869 0.1403177798195741
1.444637305245141   1.793725253759151
0.2510826487708296  -0.9172402778780173
1.050970237168825   0.3429458548719517
-0.1547669371612623 -0.2953877678347505
-0.7077964330012175 -1.577410023672865
-1.281557650915341  -0.7019585910796728
-0.6925092860234755 0.1296573951006644
0.8917492915726694  0.8043114131875437
1.376261454402816   0.2900129349329763
0.6801069893656928  1.430824000405047
-0.2484495635256442 -0.8330824219547133
-0.4795979211826638 -2.492087400741266
1.514924124643029   2.54293071805945
0.622154322642365   0.6249697071569896
0.6434471902987018  0.6624842288917316
2.009568724897284   1.295013757951034
-0.1611358858000567 0.5972825224341278
-0.4958826045353003 0.6552123781234539
-1.546608039056112  -3.092251590256194
0.01963903742918456 -0.6358681023557348
-1.217115357590687  -1.427228304202919
-0.03318482365966646    1.244676317998937
0.1441806659737843  -0.3917848725994295
0.7327139102596605  0.179905661977997
-0.2221623219999036 2.013269450880812
-1.766951404705671  -2.055174273761125
0.1820313811963468  0.5457314419902735
1.114346122011083   1.207326114691013
-1.673897686055655  0.04471554925774202
-1.88098566831986   -0.342043266131189
0.6156730283674012  0.5612745693047205
-0.4702721814316267 -0.04543544172550568
-1.732071951319274  -3.252174730928117
0.9137493443444534  1.152947925633689
0.6178392723810281  0.4132280927224983
0.2967867922623997  -0.07841919518125928
1.396752176208157   1.878892768919695
0.6920887677132427  -0.6696496175333456
-0.2888105031888556 0.05544842238228853
0.8817399280652953  -0.5140856584657298
0.8830220191165575  0.6892944908867348
0.1547642100344178  -0.1064069449067648
0.1267761409114066  0.2672715326585765
-0.1738327099717071 -0.3007997877815971
-0.2222141791185833 -0.3913443288370902
0.3135248241729749  -1.076410808299793
-1.042708487268197  1.133781748500934
0.172987850082249   1.793537418388745
-1.919319965387205  -0.9735790026351395
-1.837375345395248  -0.6512990567395325