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

3 views (last 30 days)
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.
Kp = load('Kp20150516.txt');
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 Comments
Adam Danz
Adam Danz on 20 Dec 2022
Edited: Adam Danz on 20 Dec 2022
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
Walter Roberson on 2 Jan 2023
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.

Sign in to comment.

Accepted Answer

Suvansh Arora
Suvansh Arora on 20 Dec 2022
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.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!