Two x-axis for the same contourf plot

27 次查看(过去 30 天)
I have a data set for which I make a 2D contourf plot. I have two x-axis which i would like to use at the same time (Energy and Wavelength).
The code producing the two above plots are:
figure
contourf(flipud(y(1).energy), degrees5, log10(conx_spectrum), 500, 'edgecolor', 'none')
colormap(jet)
title('Full excitation laser dependence')
xlim([1.1515 3.52759]);
xlabel('Energy [eV]')
ylabel(['Angle of polariser [' char(176) ']'])
figure
contourf(flipud(y(1).wavelength), degrees5, log10(conx_spectrum), 500, 'edgecolor', 'none')
colormap(jet)
title('Full excitation laser dependence')
xlim([351.47 1076.72]);
xlabel('Wavelength [nm]')
ylabel(['Angle of polariser [' char(176) ']'])
Is there a way, where i can have the Energy axis at the bottom, and Wavelength axis at the top?
Of course the wavelength axis at the top would have to have the same ticks as the energy, therefore being strectched.
Thank you.

采纳的回答

Adam Danz
Adam Danz 2020-7-31
编辑:Adam Danz 2020-7-31
Matab does not currently offer a second x axis.
You can create two duplicate axes and place the x-ticks of the bottom axis on top. Then, scale the second set of x-values to align with the ticks of the first set of x-values.
Here's a demo. The order of many of these steps is important. See footnotes for other important details.
% Produce figure with 2 overlapping axes
fig = figure();
ax0 = axes();
ax0.Position(3:4) = ax0.Position(3:4) * .95; % [1]
ax = copyobj(ax0, fig);
% Plot contour on top axes (demo data)
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X) + cos(Y);
co = contourf(ax,X,Y,Z,10); % [2]
% Set x axis ticks, limit, xlabel, and any
% other properties of the main axis here. [3]
ax.XTick = -6:2:6;
ax.XLim = ax.XLim; % Important to set xlim
xlabel(ax, 'Wavelength [nm]')
% Link the 2 axes [4]
linkaxes([ax0,ax],'y')
linkprop([ax,ax0], {'position','xScale','units',...
'box','boxstyle','DataAspectRatio','FontName',...
'FontSize','xlim','xtick'});
% Set the bottom axis x-axis to the top and remove its y axis
ax0.XAxisLocation = 'top';
ax0.YTick = [];
% Compute the x ticks for the 2nd x-axis on top by
% scaling the 2nd x-values to the first x-values.
x2Limits = [1, 3.5]; % The [min, max] values of the second x-axis (Energy) [5]
x2LimScaled = (x2Limits-x2Limits(1))*range(ax.XLim) + ax.XLim(1);
X1TickNorm = (ax.XTick - ax.XLim(1)) / range(ax.XLim);
X2Tick = (X1TickNorm * range(x2LimScaled)) + x2LimScaled(1);
% Sanity check: number of x2 ticks equals number of x1 ticks
assert(isequal(numel(X2Tick),numel(ax.XTick)), 'Tick lenghts between the two x axes are not equal.')
set(ax0, 'XTickLabel', compose('%.1f',X2Tick)) %[6]
xlabel(ax0, 'Energy [eV]')
Footnotes
[1] Reduce the size of the axes to make room for the upper x-axis label added at the end.
[2] The axes will look ugly for now but will be fixed later.
[3] It's important to set all axis properties before linking the axes.
[4] If any properties are added or removed to linkprop(), test it thoroughly.
[5] You'll need to compute these values. They are the x-axis limits of your second contour plot.
[6] compose('%.1f, __) sets tick labels to 1 decimal place. Adjust as needed.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by