Meshgrid stretching surface plot

7 views (last 30 days)
Moritz P.
Moritz P. on 22 Jun 2015
Answered: Mukul Rao on 23 Jun 2015
Hi, I've been trying to add axis to a surf plot i have. I've tried various suggestions but can't get it to work. I have 3 matrices:
final -> 3460x300 double
spec -> 1x300 double (x-axis)
timedate -> 1x3460 double (y-axis)
The timedate matrix values are converted time and dates with date2num.
I tried
plot = surf(final);
set(plot,'LineStyle','none');
which gives me the correct graph but the axis are wrong. See image:
When i try
[xx,yy] = meshgrid(spec,timedate)
plot2 = surf(xx,yy,final);
set(plot,'LineStyle','none');
It gives me the correct axis but the graph seems stretched
How could i solve this?

Answers (1)

Mukul Rao
Mukul Rao on 23 Jun 2015
I have three suggestions that might work for you:
1. Reconsider your stance on the issue at hand. Are you really sure that the first plot is correct? According to the documentation,
"surf(Z) creates a three-dimensional shaded surface from the z components in matrix Z, using x = 1:n and y = 1:m, where [m,n] = size(Z). The height, Z, is a single-valued function defined over a geometrically rectangular grid."
Note that the default X-Y grid here is in linear scale and the default X-Y data points themselves are linear.However, in the real scenario, a grid created with the ACTUAL X-Y data, may have data points scattered non-linearly in a linearly scaled graph. Here is an example to demonstrate what I mean:
x = [1:10].^3;
y = rand(10,1);
plot(y)
figure
plot(x,y)
Notice figure 2 contains a stretched version of the plot in figure 1. However, figure 2 actually contains the correct plot! The plot in figure 2 got stretched because the actual x-data points are non-linearly spaced.
I have a feeling this explanation is applicable to your case too.
2. Generate the first plot and capture the plot box aspect ratio in a variable.
% Generate first plot
pbr = get(gca,'PlotBoxAspectRatio')
%Close this plot and generate the second plot
%Now set the plot box ratio to pbr
set(gca,'PlotBoxAspectRatio',pbr)
3. If the above suggestion does not help, you can manually overwrite the tick labels in your first plot, with the required values. For example, to overwrite the values of the X-axis ticks with 'spec' values, try the following:
%Generate the first plot , make sure the variables spec etc exist in the workspace
%Get current figure axes handle
axesHandle = gca;
%Get current BIN tick values
CurrentTicks = axesHandle.XTick;
bins = 1:length(spec);
p = polyfit(bins,spec,1);
NewTicks = polyval(p,CurrentTicks);
%Reset the tick values
axesHandle.XTickLabel = NewTicks;
%Change the tick label mode to manual to prevent resize changes
axesHandle.XTickMode = 'manual'
axesHandle.XTickLabelMode = 'manual'

Tags

Community Treasure Hunt

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

Start Hunting!