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

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

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}]