shift x axis to plot daytime hours before nighttime hours

I have a plot where the x axis is the hour of the day. Right now the xaxis starts at 0 and ends at 24. Instead, I want to plot the daytime hours before the nighttime hours so that it starts at x=8 and ends at x=7 so that I can view all the nighttime data together. Can someone tell me how to set up the plot this way?
Thank you!
This is my code right now:
f1=figure(1)
hold on; box on;
yyaxis left
yline(0)
bar(1:24,mfbin,'facecolor',gray);
errorbar(1:24,mfbin,sdfbin,'k.','linewidth',e)
ylabel('Filtered Flux (mmol/m^2/h)')
ylim([-70 70])
yyaxis right
plot(1:24,mMPARbin,'b.','markersize',30);
yline(100,'b')
% errorbar(1:24,mMPARbin,sdMPARbin,'k.','linewidth',e);
ylabel('PAR')
ylim([-1000 1000])
xlim([0 24])
set(gca,'fontsize',18)
ax=gca;
ax.YAxis(1).Color='k'
ax.YAxis(2).Color='b'

 採用された回答

Walter Roberson
Walter Roberson 2020 年 8 月 16 日

1 投票

That only makes sense if the data is valid for 24 hours starting at 8am, not if the data is for one particular calendar day.
If the data is valid for 24 hours, then take
H = datetime('today','Format', 'HH') + hours(8:24+7);
errorbar(H, circshift(mfbin,-7), circshift(sdfbin,-7), 'k.', 'linewidth', e)
and similar.

8 件のコメント

Heidi Hirsh
Heidi Hirsh 2020 年 8 月 16 日
I'm not sure I understand what you mean about the data validity. My plot represents 17 days of data that is binned hourly. Bars represent hourly means. So it should be valid for 24 hours starting at 8am. Does that make sense?
Do I need to adjust the line for plotting the bars as well?
I have attached the data (mfbin, sdfbin,mMPARbin) here if it helps.
Heidi Hirsh
Heidi Hirsh 2020 年 8 月 16 日
編集済み: Walter Roberson 2020 年 8 月 17 日
I tried the code below (changes are bold) but I got the following error:
Error using bar (line 213)
Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double.
figure
hold on; box on;
yline(0)
H = datetime('today','Format', 'HH') + hours(8:24+7);
bar(H, circshift(mfbin,-7),'facecolor',gray);
errorbar(H, circshift(mfbin,-7), circshift(sdfbin,-7), 'k.')
% bar(1:24,mfbin,'facecolor',gray);
% errorbar(1:24,mfbin,sdfbin,'k.','linewidth',e)
ylabel('Filtered Flux (mmol/m^2/h)')
ylim([-70 70])
Walter Roberson
Walter Roberson 2020 年 8 月 17 日
Are you wanting to plot 24 hours or are you wanting to plot 17 days.
If the first day starts at midnight in the data then where do you want the data between midnight and 8am of the first day to be plotted?
If you have 17 days then is there already a variable indicating the date for each day?
Heidi Hirsh
Heidi Hirsh 2020 年 8 月 17 日
This is my desired plot: (I apologize for the Frankenstein's monster plot - I cut it apart to make this mock up)
Basically I'm plotting the same hourly means but I want them grouped so that the nighttime hours are together (so the pattern in the middle of the night becomes clear)
Walter Roberson
Walter Roberson 2020 年 8 月 17 日
Data inputs must match the axis configuration.
You had existing graphs on the plot. You start out with "hold on" so it would not have erased any existing graph.
Heidi Hirsh
Heidi Hirsh 2020 年 8 月 18 日
I removed that first 'hold on' and the bars plotted correctly (see below), but I get error messages when I try to add the error bars:
Error using errorbar (line 76)
Input arguments must be numeric or objects which can be converted to double.
Error in composite (line 889)
errorbar(H, circshift(mfbin,-7), circshift(sdfbin,-7), 'k.')
I'm unsure which inputs it doesn't like.
Also, can I still label the xaxis with just the hours (the dates don't mean anything at this point).
Again, thank you so much for your help with this.
Walter Roberson
Walter Roberson 2020 年 8 月 18 日
It appears that the underlying graphics objects used to create errorbars do not permit non-numeric x coordinates.
You can use xticklabels() to cause the output labels to be whatever you want, so you could use "hours since 08:00" as your x but label it as appropriate. The difficulty with that approach is that it does not adjust data cursors. You can program data cursors to hide the underlying values if need be... but it is easier if you just do not support data cursors.
Heidi Hirsh
Heidi Hirsh 2020 年 8 月 19 日
Thank you for all your help Walter! I try setting up the tick labels like you said and I'll leave the error bars off the plot for now.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by