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

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

The error appears to occur in the line plot(UIT1h(date_h)....) but I don't see that line in your example.
We can't run your demo code because we don't know the definitions for UT1h or date_h.
Lastly, I encourage you to use datetime values instead of using datenum.
Hi adam, thank you for your reply!
I've already attached the .m file for you to run the demo.
I used the datetime values, and this error appear.
>> KpValueGraf
Error using >=
Comparison is not defined between double and datetime arrays.
Error in KpValueGraf (line 5)
date_h=find(and(UT1h>=datetime(2015,05,12,0,0,0),UT1h<datetime(2015,05,21,24,0,0)));
In order to understand this better, I would need the following information:
  • MAT file that defines UT1h and date_h and other variables used in the script.
@MUHAMMAD I can't run your script because there are still missing data. If you clear your workspace and try to run your attached script, you will see the same errors we see.
Hi @Suvansh Arora and @Adam Danz, thank you again for your reply.
Below i attached the link of UT1h .mat file (One Drive - size of 988 MB)
Notes: please put the .mat and .m file in the same folder in order to run the UT1h.
UT1h = EE.UT.UT1h;
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');
grid on
grid minor
Until now, i still cant get the graph that i want.
Can you guys help me asap? I need to complete the task before 20-12-2022, Tuesday, 5:00 PM.
I'm so sorry @Adam Danz, make you so confuse with my question.
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.
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

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 2-D and 3-D Plots 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!