Does the matlab spectrogram function lock the created figure in anyway? I want to draw vertical lines on the figure but the line function does not seem to do anything. How can I draw a line on a matlab spectrogram?
clc; clear all; close all;
[data, fs, nbits] = wavread(<INSERT WAVE FILE HERE>);
% [data, fs, nbits] = wavread('white_0.05_6sec_aud.wav');
N_data=length(data); N_frame=128; N_half=N_frame/2; N_loop=N_data/(N_half);
output=zeros(N_data,1);
hz=0:(fs/2)/N_half:(fs/2)-(fs/2)/N_half;
spectrogram(data, hanning(N_frame), N_half, N_frame, fs);
x = [6500 6500];
y = [0 5.5];
H = gca;
% set(gca, 'NextPlot', 'add');
% line(x, y);
h = line([6500, 6500], [0, 5.5]);
set(h, 'parent', handles.predicted_ax);
% view(-90,90)
% set(gca,'ydir','reverse')
%
% [y, x] = ginput(1)
% view(-90, 180);
The spectogram generates a surf and sets the view to (0,90). The surf sets the zlim to some values (dependent on the spectrogram data), and apparently, adding a line to the current plot does not change the zlim (probably because spectrogram locked the axes somehow; should be findable in edit spectrogram). Therefore, in view(0,90), the line completely disappears (rotate the plot; you'll see the line appear somewhere above the surface).
To resolve: the way you add a line defaults to the line having z-coordinates of [0 0],
which, for many spectrograms, will be above the range of the axes set by spectrogram.
Issuing
zl = zlim;
axis([xlim ylim zl(1) max(0, zl(2))])
view(0,90)
after the line should then make the line appear.
Also: in my case, the surface over which the line was hovering was mostly blue, as was the line. This doesn't help making it apparent there is a line :) I made it white, which contrasted better with the blue/yellow/red surf below.
Related
I have the following figure
that I created using the following code
%% figures
DateNumObs=datenum(table2array(ADCPCRUM2(1:1678,ColumnYear)),table2array(ADCPCRUM2(1:1678,ColumnMonth)),table2array(ADCPCRUM2(1:1678,ColumnDay)),table2array(ADCPCRUM2(1:1678,ColumnHour)),table2array(ADCPCRUM2(1:1678,ColumnMinutes)),table2array(ADCPCRUM2(1:1678,ColumnSeconds)));
Flipecart=permute(ecart(1:1677,:),[2,1]);
Flipecartreel=permute(ecartreel(1:1677,:),[2,1]);
bottomVel=min(min(min(Magnitude)),min(min(velocityModel*1000)));
topVel=max(max(max(Magnitude)),max(max(velocityModel*1000)));
bottomVer=min(min(Flipecart))
topVer=max(max(Flipecart))
figure
subplot(4,1,1);
FlipMag=permute(Magnitude,[2,1]);
[C,h] =contourf(DateNumObs,1:1:22,FlipMag);
datetick('x','dd/mm/yy','keeplimits','keepticks')
caxis manual
caxis([bottomVel topVel])
c=colorbar;
c.Label.String = 'Horizontal velocity(mm/s)';
xlabel('Date');
ylabel('Depth(m from bottom)');
set(h,'LineColor','none')
title('Observation');
subplot(4,1,2);
[C,h] =contourf(DateNumObs(1:1677),1:1:22,MagMatrixH1*1000);
datetick('x','dd/mm/yy','keeplimits','keepticks')
caxis manual
caxis([bottomVel topVel])
c=colorbar;
c.Label.String = 'Horizontal velocity(mm/s)';
xlabel('Date');
ylabel('Depth(m from bottom)');
set(h,'LineColor','none')
title('Model D1');
subplot(4,1,3)
% x0=10;
% y0=10;
% width=550;
% height=400
gcf=plot(DateNumObs(1:1677),Flipecart(10,:))
% set(gcf,'LineWidth',1,'position',[x0,y0,width,height]) % Part giving the error
datetick('x','dd/mm/yy','keeplimits','keepticks')
caxis manual
caxis([bottomVer topVer])
subplot(4,1,4)
c=colorbar;
plot(DateNumObs(1:1677),Flipecartreel(10,:))
datetick('x','dd/mm/yy','keeplimits','keepticks')
caxis manual
caxis([bottomVer topVer])
I am trying to got the normal plot to be the same size as the (blue) contourf plots by using the code which is commented in the code I posted. I got this code from https://nl.mathworks.com/matlabcentral/answers/65402-how-to-set-graph-size .
However, when I try to run it it gives me the following error:
Error using matlab.graphics.chart.primitive.Line/set
There is no position property on the Line class.
Error in StatisticsSOLA (line 315)
set(gcf,'LineWidth',1,'position',[x0,y0,width,height])
I also tried is it possible to change the height of a subplot? but I get the same error. How do I prevent this error and change the width of the bottom two figures?
You are trying to set the position of the axes and the linewidth of the line object in one go, but are not providing the correct handles. Furthermore, don't store the handles of the lines in gcf, since this is a reference to the currently active figure.
Instead you can do:
data = rand(100,200); % some data
fig = figure(1); clf;
% first subplot with colorbar
ax(1) = subplot(211);
imagesc(data)
c = colorbar;
% second subplot without colorbar
ax(2) = subplot(212);
p = plot(data(1,:))
% set height and width of second subplot
drawnow % needed to get right position value of ax(1) and ax(2)
ax_pos = [ax(2).Position(1:2) ax(1).Position(3:4)]; % keep ax(2)'s left and bottom position, and set same width and height as ax(1)
set(ax(2), 'Position', ax_pos)
Alternative
Sometimes it is easier to create colorbar in the second axes, and hide it. This way you don't have to set the positions of the axes yourself.
data = rand(100,200); % some data
fig = figure(1); clf;
% first subplot with colorbar
ax(1) = subplot(211);
imagesc(data)
c = colorbar;
% second subplot without colorbar
ax(2) = subplot(212);
p = plot(data(1,:))
c = colorbar; % draw useless colorbar,
c.Visible = 'off'; % and hide it
Figure should look the same:
I am trying to format my graph so that the border ends at the max x and max y so there is not extra space between them and the border. Also, I'm trying to completely replace my first plot command with my second one. Should I just delete my first plot? Currently, the second plot goes over my first plot, removing most of my formatting.
clear all, close all
%%command to clear all variables and log history
x = linspace(-pi, pi, 200);
%creating x variable between - pi and 200 with pi distance between each
%value
y = sin(x) + cos(x);
%Creating y varable with the help of x
figure
plot(x,y)
title('2-D Plot')
xlabel('Theta (in radians)')
ylabel('Function Y')
xlim([-pi pi])
ylim([-1.414 1.414])
grid
plot(x,y,'r--')
grid
To fit the axes box tightly around the data without manually adjusting the axis limits, use:
axis tight;
and instead of re-plotting, you can update the relevant properties of the line.
x = linspace(-pi, pi, 200);
y = sin(x) + cos(x);
figure;
h = plot(x,y); %handle for the line plot
title('2-D Plot');
xlabel('Theta (in radians)');
ylabel('Function Y');
grid;
axis tight; %to set the axis limits equal to the range of the data
set(h, 'LineStyle', '--', 'Color', 'r'); %Updating the plot with required changes
I put two colorbars in the same image using this submission on filexchange.
The position of the first colorbar is set by:
colorbar('WestOutside')
the position of the second one by:
colorbar('EastOutside')
Does anyone know why the first one is longer?
When looking at the Matlab documentation it seemed to me that they should be the same. What am I missing?
The skeleton of the code is the following:
%define coordinates of the nodes
theta=linspace(0,2*pi,33);
[x,y]=pol2cart(theta,1);
%define colormap of the links
cm = winter;
colormap(cm);
%plot the links
for ii=1:N
quiver(...)
end
%place the first colorbar
hcb=colorbar('EastOutside');
%freeze the first colorbar
cbfreeze(hcb);
%define the second colormap
cm = autumn;
colormap(cm);
%plot the dots
for ii=1:N
plot(...)
end
%place the second colorbar
hb=colorbar('EastOutside');
The key is to use two different axes. Based on your code:
%define coordinates of the nodes
theta=linspace(0,2*pi,33);
[x,y]=pol2cart(theta,1);
%plot the links
close
figure;
for ii=1:100
quiver(x,y)
end
%define colormap of the links
ax1 = gca;
colormap (ax1,winter)
%place the first colorbar
hcb=colorbar(ax1,'EastOutside');
%this is tentative, just to get the axes right position:
hb=colorbar(ax1,'WestOutside');
pos = ax1.Position;
hb.delete
% second colorbar
ax2 = axes;
colormap (ax2,autumn)
hb=colorbar(ax2,'WestOutside');
ax2.Position = pos;
axis off
ax1.Position = pos;
It creates this:
Using MATLAB 2015a.
I have a surf plot, in which I would like to have two y-axes. I cannot seem to find any other discussion quite like this.
The closest I got so far is:
surf(peaks);
view(0,0)
ax(1) = gca;
axPos = ax(1).Position;
ax(2) = axes('Position',axPos, 'Color', 'none','XTick',[],'ZTick',[],'YAxisLocation','right');
linkprop(ax, 'CameraPosition');
rotate3d on
% Desired plot
surf(peaks);
% Save axis
ax(1) = gca;
% Use the position of the first axis to define the new axis
pos = ax(1).Position;
pos2 = pos - [0.08 0 0 0];
ax(2) = axes('Position',pos2,'Color', 'none');
% Plot random line in 3D, just make sure your desired axis is correct
plot3(ones(length(peaks),1), 10:10:length(peaks)*10,...
ones(length(peaks),1), 'Color','none')
% Make plot, and non-desired axes, invisible
set(gca,'zcolor','none','xcolor','none','Color','none')
% Link axes
linkprop(ax, 'View');
I have been currently using a Matlab script that plots a function and prints its data into a png file, but I have a problem with this last phase.
The fact is that, moving from plotted image to png files, some data points disappear.
Here are my images.
What Matlab (correctly) plots (note that the maximum is (0; 7.7)):
And here's what Matlab prints into the file:
Note that all the points in the Y asymptote are missing!
Why does this happen?
Here's my code:
grafico2D('filename.dat', -1);
...
function [result] = grafico2D(filename, max_X)
% function that loads data from a two-column file and plot them in 2D image
close all;
dati = load(filename); % load data from file
X = dati(:,1); % X axis
Y = dati(:,2); % Y axis
temp = size(X);
lungX = temp(1);
hFig = figure();
set(hFig, 'Visible', 'on');
plot(X, Y, 'red');
hold on;
ylim([0 1]);
if(max_X==-1)
xlim([1 X(lungX)]);
else
xlim([1 max_X]);
end
xlim
title(strrep(filename,'_','\_')); % substitute _ with \_
xlabel('annotation prediction position'), ylabel('likelihood');
grid
print(hFig, '-dpng', filename); % stampa su file
% }
result = 1;
end
Is there someone who can help me?
Thanks!
You can increase the resolution of your image with the -r option which specifies the resolution in dot per inch (with 90 as default). It should solve your problem (at least it did for me). See the Matlab print function documentation.
PS: you could also use thicker lines with the line property LineWidth.
You could just extend the x-limit a little bit:
xlim(xlim() - [0.05 0])