Bar plot with two y axes - matlab

I have the following code for a plot with 2 y-axes in MATLAB. I am glad that the 2-axes feature works, however, I would like to avoid the overlapping of the bars. Also, the categories on the right-hand axis should have different colors, not only yellow, yet it should be somehow clear that they are plotted on the right-hand axis and not the left one. How can this be done?
EONMW = [100 399 500];
RWEMW = [200 996 120];
GermanByEON = [0.2 0.4 0.5];
GermanByRWE = [0.1 0.5 0.9];
EONGermanPortfolio = [0.7 0.2 0.1];
RWEGermanPortfolio = [0.8 0.3 0.6];
years = [2010 2012 2014];
% Plot
values1 = [EONMW; RWEMW]';
values2 = [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio]';
years1 = [years; years]';
years2 = [years; years; years; years]';
figure;
bar(years1,values1);
ylabel('Utilities generation portfolio in MW')
yyaxis right
bar(years2,values2);
legend('EON German portfolio in MW', 'RWE German portfolio in MW',...
'Percentage of German portfolio by EON', 'Percentage of German portfolio by RWE',...
'EON"s percentage of generation in Germany', 'RWE"s percentage of generation in Germany')
legend('Location','northwest')
ylabel('Utilities generation portfolio as percentages')

I agree that it is somewhat difficult to read this sort of plot, but perhaps there's a way to improve the situation a little.
The main thing I changed was adding invisible bars (using NaN) so that the color order remains intact:
function q54071610
EONMW = [100 399 500];
RWEMW = [200 996 120];
GermanByEON = [0.2 0.4 0.5];
GermanByRWE = [0.1 0.5 0.9];
EONGermanPortfolio = [0.7 0.2 0.1];
RWEGermanPortfolio = [0.8 0.3 0.6];
years = [2010 2012 2014];
% Plot
values1 = [[EONMW; RWEMW].' NaN(3,4)];
values2 = [NaN(3,2) [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio].'];
years1 = repmat(years.', [1,6]);
figure;
bar(years1,values1, 'EdgeColor', 'b', 'LineWidth', 2);
ylabel('Utilities generation portfolio in MW')
yyaxis right
hB = bar(years1,values2, 'EdgeColor', 'r', 'LineWidth', 2);
c = lines(6);
for ind1 = 1:6
hB(ind1).FaceColor = c(ind1, :);
end
legend('EON German portfolio in MW', 'RWE German portfolio in MW',...
'Percentage of German portfolio by EON', 'Percentage of German portfolio by RWE',...
'EON"s percentage of generation in Germany', 'RWE"s percentage of generation in Germany')
legend('Location','northwest')
ylabel('Utilities generation portfolio as percentages')
Which results in:
Now you should explain that red-framed bars belong to the right axis whereas blue-framed bars belong to the left.

I'm not sure what exactly these bars mean, and so I may have missed the point of the figure (which could be the main problem here). However, I find this way of presentation not pleasing and misleading, as it takes a lot of effort from the reader to understand which value belongs to which bar, and what is comparable and what's not.
What I suggest here, is not a direct answer to the technical problem (which you have already got from #Dev-iL), but a different solution for the more basic problem - how to visualize these data? I believe that if I'll understand what the numbers represent (percentage from what?) and what you want to emphasize with this plot, I can find a better solution.
First, the code:
EONMW = [100 399 500];
RWEMW = [200 996 120];
GermanByEON = [0.2 0.4 0.5];
GermanByRWE = [0.1 0.5 0.9];
EONGermanPortfolio = [0.7 0.2 0.1];
RWEGermanPortfolio = [0.8 0.3 0.6];
years = [2010 2012 2014].';
values1 = [EONMW; RWEMW].';
values2 = [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio].'*100;
% Plot
colMap = mat2cell(lines(2),[1 1],3); % Choose your favorite colors
figure(2);
% upper plot:
subplot 211
b = bar(years,values1);
set(b,{'FaceColor'},colMap)
xticklabels({}) % remove the years labels, the bottom axes will show them
ylabel('Utilities generation portfolio in MW')
legend('EON German', 'RWE German',...
'Location','northwest')
% bottom plot
subplot 212
b = bar(years,values2);
set(b,{'FaceColor'},repmat(colMap,2,1)) % matching the colors by topic
set(b,{'FaceAlpha'},{1;1;0.6;0.6}) % distiguish between related mesures
xlabel('Year')
ylabel('Utilities generation portfolio (%)')
legend('German portfolio by EON', 'German portfolio by RWE',...
'EON''s generation in Germany', 'RWE''s generation in Germany',...
'Location','north')
The result:
The major things I changed:
Split the bars by the units of the y-axis, but align them by the x-axis
Match the colors of related bars between the plots
Shorten legends and labels
Good luck!

It is best if you define bar(x,y) as b=bar(x,y) then control the options of b one of such is b.FaceColor. Take a look at the following code
EONMW = [100 399 500];
RWEMW = [200 996 120];
GermanByEON = [0.2 0.4 0.5];
GermanByRWE = [0.1 0.5 0.9];
EONGermanPortfolio = [0.7 0.2 0.1];
RWEGermanPortfolio = [0.8 0.3 0.6];
years = [2010 2012 2014];
x=0.2;
% Plot
values1 = [EONMW; RWEMW]';
values2 = [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio]';
years1 = [years; years]';
years2 = [years; years; years; years]';
figure;
b1=bar(years1,values1,x*0.66);
ylabel('Utilities generation portfolio in MW')
yyaxis right
b2=bar(years2,values2,x);
%%%%%%%%%%%%
%%%%%%%%%%%%
%% Defining colors
b1(1).FaceColor=[1 0 0];
b1(2).FaceColor=[0 1 0];
b2(1).FaceColor=[0 0 1];
b2(2).FaceColor=[1 1 0];
b2(3).FaceColor=[0 1 1];
b2(4).FaceColor=[1 0 1];
%%%%%%%%%%%%
%%%%%%%%%%%%
%%
legend('EON German portfolio in MW', 'RWE German portfolio in MW',...
'Percentage of German portfolio by EON', 'Percentage of German portfolio by RWE',...
'EON"s percentage of generation in Germany', 'RWE"s percentage of generation in Germany')
legend('Location','northwest')
ylabel('Utilities generation portfolio as percentages')
which is your code plus the following block
%%%%%%%%%%%%
%%%%%%%%%%%%
%% Defining colors
b1(1).FaceColor=[1 0 0];
b1(2).FaceColor=[0 1 0];
b2(1).FaceColor=[0 0 1];
b2(2).FaceColor=[1 1 0];
b2(3).FaceColor=[0 1 1];
b2(4).FaceColor=[1 0 1];
%%%%%%%%%%%%
%%%%%%%%%%%%
%%
and the result is as follows
also a trigger option x has been added to control bar width.

Related

How to shade rectangles in MATLAB with time on the x axis?

I am trying to plot gray rectangulars on this plot:
t1 = datetime(2002,02,01);
t2 = datetime(2019,11,01);
t = t1:calmonths:t2;
t= datenum(char(t),'dd-mm-yyyy')
plot(t, randn(214,1), 'LineWidth',1,'Color', [0 0 0]);
ax.XTick = t(1:end);
datetick('x','yyyy','keepticks');
yline(0,'-')
ylabel('%')
I would like to have two shaded areas: one that goes from April 2009 to October 2010 and another one that goes from January 2003 to October 2015. I did this in the past but with time on the x-axis complicates it, at least for me.
Can anyone help me out?
Here is an attempt
t1 = datetime(2002,02,01);
t2 = datetime(2019,11,01);
t = t1:calmonths:t2;
t= datenum(char(t),'dd-mm-yyyy');
figure;
hold on
% 1st shaded area
d1=datenum(datetime(2003,01,01));
d2=datenum(datetime(2015,10,01));
x=[[d1,d2],[d2,d1],d1];
y=[-[3,3],[3,3],[-3]];
fill(x, y, 'r');
% 2nd shaded area
d1=datenum(datetime(2009,04,01));
d2=datenum(datetime(2010,10,01));
x=[[d1,d2],[d2,d1],d1];
y=[-[3,3],[3,3],[-3]];
fill(x, y, 'g');
% Time-series
plot(t, randn(214,1), 'LineWidth',1,'Color', [0 0 0]);
ax.XTick = t(1:end);
datetick('x','yyyy','keepticks');
yline(0,'-')
ylabel('%')
You can play on the transparency (alpha) to make it even prettier.

3 x-axis in matlab plot?

I need to plot a figure with 3 x-axes. Each axis is linked to the other by a mathematical formula. I want to do this because the x value can be seen as wavelength [nm], velocity [m/s] or energy [eV] and I want the reader to not have to convert it themselves on each graph.
I searched online and only found something for 2 x-axes, but no more.
Edit: I am using version R2011a.
So it should look like this, which I (obviously) didn't create in MATLAB:
Thanks in advance!
As shown in this answer, you can create a new axes object with near-zero height, so that it is essentially just the x-axis. Be aware that all actual plots must be done on the first axes as this is the area you can see!
Demo code:
% Create some plotting data and plot
x = 0:0.1:2*pi; y = sin(x);
% Plot, can specify line attributes (like LineWidth) either
% - inline: plot(x,y,'linewidth',2)
% - after: p1 = plot(x,y); p1.LineWidth = 2;
plot(x,y);
% Get current axes object (just plotted on) and its position
ax1 = gca;
axPos = ax1.Position;
% Change the position of ax1 to make room for extra axes
% format is [left bottom width height], so moving up and making shorter here...
ax1.Position = axPos + [0 0.3 0 -0.3];
% Exactly the same as for plots (above), axes LineWidth can be changed inline or after
ax1.LineWidth = 2;
% Add two more axes objects, with small multiplier for height, and offset for bottom
ax2 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.15 0 0], 'color', 'none', 'linewidth', 2);
ax3 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.00 0 0], 'color', 'none', 'linewidth', 2);
% You can change the limits of the new axes using XLim
ax2.XLim = [0 10];
ax3.XLim = [100 157];
% You can label the axes using XLabel.String
ax1.XLabel.String = 'Lambda [nm]';
ax2.XLabel.String = 'Velocity [m/s]';
ax3.XLabel.String = 'Energy [eV]';
Output:
Edit:
Before the 2014b graphics changes you will need to make a couple of tweaks for getting and setting axes properties. The equivalent code would more heavily use the set command, and look something like this:
x = 0:0.1:2*pi; y = sin(x);
plot(x,y);
ax1 = findobj(gca, 'type', 'axes')
axPos = get(ax1, 'Position');
set(ax1, 'Position', axPos + [0 0.3 0 -0.3]);
set(ax1, 'LineWidth', 2);
ax2 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.15 0 0], 'color', 'none', 'linewidth', 2);
ax3 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.00 0 0], 'color', 'none', 'linewidth', 2);
set(ax2, 'xlim', [0 10]);
set(ax3, 'xlim', [100 157]);
axes(ax1); xlabel('Lambda [nm]');
axes(ax2); xlabel('Velocity [m/s]');
axes(ax3); xlabel('Energy [eV]');
Here's an example of how you can do this:
msx = [1 50 60 90];
msy = [0 1 3 8];
lx = 90/4*[1 2 3 4]; % Scale the data with respect to the data that will use the "primary" X-axis
ly = [0 2 8 10];
evx = 90/19*[1 7 10 19]; % Scale the data with respect to the data that will use the "primary" X-axis
evy = [0 8 16 20];
figure
a=axes('units','normalized','position',[.1 .35 .7 .6],'xlim',[0 100],'xtick',0:10:100);
plot(lx, ly)
hold on
plot(msx, msy)
hold on
plot(evx, evy)
xlabel(a,'velocity m/s')
b=axes('units','normalized','position',[.1 .21 .7 0.000001],'xlim',[0 4],'color','none', 'xtick',0:1:10);
xlabel(b,'lambda nm');
c=axes('units','normalized','position',[.1 .10 .7 0.000001],'xlim',[0 19],'color','none', 'xtick',0:1:19);
xlabel(c,'energy eV');
For the position: specified as a four-element vector of the form [left bottom width height]. The default value of [0 0 1 1] includes the whole interior of the container. (see https://de.mathworks.com/help/matlab/ref/axes-properties.html)
Output figure:

Plotting unit circles in matlab and using the hold on to insert individual data points and plotting the intersection with circle

I was trying to produce the following figure:
as similar as possible. I was having difficulties because I wasn't sure how to plot the unit circle. How does one do that? I tried using the polar function but I couldn't then make it also plot the blue crosses and red crosses. Anyone has an idea?
Edited answer
Here is a part of the code:
% --- Plot everything on the same axes
hold on
% --- Plot the unit circle:
theta = linspace(0, 2*pi, 360);
plot(cos(theta), sin(theta), 'k--');
% --- Plot the blue points
x = [0.2 0.4 0.4 0.8];
y = [0.4 0.2 0.8 0.4];
scatter(x, y, 'bs');
% --- Plot the red points:
x = [0.4 0.8];
y = [0.4 0.8];
scatter(x, y, 'ro');
There are still many modifications to do to get the final plots, but at least this is a starting point.
Second edit
To answer your question about the intersection of a line and a circle, you have to start with the maths behind. Line and circle are defined by the following equations:
y = a.x + b % Cartesian definition of a line
(x-x0)² + (y-y0)² = r² % Cartesian definition of a circle
If you combine the two, you realize that finding the intersection is similar to finding the solution of:
(a²+1).x² + 2(a(b-y0)-x0).x + x0²+(b-y0)²-r² = 0
i.e. the roots of a polynom. As this is a trinom, there are 3 possibilities: 0 solution (no intersection), 1 solution (line is tangent to the circle) and 2 solutions (line crossing the circle).
So, in practice you have to:
Get the parameters a, b, x0, y0 and r of your problem
Find the roots of the polynom (for instance, with the function roots)
Decide what to do based on the number of roots.
Hope this helps,
%% Construct polar grid (put inside another function)
range = 0.2:0.2:1;
n = 50;
for i=1:length(range)
ro = ones(1,n);
ro = ro.*range(i);
th = linspace(0,pi*2,n);
[xx,yy] = pol2cart(th,ro);
plot(xx,yy, 'color',[.9 .9 .9]), grid on, hold on
n = round(n * 1.5);
end
%% Plot like normal
x1 = [.2 .4 .4 .8];
y1 = [.4 .2 .8 .4];
x2 = [.4 .8];
y2 = [.4 .8];
plot(x1,y1, 'bx',...
x2,y2, 'ro');
xlim([-.05 1]);
ylim([-.05 1]);

Drawing star-like figure in Matlab

I had to plot this in matlab :
so I wrote the following code:
x=[0.9 1 0.9 -0.9 0.7 0.7 -0.9 0.9];
y=[-1 0 1 -0.5 -0.8 0.8 -0.4 -1];
plot(x,y);
but this gives me:
Is this method insufficient to draw the first figure....Is there some other short method for this...
You can do better positioning your data points
nPoints = 7;
th = linspace(-2*pi, 2*pi, 2*nPoints+1);%// extra point to close the curve
r = 1;
x = r*cos(th);
y = r*sin(th);
scatter( x, y, 'ro' );
hold on;
plot( x(1:2:end), y(1:2:end), 'b-' );
axis equal;
Resulting with:
BTW, this code works for any odd nPoints - just try ;)
It seems to me that you want to construct a star polygon, which has the property that all points are equidistant to its neighboring points, and all points lie on the same circle.
First, we generate the desired angles a (measured from the x-axis to a line that connects the origin with the desired point, see wikipedia). Based on the polygon, we rearrage the angles in such a way that a desired number of points is skipped (in the code below, this is done by using among others repmat - but many alternatives exist).
To convert the angles to actual points in the plane, we take sine and cosine values, and then we can plot the dots and lines. Putting this together results in the following code, which results in the dersired figure
n = 7;
a = linspace(0,2*pi,n+1);
skip = 1;
b = [repmat(a(1:end-1),1,skip+1) a(end)];
a = b(1:skip+1:end);
figure;
clf;
hold on;
plot(cos(a),sin(a),'b-');
plot(cos(a(1:end-1)),sin(a(1:end-1)),'ro');
axis([-1 1 -1 1])
axis equal
A little less involved would be to compute the vector a like this:
a = mod(linspace(0,(skip+1)*2*pi,n+1),(skip+1)*2*pi);
I tried manually:
x = [-0.4 0.6 1 0.6 -0.4 -0.8 -0.8];
y = [-0.9 -0.7 0 0.7 0.9 0.4 -0.4];
x_ = [x,x];
y_ = [y,y];
figure;
hold on;
for ii=1:numel(x),
scatter(x(ii),y(ii),'ro');
plot([x_(ii),x_(ii+1+1)],[y_(ii),y_(ii+1+1)],'b-');
end
axis([-1 1 -1 1]);
And I get his:

Plot Shaded Area Between Two User-Defined Lines and Beneath Preexisting Data

I am trying to add a grey shaded area between two user-defined vertical lines. I need this gray shaded area to appear beneath my already plotted data. I have tried using the fill and area functions, but haven't been able to successfully create a bounded area to appear underneath the Matlab plot. I need the shaded area plot to extend from the x-axis at vertical lines created at 5.5 and 19 and extend up to the y-axis at 900 (left y-axis) and at 1 (right y-axis). See here: https://www.dropbox.com/s/qyzkuhw17yxn8p5/sample.png
I suppose the problem does not lie in generating the shaded area, but in making it appear below other elements of the plot even though it gets plotted later. You can achieve that by locating it "deeper", i.e. at a smaller z-value. Here is an example, building on the code by randomatlabuser:
x = linspace(0, 24, 100);
plot(x, 450 - 400 * cos(x / 12 * pi), 'k.-')
hold all
x = [5.5 5.5 19 19 5.5];
y = [0 900 900 0 0];
patch(x, y, -1 * ones(size(x)), [0.9 0.9 0.9], 'LineStyle', 'none')
patch is essentially the same as fill, but has a version that supports a third coordinate, the z-value.
From what I understand you want to do this:
h1 = plot((0:24), (0:700/24:700), '-b', 'LineWidth', 10); % some function
hold on
h2 = plot((0:24), 1.5 * (0:24) .^2, '-r', 'LineWidth', 10); % some other function
x = [5.5 5.5 19 19 5.5]; y = [0 900 900 0 0]; % define edges of area
h3 = fill(x, y, [0.9 0.9 0.9], 'LineStyle', 'none'); % fill area
set(gca, 'Children', [h1 h2 h3]) % h3 in the background, then h2 and finally h1 upfront
If not, you need to explain better what you are after.
% Add shaded area to plot
% Line 1
hl1 = line(x1,y1,'Color','b');
x = [5.5 5.5 19 19]; y = [0 900 900 0]; % define edges of shaded area
% Locations for 5:30am and 19:00pm with the left y-axis ranging from 0-900
hl2=fill([x(1) x(2) x(3) x(4)], [y(1) y(2) y(3) y(4)],'Color','r','EdgeColor','none','LineStyle','none');
hl1 = line(x1,y1,'Color','b'); % Replot line 1 over shaded area
hold on;
% Continue adding more shaded areas using the fill function or add lines directly...