How to plot like this in MATLAB
I have tried this, but didn't find desired result
X = 1.2:0.2:3;
Y = 3:0.2:4;
[x, y] = meshgrid(X, Y);
z = x.*y;
contourf(x, y, z, 'ShowText', 'on');
colorbar
title ('Power Efficient Conversion (PEC) Bulk Heterojunction of Solar Cell');
xlabel ('Band Gap [donor]');
ylabel ('LUMO [donor]');
Related
I am looking for a way to shade the background of my semilog plot in two colors.
For example, in the following image, I have am plotting three polynomials and they all are equal at x=1. I want one rectangle for x<1 region and other for x>1 region. How can I insert two such rectangles, of different colors, in background to highlight these two regions.
MWE:
x = 0.1:0.1:10;
y1 = polyval([1, 0], x); % Evaluate y = x;
y2 = polyval([1, 0, 0], x); % Evaluate y = x^2;
y3 = polyval([1, 0, 0, 0], x); % Evaluate y = x^3;
figure
semilogy(x, y1, '.k', x, y2, '.b', x, y3, '.r'); title ('Three
polynomials on a semilog y scale') xlabel('x'); ylabel('y');
legend({'y= x', 'y = x^2', 'y = x^3'}, 'Location', 'Northwest')
You can solve that using area or patch.
As pointed by #SardarUsama, there are others questions with good examples on it, however, you need to avoid to have any zeros in the area data, otherwise it will fail.
Follows the code setting one area only.
x = 0.1:0.1:10;
y1 = polyval([1, 0], x); % Evaluate y = x;
y2 = polyval([1, 0, 0], x); % Evaluate y = x^2;
y3 = polyval([1, 0, 0, 0], x); % Evaluate y = x^3;
figure
plot(x, y1, '.k', x, y2, '.b', x, y3, '.r'); %MODIFIED
hold on %ADDED
title ('Three polynomials on a semilog y scale')
set (gca, 'Yscale', 'log'); %ADDED
xlabel('x');
ylabel('y');
legend({'y= x', 'y = x^2', 'y = x^3'}, 'Location', 'Northwest')
area( [1 1 10 10],[1e-3 1e+3 1e+3 1e-3 ],'FaceColor','green','facealpha',0.3) %ADDED
The code above works for matlab after 2014b. If you have one before that, you can use the patch function (which requires some small change in the data, but uses the Facealpha option) or you can move the area to the background as i do below:
ax=get(gca,'Children'); %ADDED
set(gca,'Children',[ax(2) ax(3) ax(4) ax(1)]); %ADDED, move area to background
Note: Indeed, I missed the problem with the legend. I correct as mentioned, however for me the area was on top of the other graphs. To solve it i changed the order of the plots. If the area was with transparency, this will not be an issue.
How do I plot xyz In rectangular, polar, and 3-D?
for x = 0 to 35pi:
Y = x*sin(x)
Z = x*cos(x)
Using the the intervals of X which provides very smooth plots . Create three plots including tittle and labels .
This is the input I have put in so far. I'm not sure if this is correct:
x = pi*linspace(0,35);
y = pi*x,sin(pi*x);
z = pi*x,cos(pi*x);
plot3(x,y,z)
title('data analysis')
xlabel('independent x')
ylabel('dependent y')
zlabel('z')
I believe this solves the problem as you describe it:
x = linspace(0, 35*pi, 10000);
y = x .* sin(x);
z = x .* cos(x);
plot3(x, y, z);
title('data analysis');
xlabel('independent x');
ylabel('dependent y');
zlabel('z');
I have the following script:
close all; clear all; clc;
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
values = -10:0.5:10;
figure
[C,hh] = contour(X, Y, Z, values,'r', 'LineWidth',1);
clabel(C, hh, values, 'fontsize',7)
As you can see in the contour lines, all of the lines are plotted with LineWidth = 1. I would like to plot special line for the value = 0, with LineWidth = 2, how to set it? Thanks a lor for your help.
You will need to make a secondary contour plot to highlight the desired contour levels. The MathWorks has an example of this in the documentation.
For your case we'll have something like the following:
% Generate sample data
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
values = -10:0.5:10;
% Generate initial contour plot
figure
[C,hh] = contour(X, Y, Z, values,'r', 'LineWidth',1);
clabel(C, hh, values, 'fontsize',7)
% Generate second contour plot with desired contour level highlighted
hold on
contour(X, Y, Z, [0 0], 'b', 'LineWidth', 2);
hold off
Which returns the following:
Not that I've specified the single contour level as a vector. This is explained by the documentation for contour:
contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v. To display a single contour line at a particular value, define v as a two-element vector with both elements equal to the desired contour level. For example, to draw contour lines at level k, use contour(Z,[k k])
If you want to highlight multiple levels then this does not apply (e.g. contour(X, Y, Z, [-1 0], 'b', 'LineWidth', 2) to highlight -1 and 0)
I want to use Octave to plot fairly simple functions with two variables like: f(x,y) = x^2 + 3y . It is very easy to plot single variable functions, but I am having a lot of trouble finding out how to do two variable functions. Does anyone know the best way of doing this?
Plotting a function of two variables would normally mean a 3-dimensional plot - in MATLAB you would use the function plot3 for that. To plot your function f(x,y) in the interval [-10,10] for both X and Y, you could use the following commands:
x = [-10:.1:10];
y = [-10:.1:10];
plot3(x, y, x.^2 + 3*y)
grid on
In case it may help someone out there... I ran in Octave the code in the accepted answer and I got this plot:
But I really wanted the function for every point in the Cartesian product of x and y, not just along the diagonal, so I used the function mesh to get this 3D plot with the projected contour lines in the x,y plane:
x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;
mesh(x, y, z)
meshc(xx,yy,z)
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
grid on
To get rid of the mesh-wire texture of the plot, the function surf did the trick:
x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;
h = surf(xx,yy,z);
colormap hsv;
set(h,'linestyle','none');
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
Another way to plot is as a heatmap with contour lines:
x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + yy.*3;
contourf(xx,yy,z);
colormap hsv;
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
grid on
And for completeness, the levels can be labeled:
x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;
[C,h] = contour(xx,yy,z);
clabel(C,h)
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
grid on
In addition to the excellent answers from #Toni and #esskov, for future plotters of functions with two variables, the contour and contourf functions are useful for some applications.
MATLAB Code (2018b):
x = [-10:.1:10];
y = [-20:.1:20];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy; % Borrowed 4 lines from #Toni
figure
s(1) = subplot(1,2,1), hold on % Left Plot
[M,c] = contour(xx,yy,z); % Contour Plot
c.ShowText = 'on'; % Label Contours
c.LineWidth = 1.2; % Contour Line Width
xlabel('X')
ylabel('Y')
box on
s(2) = subplot(1,2,2), hold on % Right Plot
[M2,c2] = contourf(xx,yy,z);
colorbar % Add Colorbar
xlabel('X')
ylabel('Y')
box on
title(s(1),'Contour Plot')
title(s(2),'Filled Contour Plot')
Update: Added example of surfc
h = surfc(xx,yy,z)
I have a function y=f(x), and I would like to plot in Matlab a histogram where I have the sum of ys for x in[n,n+1).
Is there a function in Matlab doing that automatically?
cumtrapz
http://www.mathworks.com/help/techdoc/ref/cumtrapz.html
Example:
x = linspace(0, pi, 30);
y = cos(x);
yint = cumtrapz(x, y);
plot(x, yint, 'x');
hold on;
% exact integral
plot(x, sin(x), '-');