7
$\begingroup$

I have 2 data sets, data1 and data2 with large (in the thousands) amounts of data and I want to compare them in a plane. They aren't modeled by the normal distribution, but I'm using that here for simplicity.

data1 = RandomReal[NormalDistribution[], {10000, 2}];
data2 = data1+1;

I can put these on a list plot and plot them. However, this is visually unpleasing and even hides some of the data.

combPlot = ListPlot[{data1, data2},
    PlotRange->All,
    PlotStyle->PointSize[0.01],
    PlotStyle->Directive[Opacity[0.5]]
];
Export[Directory[] <> "/figures/test-comb.pdf", combPlot];

The SmoothDensityHistogram command with the following color function shows the data exactly how I want it for each data set.

histOpts = {ColorFunction -> Function[c, GrayLevel[1 - c]], 
   PlotRange -> {{-4, 4}, {-4, 4}}, ImageSize -> Medium};
hist1 = SmoothDensityHistogram[data1, histOpts];
Export[Directory[] <> "/figures/test-hist1.pdf", hist1];
hist2 = SmoothDensityHistogram[data2, histOpts];
Export[Directory[] <> "/figures/test-hist2.pdf", hist2];

How can I combine these into a single plot, preferably with different colors? I had no success using the Show command. Again, the idea is to visually compare the 2 data sets. If there are any other alternatives, I'd like to see them too.


Update: After seeing Diego Zviovich's post, I was able to get it working for my example. However, my actual data sets may significantly differ so the bounds aren't the same:

data1 = RandomReal[NormalDistribution[], {10000, 2}];
data2 = data1+10;

hist1 = SmoothDensityHistogram[data1,
    ColorFunction->Function[c, Hue[215/360, .973, 1, c]], 
    PlotRange->All, ImageSize -> Medium];
hist2 = SmoothDensityHistogram[data2, 
    ColorFunction->Function[c, Hue[311/360, .973, 1, c]], 
    PlotRange->All, ImageSize -> Medium];
combHist = Show[
    Rasterize[hist1],
    Rasterize[hist2],
    PlotRange->All
];

Export[Directory[] <> "/figures/test-hist1.pdf", hist1];
Export[Directory[] <> "/figures/test-hist2.pdf", hist2];
Export[Directory[] <> "/figures/test-combHist.pdf", combHist];

data1:

data2:

Combined:

How do I correct the bounds for this?


Update: Using m_goldberg's suggestion, I'm able to get what I want:

I'll leave this opened for a little for any further discussion, but I'm satisfied with this now. Thanks to everyone who helped!


Further update (sorry): I'm a little unsatisfied with the results for my actual data. The first one appears fine, but the second is overly distorted:

Does anybody have any further suggestions for these?

$\endgroup$
4
  • $\begingroup$ I don't fully understand why that works. Is there any weird hack to get Show to act as I expect here? $\endgroup$ Commented Nov 27, 2012 at 2:48
  • $\begingroup$ Hi @Highphi, what about switching the X-Axis to Log X or Ln X? $\endgroup$
    – Zviovich
    Commented Nov 27, 2012 at 20:56
  • $\begingroup$ Just a guess, but perhaps their are few outlying data points that are expanding the plot range unduly. Perhaps you can restrict the plot range with something like `PlotRange -> {{0,45000},{0,100}}. $\endgroup$
    – m_goldberg
    Commented Nov 28, 2012 at 1:00
  • $\begingroup$ If its point clouds, try downsampling your data to every 100th point, for example. Then you can still get the sense of the distribution, but see through one to the other. $\endgroup$
    – MikeY
    Commented Jan 9, 2019 at 15:48

3 Answers 3

10
$\begingroup$

I you add PlotStyle -> Opacity[0.5] to histOpts you can to evaluate Show[hist1, hist2] to get

overlaid plots

$\endgroup$
2
  • $\begingroup$ Sorry for forgetting about this! $\endgroup$ Commented Mar 14, 2013 at 22:42
  • $\begingroup$ Beautiful plot... $\endgroup$
    – MikeY
    Commented Jan 11, 2019 at 19:01
5
$\begingroup$

Would this work for you?

g1 = Rasterize[hist1];
g2 = Rasterize[hist2];
ImageMultiply[hist1,hist2]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Yes! See my update for a slight issue I'm having. $\endgroup$ Commented Nov 27, 2012 at 3:35
3
$\begingroup$

Another way to go about this is to visualize in 3D. Taking the two data sets above, concatenate "1" to all the elements of the first data set and "-1" to the second data set.

dat1 = Partition[Flatten[Riffle[data1, 1, 2]], 3];
dat2 = Partition[Flatten[Riffle[data2, -1, 2]], 3];

Then you can plot in 3D.

ListPointPlot3D[{dat1, dat2}]

The advantage is that you can interactively rotate and play with the graph to view it from the different angles.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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