Message Boards Message Boards

[?] Add density over scatter and residual plots?

Hi,

Is it possible to draw figures like below plots in the Mathematica? enter image description here

data1 = {9.186654398965594`, 11.32413825912701`, 12.999492089337714`, 
   10.948577055056163`, 10.466966636206493`, 10.032664245063756`, 
   11.100934973327298`, 10.296924435151816`, 9.758180435696307`, 
   11.100934973327298`, 11.39756761599669`, 16.511953514399373`, 
   12.54725830501969`, 13.611075936967767`, 12.812451028327741`, 
   12.140369723021866`, 11.39756761599669`, 11.763357200363862`, 
   11.470720890999317`, 12.722065784278762`, 8.895604530001734`, 
   11.250303448593353`, 12.063157696444643`, 10.632084236557162`, 
   12.999492089337714`, 11.987002395607853`, 12.140369723021866`, 
   11.32413825912701`, 10.21004588638901`, 13.19507447508718`, 
   11.470720890999317`, 11.39756761599669`, 12.298550801936036`, 
   11.911752675202644`, 11.689904661828214`, 13.096212914071664`, 
   14.745682792162185`, 12.21878636680408`, 13.296052934922562`, 
   13.19507447508718`, 11.911752675202644`, 11.32413825912701`, 
   11.175940667366268`, 12.298550801936036`, 13.096212914071664`, 
   11.470720890999317`, 15.82627035512297`, 13.19507447508718`, 
   14.514709617293894`, 13.296052934922562`, 12.379799920436515`, 
   12.54725830501969`, 11.175940667366268`, 12.140369723021866`, 
   13.719779654570376`, 12.722065784278762`, 10.871038865783685`, 
   12.462662670206388`, 12.999492089337714`, 13.941864030648507`, 
   11.39756761599669`, 12.462662670206388`, 12.298550801936036`, 
   12.462662670206388`, 11.100934973327298`, 14.283724748680845`, 
   15.727576276756924`, 14.860470689313262`, 13.50414069499622`, 
   13.296052934922562`, 14.974423226880063`, 13.096212914071664`, 
   13.399099705669839`, 12.904912172736637`, 13.941864030648507`, 
   13.830100790625906`, 8.51019034625159`, 9.66470098963815`, 
   8.895604530001734`, 10.382566438719257`, 11.175940667366268`, 
   12.812451028327741`, 15.416637783875956`, 14.974423226880063`, 
   16.719661987369243`, 16.277279769165393`, 16.653361224298923`, 
   17.19665956317432`, 17.15331412466529`, 14.399081483548601`, 
   17.1077592842704`, 17.00966396624458`, 16.584141183722732`, 
   15.82627035512297`, 15.727576276756924`, 14.514709617293894`, 
   16.105687053352973`, 15.82627035512297`, 16.01542152908552`, 
   17.1077592842704`, 17.831100564198714`, 15.727576276756924`, 
   16.192987509384956`, 15.198750822558287`, 17.909700347848506`, 
   14.283724748680845`, 12.904912172736637`, 16.105687053352973`, 
   16.956946738384133`, 17.27707869096585`, 17.738468329423736`, 
   17.72392718647565`, 17.34971939084806`, 17.474341006456047`, 
   17.708602894742256`, 17.789588874011933`, 17.19665956317432`, 
   17.88603559504305`, 14.168903904223178`, 14.283724748680845`, 
   16.584141183722732`, 15.087270137561696`, 16.653361224298923`, 
   16.01542152908552`, 17.237884899418653`, 16.358539773955847`, 
   17.34971939084806`, 11.987002395607853`, 14.974423226880063`, 
   17.27707869096585`, 17.777791565244158`, 17.76536267120605`, 
   17.892405107913657`, 16.843745941491832`, 17.501650170947094`, 
   16.584141183722732`, 17.904221533665098`, 17.738468329423736`, 
   17.15331412466529`, 17.638579565003546`, 17.552161527420328`, 
   17.657489538529497`, 16.843745941491832`, 14.283724748680845`, 
   16.653361224298923`, 13.296052934922562`, 16.956946738384133`, 
   12.063157696444643`, 15.308617466951913`, 16.277279769165393`, 
   11.470720890999317`, 14.514709617293894`, 13.941864030648507`, 
   12.722065784278762`, 14.860470689313262`, 16.719661987369243`, 
   16.511953514399373`, 17.383335824377728`, 17.777791565244158`, 
   15.198750822558287`, 17.237884899418653`, 17.857066301635133`, 
   17.789588874011933`, 17.800787776462574`, 17.831100564198714`, 
   17.89846162439554`, 17.27707869096585`, 15.198750822558287`, 
   14.860470689313262`, 16.653361224298923`, 15.308617466951913`, 
   14.168903904223178`, 15.922254375716564`, 12.462662670206388`, 
   9.66470098963815`, 12.462662670206388`, 10.032664245063756`, 
   10.712854234546828`, 12.063157696444643`, 13.719779654570376`, 
   11.02517968807868`, 14.860470689313262`, 15.308617466951913`, 
   14.745682792162185`, 14.974423226880063`, 17.34971939084806`, 
   16.719661987369243`, 16.436761118424144`, 17.1077592842704`, 
   17.383335824377728`, 15.922254375716564`, 17.05990556381551`, 
   17.929086730908303`, 17.638579565003546`, 16.783101497634195`, 
   14.283724748680845`, 15.308617466951913`, 13.399099705669839`, 
   10.948577055056163`, 14.168903904223178`, 11.100934973327298`, 
   13.611075936967767`, 12.999492089337714`, 13.296052934922562`, 
   12.54725830501969`, 10.948577055056163`, 10.550132530902804`, 
   12.140369723021866`, 11.470720890999317`, 11.987002395607853`, 
   11.763357200363862`, 12.999492089337714`, 16.192987509384956`, 
   12.904912172736637`, 12.812451028327741`, 15.416637783875956`, 
   14.054871325507664`, 12.462662670206388`, 13.50414069499622`, 
   11.911752675202644`, 11.837255498255267`, 13.296052934922562`, 
   12.812451028327741`, 11.32413825912701`, 12.999492089337714`, 
   11.02517968807868`, 10.121948873095114`, 10.550132530902804`, 
   9.186654398965594`, 9.570369786078889`, 9.850718288439335`, 
   10.032664245063756`, 10.948577055056163`, 10.21004588638901`, 
   10.466966636206493`, 11.250303448593353`, 10.871038865783685`, 
   12.904912172736637`, 11.616746395205226`, 9.186654398965594`, 
   7.451007394398807`, 8.045075512023349`, 9.475286641859373`, 
   11.02517968807868`, 10.871038865783685`, 11.616746395205226`, 
   11.250303448593353`, 11.837255498255267`, 11.543733556805783`, 
   11.763357200363862`, 8.60583733659225`, 7.780794138637022`, 
   8.135930210314118`, 9.850718288439335`, 7.530817094579609`, 
   7.373046795680546`, 9.850718288439335`, 9.570369786078889`, 
   10.466966636206493`, 10.382566438719257`, 10.382566438719257`, 
   10.871038865783685`, 9.570369786078889`, 10.466966636206493`, 
   9.850718288439335`, 10.871038865783685`, 11.689904661828214`, 
   10.79248705135189`, 11.100934973327298`, 11.250303448593353`, 
   10.712854234546828`, 10.032664245063756`, 11.689904661828214`, 
   10.632084236557162`, 11.911752675202644`, 11.837255498255267`, 
   11.911752675202644`, 11.39756761599669`, 10.032664245063756`, 
   10.382566438719257`, 7.222831140874281`, 7.612425624735828`, 
   6.758878866080269`, 8.045075512023349`, 8.415245061795318`, 
   9.186654398965594`, 7.780794138637022`, 9.283308357965344`, 
   9.758180435696307`, 10.121948873095114`, 11.470720890999317`, 
   12.722065784278762`, 12.633694561467989`, 13.19507447508718`, 
   12.633694561467989`, 13.719779654570376`, 14.399081483548601`, 
   12.812451028327741`, 12.633694561467989`, 11.987002395607853`};

data2 = {11.`, 13.1`, 10.5`, 9.9`, 9.4`, 10.7`, 9.7`, 9.1`, 10.7`, 11.1`, 
   16.5`, 12.6`, 13.7`, 12.9`, 12.1`, 11.1`, 11.6`, 11.2`, 12.8`, 8.2`, 10.9`,
    12.`, 10.1`, 13.1`, 11.9`, 12.1`, 11.`, 9.6`, 13.3`, 11.2`, 11.1`, 12.3`, 
   11.8`, 11.5`, 13.2`, 14.7`, 12.2`, 13.4`, 13.3`, 11.8`, 11.`, 10.8`, 12.3`,
    13.2`, 11.2`, 15.7`, 13.3`, 14.5`, 13.4`, 12.4`, 12.6`, 10.8`, 12.1`, 
   13.8`, 12.8`, 10.4`, 12.5`, 13.1`, 14.`, 11.1`, 12.5`, 12.3`, 12.5`, 10.7`,
    14.3`, 15.6`, 14.8`, 13.6`, 13.4`, 14.9`, 13.2`, 13.5`, 13.`, 14.`, 13.9`,
    7.8`, 9.`, 8.2`, 9.8`, 10.8`, 12.9`, 15.3`, 14.9`, 16.8`, 16.2`, 16.7`, 
   17.7`, 17.6`, 14.4`, 17.5`, 17.3`, 16.6`, 15.7`, 15.6`, 14.5`, 16.`, 15.7`,
    15.9`, 17.5`, 20.6`, 15.6`, 16.1`, 15.1`, 21.7`, 14.3`, 13.`, 16.`, 17.2`,
    17.9`, 19.8`, 19.7`, 18.1`, 18.5`, 19.6`, 20.2`, 17.7`, 21.3`, 14.2`, 
   14.3`, 16.6`, 15.`, 16.7`, 15.9`, 17.8`, 16.3`, 18.1`, 11.9`, 14.9`, 17.9`,
    20.1`, 20.`, 21.4`, 17.`, 18.6`, 16.6`, 21.6`, 19.8`, 17.6`, 19.2`, 18.8`,
    19.3`, 17.`, 14.3`, 16.7`, 13.4`, 17.2`, 12.`, 15.2`, 16.2`, 11.2`, 14.5`,
    14.`, 12.8`, 14.8`, 16.8`, 16.5`, 18.2`, 20.1`, 15.1`, 17.8`, 20.9`, 
   20.2`, 20.3`, 20.6`, 21.5`, 17.9`, 15.1`, 14.8`, 16.7`, 15.2`, 14.2`, 
   15.8`, 12.5`, 9.`, 12.5`, 9.4`, 10.2`, 12.`, 13.8`, 10.6`, 14.8`, 15.2`, 
   14.7`, 14.9`, 18.1`, 16.8`, 16.4`, 17.5`, 18.2`, 15.8`, 17.4`, 22.1`, 
   19.2`, 16.9`, 14.3`, 15.2`, 13.5`, 10.5`, 14.2`, 10.7`, 13.7`, 13.1`, 
   13.4`, 12.6`, 10.5`, 10.`, 12.1`, 11.2`, 11.9`, 11.6`, 13.1`, 16.1`, 13.`, 
   12.9`, 15.3`, 14.1`, 12.5`, 13.6`, 11.8`, 11.7`, 13.4`, 12.9`, 11.`, 13.1`,
    10.6`, 9.5`, 10.`, 8.5`, 8.9`, 9.2`, 9.4`, 10.5`, 9.6`, 9.9`, 10.9`, 
   10.4`, 13.`, 11.4`, 8.5`, 6.6`, 7.3`, 8.8`, 10.6`, 10.4`, 11.4`, 10.9`, 
   11.7`, 11.3`, 11.6`, 7.9`, 7.`, 7.4`, 9.2`, 6.7`, 6.5`, 9.2`, 8.9`, 9.9`, 
   9.8`, 9.8`, 10.4`, 8.9`, 9.9`, 9.2`, 10.4`, 11.5`, 10.3`, 10.7`, 10.9`, 
   10.2`, 9.4`, 11.5`, 10.1`, 11.8`, 11.7`, 11.8`, 11.1`, 9.4`, 9.8`, 6.3`, 
   6.8`, 5.6`, 7.3`, 7.7`, 8.5`, 7.`, 8.6`, 9.1`, 9.5`, 11.2`, 12.8`, 12.7`, 
   13.3`, 12.7`, 13.8`, 14.4`, 12.9`, 12.7`, 11.9`, 10.8`};

ListPlot[(data1 - data2), LabelStyle -> {14, GrayLevel[0], Bold}, 
 PlotStyle -> Blue, ImageSize -> 700, PlotRange -> All]

pairs = Transpose[{data1, data2}];

ListPlot[pairs, Mesh -> All, ImageSize -> 500, AspectRatio -> Automatic, 
 PlotRange -> {{0, 25}, {0, 25}}, TicksStyle -> Directive[Black, 10], 
 AxesStyle -> Directive[Black, 12], Ticks -> Automatic, 
 GridLines -> Automatic, Axes -> True, PlotStyle -> {PointSize[.01], Purple}, 
 Epilog -> Line[{{0, 0}, {25, 25}}]]

enter image description here

Attachments:
POSTED BY: M.A. Ghorbani
7 Replies

I could add a plot legend :

diff1 = Transpose[{Observed, Model - Observed}];
residual = 
 SmoothDensityHistogram[diff1, 
  FrameTicksStyle -> Directive[Black, 18], 
  ColorFunction -> colorFunction, PlotRange -> {{0, 110}, {-60, 40}}, 
  Epilog -> Point[diff1], ImageSize -> 400, 
  PlotLegends -> 
   BarLegend[Automatic, LegendMarkerSize -> 350, 
    LabelStyle -> {FontSize -> 14}], 
  FrameLabel -> {None, Style["Residuals", 18, Black]}]

enter image description here

POSTED BY: M.A. Ghorbani

Hi Rohit,

How do I add a legend to the below plot? What do the numbers indicate in the legend? Error ? Density? If the answer is density, so what is a simple way to explain these values?

Observed = {8.1762251688`, 10.680774544799998`, 2.101135104`, 0.`, 
   0.`, 0.0995636016`, 27.0830207376`, 15.0667214544`, 46.0322927352`,
    33.4499352768`, 89.74453716000001`, 71.15363771039999`, 
   85.40835771599998`, 7.9187401368`, 0.`, 0.`, 0.`, 0.`, 
   0.017166132`, 17.647645716000003`, 50.603203562400005`, 
   74.5370912664`, 13.93890534`, 62.68730042879999`, 40.1627508624`, 
   2.0238882696`, 0.26435885039999996`, 0.0514984248`, 0.`, 0.`};

Model = {20.127677132825696`, 24.286484941992796`, 
   1.5303852278420926`, -0.4108672387040535`, 0.8343341776470545`, 
   0.38546229592691716`, 25.319131858851726`, 31.75895264009703`, 
   45.07662634888372`, 37.61173733579122`, 44.653709637819745`, 
   62.33181685334203`, 78.59638454711525`, 
   19.058290707396193`, -1.5222139468978249`, 2.563265647989068`, 
   0.8428081551606574`, -0.044806561142099355`, 3.5370303324946235`, 
   24.88438584383924`, 37.56475941870512`, 50.60039533255333`, 
   37.14394282266049`, 62.936192425777136`, 49.67731415361163`, 
   9.169271877259636`, 1.9442197011006144`, 1.7797336918746005`, 
   0.6865736189010789`, 0.14600709152442448`};

colorFunction = 
  Blend[{{0.0491, 
      Directive[RGBColor[1., 0.9999694819562066, 0.9999847409781033], 
       Opacity[1]]}, {0.1298, 
      Directive[
       RGBColor[0.5867093919279774, 0.47226672770275424`, 
        0.7917906462195773], Opacity[1]]}, {0.1767, 
      Directive[RGBColor[0.250728, 0.225386, 0.769152], 
       Opacity[1]]}, {0.2043, 
      Directive[RGBColor[0.24408, 0.361242, 0.816084], 
       Opacity[1]]}, {0.25, 
      Directive[RGBColor[0.266122, 0.486664, 0.802529], 
       Opacity[1]]}, {0.3125, 
      Directive[RGBColor[0.305919, 0.585575, 0.739666], 
       Opacity[1]]}, {0.375, 
      Directive[RGBColor[0.36048, 0.655759, 0.645692], 
       Opacity[1]]}, {0.4375, 
      Directive[RGBColor[0.429842, 0.701849, 0.540321], 
       Opacity[1]]}, {0.5, 
      Directive[RGBColor[0.513417, 0.72992, 0.440682], 
       Opacity[1]]}, {0.5625, 
      Directive[RGBColor[0.607651, 0.743718, 0.358588], 
       Opacity[1]]}, {0.625, 
      Directive[RGBColor[0.705038, 0.742591, 0.299167], 
       Opacity[1]]}, {0.6875, 
      Directive[RGBColor[0.794549, 0.721158, 0.260829], 
       Opacity[1]]}, {0.75, 
      Directive[RGBColor[0.863512, 0.670771, 0.236564], 
       Opacity[1]]}, {0.8125, 
      Directive[RGBColor[0.901014, 0.582826, 0.216542], 
       Opacity[1]]}, {0.875, 
      Directive[RGBColor[0.902853, 0.453964, 0.192014], 
       Opacity[1]]}, {0.9375, 
      Directive[RGBColor[0.878107, 0.293208, 0.160481], 
       Opacity[1]]}, {1., 
      Directive[RGBColor[0.857359, 0.131106, 0.132128], 
       Opacity[1]]}}, #] &;

diff = Transpose[{Observed, Model - Observed}];
residual = 
 SmoothDensityHistogram[diff, FrameTicksStyle -> Directive[Black, 18],
   ColorFunction -> colorFunction, PlotRange -> {{0, 110}, {-60, 40}},
   Epilog -> Point[diff1], ImageSize -> 400, 
  FrameLabel -> {None, Style["Residuals", 18, Black]}]
POSTED BY: M.A. Ghorbani

Thank you very much, Rohit.

POSTED BY: M.A. Ghorbani
Posted 5 years ago
 residual = SmoothDensityHistogram[diff, ColorFunction -> "Rainbow", 
  PlotRange -> All, Epilog -> Point[diff], ImageSize -> 400, 
  PlotLegends -> Placed[Automatic, Below], 
  FrameLabel -> {None, Style["Residuals", 14, Bold, Black]}]

scatter = SmoothDensityHistogram[pairs, ColorFunction -> "Rainbow", 
  PlotRange -> {{0, 25}, {0, 25}}, 
  Epilog -> {Point[pairs], Line[{{0, 0}, {25, 25}}]}, 
  ImageSize -> 400];

Row[{residual, scatter}]

enter image description here

POSTED BY: Rohit Namjoshi

I forgot to say please add "Density Legend".

I appreciate your help.

POSTED BY: M.A. Ghorbani

Henrik,

Good Job. Thank you very much.

Is it possible to add Y-axis on Residual Plot and also Y=X line on scatter plot?

POSTED BY: M.A. Ghorbani

You probably want SmoothDensityHistogram. Here comes a first guess:

diff = MapIndexed[{First[#2], #1} &, data1 - data2];
pairs = Transpose[{data1, data2}];
SmoothDensityHistogram[#, ColorFunction -> "Rainbow", 
   Epilog -> Point[#], ImageSize -> 400] & /@ {diff, pairs}

enter image description here

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract