Skip to main content

graphics - Programmatically combine 2D contour plots with 1D projections


I regularly need to plot 2D and 1D data together. I want to be able to show a 2D contour plot with the projections along each of the axes. As it is now, I create the three plots separately and use Inkscape to combine them. The problem with this is twofold. First, it is cumbersome if I have to do it over and over. Second, I have to use my eyes to move the 1D plot so that its features line up properly with the 2D data.


As a simple working example, consider this 2D array of data


xRange = {8, 19};

yRange = {6, 16};

twoDlist =
Table[Abs[

I/(ω1 - 14 + I) I/(ω2 - 11 + I) + (
I/3)/(ω1 - 8 + I/3) I/(ω2 - 16 + I)], {ω1,
yRange[[1]], yRange[[2]], .05}, {ω2, xRange[[1]],
xRange[[2]], .05}];

From this I generate the 2D and 1D plots.


topplot = 
ListLinePlot[Total[twoDlist], Axes -> False, DataRange -> xRange,
PlotStyle -> {{Thick, Red}}, PlotRange -> {Full, All}];


rightplot =
ListLinePlot[Total /@ twoDlist, Axes -> False, DataRange -> yRange,
PlotStyle -> {{Thick, Red}}, PlotRange -> All];

contourplot =
ListContourPlot[twoDlist, DataRange -> {xRange, yRange},
ContourShading -> None, Contours -> 20,
PlotRange -> {Full, Full, All},
ContourStyle ->
Table[Blend[{Blue, Green, Yellow, Red}, n], {n, .05, 1, .05}]];


Now I would like to combine them in such a way to get an image like this one:


enter image description here


How can I do this? Ideally, I want to end up with a plotting function that takes twoDlist, xrange, and yrange as arguments.


Thanks in advance.


ETA: So I saw the other thread (How can I make an X-Y scatter plot with histograms next to the X-Y axes?) and while I thought that would answer my question it really didn't. There the 2D plot was an x-y scatter and the 1D plots were histograms.


The first solution offered at that page answers the question in a specific way that isn't what I'm going for. They in essence created three graphics objects and then stuck their corners together. No matter how I mess with their function, I can't get it to give what I'm looking for. I can't really understand how the Graphics and Inset functions work together there, and the issue is compounded by the fact that I want tick labels on the contour plot directly, as in the image above.


The second solution listed on that page just isn't for the data type that I have. That DenstityHistogram has such a cool option as "DistributionAxes" is great, but I need to build the equivalent for this data type.


Here is the progress I've made after looking at that page. Basically I rescale the 1D data sets so that when they are plotted they are in the same region as the 2D plot. Then I wrap them in a Show command. With xRange, yRange, and twoDlist defined as above, this code


combinedplot[data_, xrange_, yrange_] := 


Module[{rightdata, topdata, xspan, yspan, dx, dy, contourplot, rightplot, topplot},
rightdata = Total /@ data;
topdata = Total[data];
xspan = (xrange[[2]] - xrange[[1]]);
yspan = (yrange[[2]] - yrange[[1]]);
dx = xspan/(Length[topdata] - 1.0);
dy = yspan/(Length[rightdata] - 1.0);
rightdata = (rightdata - Min[rightdata])/(Max[rightdata] - Min[rightdata]);
rightdata = rightdata*(xspan/5.0) + xrange[[2]];

rightdata = Transpose[{rightdata, Table[n, {n, yrange[[1]], yrange[[2]], dy}]}];
topdata = (topdata - Min[topdata])/(Max[topdata] - Min[topdata]);
topdata = topdata*(yspan/5.0) + yrange[[2]];
contourplot = ListContourPlot[data, DataRange -> {xrange, yrange}, ContourShading -> None, Contours -> 20, PlotRange -> {Full, Full, All}, ImagePadding -> {{Automatic, Scaled[0.05]}, {Automatic, Scaled[0.05]}}, ContourStyle -> Table[Blend[{Blue, Green, Yellow, Red}, n], {n, .05, 1, .05}], ImageSize -> 500, BaseStyle -> 30];

rightplot = ListLinePlot[rightdata, PlotStyle -> {{Red, Thick}}, Axes -> False, PlotStyle -> {{Thick, Red}}, PlotRange -> All];

topplot = ListLinePlot[topdata, PlotStyle -> {{Red, Thick}}, DataRange -> xrange, Axes -> False, PlotStyle -> {{Thick, Red}}, PlotRange -> All];

Show[contourplot, rightplot, topplot, PlotRange -> All]];


combinedplot[twoDlist, xrange, yrange]

generates this plot:


enter image description here


which is not quite what I'm looking for. In order for it to show the 1D plots, the PlotRange for the contour plot has to be extended, and then it displays tick marks out where they don't make any sense. This can be solved by using the CustomTicks package, but this only gets me here: enter image description here


which again is not quite right. I want to end up with the 1D plots outside the frame of the 2D contour plot. Any suggestions?


Thanks in advance.



Answer



With help from rm-rf, I was able to figure this out. The key lies in setting the ImagePadding the same for each plot, then combining them with Grid.



So with the data defined as


xrange={8,19};
yrange={6,16};
twoDlist=Table[Abs[I/(ω1-14+I) I/(ω2-11+I)+(I/3)/(ω1-8+I/3) I/(ω2-16+I)],{ω1,yrange[[1]],yrange[[2]],.05},{ω2,xrange[[1]],xrange[[2]],.05}];

and the function defined as


getMaxPadding[p_List] := 
Map[Max, (BorderDimensions@
Image[Show[#, LabelStyle -> White, Background -> White]] & /@
p)~Flatten~{{2}, {3}}, {2}] + 1;

combinedplot[data_, xrange_, yrange_, plotopts:OptionsPattern[]] := Module[
{rightdata, topdata, dy, contourplot, rightplot, topplot, padding, dimensions},
rightdata = Map[Total, data];
topdata = Total @ data;
dy = (Part[yrange, 2] + -Part[yrange, 1]) / (Length[rightdata] - 1.);
rightdata = Transpose[
{rightdata, Table[n, {n, yrange[[1]], yrange[[2]], dy}]}
];
contourplot = ListContourPlot[data,
DataRange -> {xrange, yrange},

ContourShading -> None, Contours -> 30, PlotRange -> {xrange, yrange, All},
ContourStyle -> Table[
{Thick, Blend[{Blue, Green, Yellow, Red}, n]},
{n, 1 / 30, 1, 1 / 30}
],
Evaluate @ FilterRules[{plotopts}, Options @ ListContourPlot],
PlotRangePadding -> 0
];
padding = getMaxPadding @ {contourplot};
dimensions = ImageDimensions @ contourplot;

rightplot = ListLinePlot[rightdata,
PlotStyle -> {{Red, Thickness[0.04]}},
Axes -> False, PlotRange -> All, ImageSize -> {Automatic, Part[dimensions, 2]},
ImagePadding -> {{0, 5}, Part[padding, 2]},
PlotRangePadding -> 0, AspectRatio -> 5
];
topplot = ListLinePlot[topdata,
PlotRange -> All, DataRange -> xrange, Axes -> False, PlotStyle -> {{Thickness[0.008], Red}},
ImageSize -> {Part[dimensions, 1], Automatic},
AspectRatio -> (1 / 5),

PlotRangePadding -> 0, ImagePadding -> {Part[padding, 1], {0, 10}}
];
Grid[{{topplot, Null}, {contourplot, rightplot}},
Alignment -> Bottom
]
];

The result is robust to changing the size of the image, or any other options for ListContourPlot. The following input


Grid[{{
combinedplot[twoDlist, xrange, yrange, ImageSize -> 250],

combinedplot[twoDlist, xrange, yrange, ImageSize -> 561,
BaseStyle -> 30],
combinedplot[twoDlist, xrange, yrange, ImageSize -> 561,
BaseStyle -> 30,
FrameLabel -> {Style[
"\!\(\*SubscriptBox[\(Ω\), \(1\)]\)", 30],
Style["\!\(\*SubscriptBox[\(Ω\), \(3\)]\)", 30]},
FrameStyle -> Thick]
}}]


gives this as output. enter image description here


Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.