7
votes

How can I make plots in MATLAB like in below? 1D plot example

I won't need labels, so you can ignore them. I tried using normal 2D plot, by giving 0 to y parameter for each data points. It does help, but most of the plot remains empty/white and I don't want that. 1d plot in 2d figure

How can I solve this problem?

Edit:

This is how I plot(playing with values of ylim does not help):

hold on
for i=1:120
    if genders(v_labels(i)) == CLASS_WOMAN
        plot(v_images_lda(i,:) * w_lda,0,'r*');
    else
        plot(v_images_lda(i,:) * w_lda,0,'b.');
    end
end
title('LDA 1D Plot');
ylim([-0.2 0.2]);
hold off
4
If you are correctly setting the y parameter to 0 then your "empty/white" problem might not be caused by the plotting function. Can you provide more information and some code ?Pepe
I'm working on a two-class gender classification algorithm. I decrease the dimension to 1 using Linear Discriminant Analysis(LDA), and I want to plot it nicely. Simply put, I have a set of numbers that I want to plot on X axis.kolistivra
@kolisivra Well I hate to point out the obvious, but have you tried printing out the dataset you are trying to plot to ensure everything is in order?Pepe
@P.R. What do you mean by 'in order'?kolistivra
@kolistivra As in you don't have any garbage and the dataset being plotted is actually 2D. Try placing v_images_lda(i:) * w_lda in a temp variable before plotting it. That way you can look at the temp variable and see what is actually being plotted.Pepe

4 Answers

11
votes

One way to do this would be to adjust the 'XLim', 'YLim', and 'DataAspectRatio' properties of the axes so that it renders as essentially a single line. Here's an example:

data1 = rand(1,20)./2;      %# Sample data set 1
data2 = 0.3+rand(1,20)./2;  %# Sample data set 2
hAxes = axes('NextPlot','add',...           %# Add subsequent plots to the axes,
             'DataAspectRatio',[1 1 1],...  %#   match the scaling of each axis,
             'XLim',[0 1],...               %#   set the x axis limit,
             'YLim',[0 eps],...             %#   set the y axis limit (tiny!),
             'Color','none');               %#   and don't use a background color
plot(data1,0,'r*','MarkerSize',10);  %# Plot data set 1
plot(data2,0,'b.','MarkerSize',10);  %# Plot data set 2

And you will get the following plot:

enter image description here

6
votes

Here's one way to reproduce your figure using dsxy2figxy and annotate. dsxy2figxy can be hard to find the first time, as it is not really in your path. It is part of the MATLAB package and is provided in the example functions. You can reach it by searching for it in the help docs and once you find it, open it and save it to a folder in your path.

h1=figure(1);clf
subplot(4,1,1);
hold on
xlim([0.2,1]);ylim([-1,1])

%arrow
[arrowX,arrowY]=dsxy2figxy([0.2,1],[0,0]);
annotation('arrow',arrowX,arrowY)

%crosses
x=[0.3,0.4,0.6,0.7,0.75];
plot(x,0,'kx','markersize',10)

%pipes
p=[0.5,0.65];
text(p,[0,0],'$$\vert$$','interpreter','latex')

%text
text([0.25,0.5,0.65],[1,-1,-1]/2,{'$$d_i$$','E[d]','$$\theta$$'},'interpreter','latex')

axis off
print('-depsc','arrowFigure')

This will produce the following figure:

enter image description here

This is sort of a hackish way to do it, as I've tricked MATLAB into printing just one subplot. All rasterized formats (jpeg, png, etc) will not give you the same result, as they'll all print the entire figure including where the non-declared subplots should've been. So to get this effect, it has to be an eps, and it works with it because eps uses much tighter bounding boxes... so all the meaningless whitespace is trimmed. You can then convert this to any other format you want.

3
votes

Ok so the closest I have come to solving this is the following

hax = gca();
hold on
for i=1:120
    if genders(v_labels(i)) == CLASS_WOMAN
        plot(v_images_lda(i,:) * w_lda,0,'r*');
    else
        plot(v_images_lda(i,:) * w_lda,0,'b.');
    end
end

set(hax, 'visible', 'off');
hax2 = axes();
set(hax2, 'color', 'none', 'ytick', [], 'ycolor', get(gcf, 'color');
pos = get(hax, 'position');
set(hax2, 'position', [pos(1), pos(2)+0.5*pos(4), pos(3), 0.5*pos(4)]);
title('LDA 1D Plot');

hold off

So in short, I hid the original axis and created a new one located at 0 of the original axis, and as I couldn't remove the y axis completely I set it's color to the background color of the figure. You can then decide if you also want to play with the tick marks of the x-axis.

Hope this helps!

-1
votes

Very naive trick but a useful one.

Plot in 2d using matlab plot function. Then using edit figure properties compress it to whichever axis you need a 1D plot on !! Hope that helps :)