Create a multiple horizontal line plot, plotting n

2019-09-01 07:47发布

I have 9 years of data, each has the same variables saved e.g. summer, warmest months, length of sunlit months etc. I want to plot each year's variables as a set of horizontal lines, grouped together, with different line properties. So years will be on the y axis and months on the x axis.

Sorry this is a little vague, I'm not sure how else to describe it.

标签: matlab plot line
1条回答
再贱就再见
2楼-- · 2019-09-01 08:02

The following code might get you close to, if not the exact thing, what you are looking for -

%% PLOT YEARLY DATA ON TOP OF EACH OTHER
%% NOTE: Tinker with the text and plot properties to put Y labels and the custom-made legend at custom positions on the plot

%% Data - Insert your data in this way
years = 2001:2009;
months_string = {'Jan'; 'Feb'; 'Mar';'Apr'; 'May'; 'Jun';'Jul'; 'Aug'; 'Sep';'Oct'; 'Nov'; 'Dec'};
num_years = numel(years);
num_months = numel(months_string);

for count = 1:num_years
    data.year(count).summer = 12.*rand(num_months,1);
    data.year(count).warmest_months = 48*rand(num_months,1);
    data.year(count).len_sunlit = 23*rand(num_months,1);
end

%% Params
offset_factor = 0.5;
x_legend_offset_factor = 0.75;
extension_top_legend = 0.2;
ylabel_pos = -1.0;

%% Add some useful info the struct, to be used later on
for count = 1:num_years
    data.year(count).minval = min([ min(data.year(count).summer) min(data.year(count).warmest_months) min(data.year(count).len_sunlit)]);
    data.year(count).maxval = max([ max(data.year(count).summer) max(data.year(count).warmest_months) max(data.year(count).len_sunlit)]);
    data.year(count).range1 = data.year(count).maxval - data.year(count).minval;
end

%% Global Offset
max_range = max(extractfield(data.year,'range1'));
global_offset = offset_factor*max_range;

off1 = zeros(num_years,1);
for count = 2:num_years
    off1(count) = data.year(count-1).maxval + global_offset;
end
off1 = cumsum(off1);

%% Plot
figure,hold on,grid on,set(gca, 'YTick', []);
xlabel('Months');

for count = 1:num_years
    plot(1:num_months,off1(count)+data.year(count).summer,'b')
    plot(1:num_months,off1(count)+data.year(count).warmest_months,'r')
    plot(1:num_months,off1(count)+data.year(count).len_sunlit,'g')
    text(ylabel_pos,(data.year(count).minval+data.year(count).maxval)/2+off1(count),['Year -  ',num2str(years(count))])
end

% Find Y Limits and extending the plot at the top to accomodate the custom legend
ylimit = off1(num_years) + data.year(num_years).maxval;
ylim_1 = data.year(1).minval;
ylim_2 = ylimit+(ylimit - data.year(1).minval)*extension_top_legend;
ylim([ylim_1 ylim_2])

xlimits = xlim;
x_legend_offset = (xlimits(2) - xlimits(1))*x_legend_offset_factor;

% Adding text to resemble legends
txstr(1) = {'\color{blue} Summer'};
txstr(2) = {'\color{red} Warmest Months'};
txstr(3) = {'\color{green} Length of sunlit months'};
text(x_legend_offset,ylim_2,txstr,'HorizontalAlignment','center','EdgeColor','red','LineWidth',3)
set(gca, 'XTickLabel',months_string, 'XTick',1:numel(months_string))

For a random data, the plot might look like -

enter image description here

Let us know if the above code works for you!

查看更多
登录 后发表回答