plot many columns to different figure
4 views (last 30 days)
Show older comments
Hello! I have 2004 columns of data. I want to plot the first column (X) with all others (Y). i.e.: plot(X,Y1) plot(X,Y2) . . . plot(X,Y2004)
If I do this and run the program, I take one figure only with the first plot. If I write plot(X, Y(:,1:2004)), I take one figure with 2003 lines. I don't want this. I want to take 2003 figures which are X,Y1, the other figure will be X,Y2 and so on. Also, I would like to do this in one step.
Is there an idea?
Thank you for your help.
0 Comments
Accepted Answer
Star Strider
on 6 Sep 2014
You could create a for loop to generate 2004 different plots if you want to:
for k = 1:2004
figure(k)
plot(X, Y(:,k))
end
I would not.
I would use a ribbon plot for what you want to do. There is no way 2004 different plots are going to be meaningful otherwise.
2 Comments
Star Strider
on 8 Sep 2014
II am not certain I understand: ‘I want to do linear fitting and save all these as bmp images. I did linear fitting in each figure separately, but I'd like to do this into the loop!’
You can use polyfit to do the fitting, and then polyval if you want to plot the regression as well:
X = sort(rand(10),2);
Y = sort(rand(10),2);
for k1 = 1:size(X,1)
cfs(k1,:) = polyfit(X(k1,:), Y(k1,:), 1);
yf = polyval(cfs(k1,:), X(k1,:));
figure(k1)
plot(X, Y(:,k1),'+b', X(k1,:),yf,'-r')
print(sprintf('Figure(%d).bmp',k1), '-dbmp')
end
I tested everything but the print call in that loop (since I didn’t want to have to go deleting those files), so you may have to experiment with that line in your application. The first column of ‘cfs’ has the slopes and the second column the intercepts for each regression. I do not have your data so I cannot use it to design my code, but I would do something like what I outlined here if I were to do what I believe you want to do.
Star Strider
on 9 Sep 2014
‘I added the function p=polyfit(X,Z(:,k)) or the function linearFit(X,Z(k)) and did not work.’
You have to tell polyfit the order of the polynomial you want to fit. For a linear fit, the order is 1.
Use this:
p=polyfit(X, Z(:,k), 1);
More Answers (4)
Image Analyst
on 6 Sep 2014
There is no way you're going to see 2002 plots of data unless you have a very large monitor. I would not use bar charts, line/curves, ribbon plots or anything like that. I'd display it as an image. Even then, unless you have more than 2003 pixels across, you're not going to see all the columns. I'm not sure what your x is but assuming that the x's are linearly spaced and increasing, I'd just display the y as an image:
image(Y(:,2:end));
Apply a colormap if you want
colormap(jet(256)); % Or gray(256)
colorbar;
If your monitor is not 2003 columns across, MATLAB will shrink the image down so that it fits on the screen, though you can zoom it up to the original size if you want. So each column is a line of pixels on your monitor. No other type of display (ribbons, or other 3D-like graphics) will fit - you won't see them unless you are zoomed in so much that you then won't see the entire data set at the same time.
The other option is to use plot() with a different color for each column and plot everything on one axes. Of course they'll all pile on top of each other but it will let you see the average (i.e., the band of curves where the bulk of the curves lie) and outlier curves. In fact you might plot the average over the mess of lines in a thicker line in a color like red so you can see it. But I think an image is better, particularly if there is some relationship/correlation between nearby columns.
0 Comments
Fani
on 8 Sep 2014
Edited: Fani
on 8 Sep 2014
1 Comment
Image Analyst
on 8 Sep 2014
I'm not sure what you did. Did you use plot() or ribbon() as Star suggested, or an image like I suggested? Who are you replying to? I'm not sure what kind of BMP image you want. Can you show us a screenshot of what you're starting with? With my suggestion you'd already have an image, but I don't know what you mean by doing a "linear fitting". Fitting of what to a line?
Fani
on 9 Sep 2014
Edited: Fani
on 9 Sep 2014
2 Comments
Star Strider
on 9 Sep 2014
@Fani — I outlined a way to do what you want in my comment to my Answer yesterday. What about that did not work?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!