I have 3-hours data interval for 1 day data. How do i plot a bar graph in matlab?
3 ビュー (過去 30 日間)
古いコメントを表示
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1233027/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1233032/image.png)
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 件のコメント
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1236717/image.png)
- 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:
- Display Groups of Bars on a single plot:Bar graph - MATLAB bar
- Savefig Documentation:Save figure and contents to FIG-file - MATLAB savefig
- Combine Multiple plots on same figure:Combine Multiple Plots - MATLAB & Simulink
I hope the above information helps you.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Graphics Object Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!