I have 3-hours data interval for 1 day data. How do i plot a bar graph in matlab?

조회 수: 11 (최근 30일)
MUHAMMAD 2022년 12월 16일
댓글: Walter Roberson 2023년 1월 2일
Help, I need to plot a bar/histogram graph exactly like this website https://www.spaceweatherlive.com/en/archive/2012/05/12/kp.html using Matlab coding.
For briefing, we assume the file name is Kp Value. The Kp Value file will consist of 30 days of 3-hours interval data. For example, 0-3 UT, 3-6 UT, 6-9 UT, ..., 21-24 UT. Therefore, one (1) day data should have 8 bar in one graph. You get what i mean? (You can refer at the link as well).
So now, i want to add 10 days data in one graph where in my task, my date is from 2015-05-12 until 2015-05-22 (exactly ten (10) days). Below i attached the Kp Value file (Kp20150516.txt) for one month of 05-2015 to ploting the bar/histogram bar which I just want to grab the 10 days data (which consists of 3-hours interval data) from 2015-05-12 until 2015-05-22 as i mentioned before from the file name Kp20150516.txt.
This is my idea but it shows the whole 30 days data. It get messy.
date_h=find(and(UT1h>=datenum(2015,05,12,0,0,0),UT1h<datenum(2015,05,21,24,0,0)));
bar(UT1h(date_h),Kp(:,8),0.8,'stacked'); %Kp(:,8) column 8 is the exactly value of Kp.
datetick('x','dd','keeplimits')
ylabel('Kp Index');
xlabel('Days');
This is the graph that i get messy.
Since i want to combine 2 plot in one graph, the value should be 240x1 double and each days must have only 8 bars. Otherwise, it shows error like this
Error using plot
Vectors must be the same length.
Error in Lemah (line 57)
plot(UT1h(date_h),Kp(:,8),'LineWidth',1.2)
Thank you!
댓글 수: 7이전 댓글 5개 표시이전 댓글 5개 숨기기
Adam Danz 2022년 12월 20일
편집: Adam Danz 2022년 12월 20일
The mat file download is not a mat file and it is nearly 1gig.
I tried treating it as a zip file but that also did not work.
Walter Roberson 2023년 1월 2일
The attached file is about 15 kilobytes.
The sharepoint file (not attached) is large, but when I click through the download procedure on the linked page, and save to a local drive, then MATLAB is able to open the file as a .mat file.

댓글을 달려면 로그인하십시오.

채택된 답변

Suvansh Arora 2022년 12월 20일
To resolve the issue mentioned, follow the procedure mentioned below:
• In order to plot different bar graphs for different days of month, you may choose to save bar graphs using ‘savefig’. Legend and figure name will be helpful in identifying the day for which the bar graph is plotted.
for i = 1:8:size(date_h(:))
bar(Kp(i:i+7,8),0.8,'stacked'); %Kp(:,8) column 8 is the exactly value of Kp.
%datetick('x','dd','keeplimits')
ylabel('Kp Index');
xlabel('Hours');
set(gca,'XTickLabel',{'0-3', '3-6', '6-9', '9-12', '12-15', '15-18', '18-21', '21-24'})
legend(sprintf('%.0f Day',Kp(i,3)));
grid on
grid minor
savefig(gcf,'Day '+string(Kp(i,3)));
close(gcf);
end
Above mentioned code will save 30 plots for 30 days and a plot will display the following information:
• Another way to achieve the mentioned task is by plotting multiple graphs on a single plot.
Follow the MATLAB answers articles and documentation below for reference:
1. Display Groups of Bars on a single plot:Bar graph - MATLAB bar
2. Savefig Documentation:Save figure and contents to FIG-file - MATLAB savefig
3. Combine Multiple plots on same figure:Combine Multiple Plots - MATLAB & Simulink
I hope the above information helps you.
댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
MUHAMMAD 2022년 12월 20일
Thank you so much @Suvansh Arora for your help <3

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by