Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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},
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[

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




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?

share|improve this question
I don't fully understand why that works. Is there any weird hack to get Show to act as I expect here? – Brandon Amos Nov 27 '12 at 2:48
Hi @Highphi, what about switching the X-Axis to Log X or Ln X? – Zviovich Nov 27 '12 at 20:56
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}}. – m_goldberg Nov 28 '12 at 1:00
up vote 6 down vote accepted

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

overlaid plots

share|improve this answer
Sorry for forgetting about this! – Brandon Amos Mar 14 '13 at 22:42

Would this work for you?

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

Mathematica graphics

share|improve this answer
Yes! See my update for a slight issue I'm having. – Brandon Amos Nov 27 '12 at 3:35

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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