How to get vertical Z axis in 3D surface plot of Matplotlib? - matlab

I am plotting some data as a 3D surface plot using Matplotlib. The code I am using is pretty similar to this 3D surface plot example. It generates the plot shown below:
The problem with this plot is that the Z-axis is not vertical. How do I make the Z-axis vertical?
I examined the different API available and found view_init. By setting a low elevation (elev=1) of the plot using view_init, I can get the Z-axis to be vertical, as shown below:
However, the problem here is that at low elevation the Y axis tick labels are all overwritten and cannot be viewed correctly. So I need the plot to be at a high elevation and with the X-Y plane rotation angle as shown in Figure 1. How do I achieve a vertical Z-axis for this plot?
I do not know Matlab, but I know Matplotlib is derived from the Matlab API. So, I looked at surface plot examples in Matlab documentation and found that the Z-axis seems to be vertical in their examples. One figure for illustration with high elevation is shown below:
Update 1: Based on Bentoy13's suggestion, I set view_init(elev=30, azim=-37.5) and got the below result. It is better, but not yet vertical:
In comparison, Z-axis in Matlab plot is perfectly vertical. Also, as is visible from this angle, I cannot actually use this azim=-37.5 since some information is hidden. Any other solution? :-)

In MPL 2.1.01 they added the option to manually override the projection to generate your 3d graph with an orthographic projection; simply insert the following line into your script:
ax.set_proj_type('ortho')
[Example from MPL source 1]
1 https://matplotlib.org/users/prev_whats_new/whats_new_2.1.0.html

In the examples given in the Matlab documentation, surface plots are often viewed in a special position which is given by the command view:
view(3)
The command view sets the viewpoint of the axes. According to the doc, view(3) sets the default three-dimensional view with azimutal = –37.5 and elevation = 30.
EDIT
The problem of having a vertical z-axis is not that the axis is vertical, but is a problem of perspective. By default, Matlab plots 3D surfaces with a orthographic projection, but matplotlib seems to draw with perspective projection. Sadly matplotlib doesn't provide an API for setting this...
EDIT 2
It appears that the following patch is very useful to draw 3D in orthographic projection:
import numpy
from mpl_toolkits.mplot3d import proj3d
def orthogonal_proj(zfront, zback):
a = (zfront+zback)/(zfront-zback)
b = -2*(zfront*zback)/(zfront-zback)
return numpy.array([[1,0,0,0],
[0,1,0,0],
[0,0,a,b],
[0,0,0,zback]])
proj3d.persp_transformation = orthogonal_proj
I take no credit for this patch, you can find it here.

Related

Changing the plot position of z-axis

I wanted to change the position of z-axis in a 3d graph. I tried to do using graph properties but it does not work, Matlab has this option in 2D plot in axis properties window in the graph, but it does not work in 3d plots. Currently,the plot is at z=0 and I wanted to the position to z=6. Attached is the sketch where I need to change the position of the curve plot (red) from z=0 to z6. I appreciate if there is any solution/suggestion regarding this issue. Thank you.
sketch
Regards,
Alishah
A very simple solution for this question is that convert z in your formula to z-6. You know it from mathematics, It will shift a curve, 6 unit .
If you want to change in right or left, you plus or minus.

Change figure view in matlab

Is there some way of using the surf() style data format to create a heat map?
I have a bunch of scattered data of the form z=f(x,y) (so I used TriScatteredInterp to make it work with meshgrid) and I'd like to visualize z with a heat map. surf already does something similar to what I want, except you have to manually rotate the graph so the view is top down looking at the XY plane.
Basically, I'd like something similar to this:
But surf gives you this by default:
Although the answers here already show how to do this with surf, rendering the 3d surface feels a little like overkill...
pcolor creates the required images directly
(with slightly better results -surf has a gap next to the axes)
code
figure(1)
pcolor(peaks)
figure(2)
surf(peaks)
view(2)
results
pcolor
surf
Adding to Ben's answer, you can use the view command. view allows you to rotate your plot to whatever camera angle you wish.
In general, you call the command like so:
view(AZ, EL);
AZ is the azimuth or horizontal rotation, while EL is the vertical elevation. These are both in degrees.
In your case, once you plot your surf plot, use view(0, 90); before you go to the next subplot. view(0, 90); is the default 2-D view, and this looks directly overhead.
By doing this, you avoid having to rotate your plot manually, then using campos to determine what the camera position is at given your plot. view(0, 90); should give you what you need.
Sidenote
Doing view(2); also gives you the default 2D view, which is equal to view(0, 90); as we talked about. By doing view(3);, this gives you the default 3D view as seen in your plots. FWIW, the default azimuth and elevation for a 3D plot is AZ = -37.5, EL = 30, in degrees of course.
Rotate the view to what you want. Then type campos in the terminal. This shows you the camera position. You can then use campos( your_desired ) to set the camera position for future plots.
For example, the x, y view is usually:
campos([4.0000 2.5000 86.6025])

Change 3D view in matlab

I would like to change the view of a 3D plot in matlab such that the y-axis points upward and the z-axis points to left. For example, consider the following plot:
Here the x-axis points forward, the y-axis points to the right and the z-axis points upward.
I would like to have the y-axis points upward and the z-axis points to the left instead. I tried to rotate the plot (using the figure window toolbar rotate button) but I could not get it to work. (It should just be a simple 90 degrees rotation about the x-axis)
Code to generate the plot:
membrane
view(100,50)
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
grid on
Try using view. I don't have MATLAB available so I can't test it, but I think it can do what you want.
Example from the documentation:
Set the view along the y-axis, with the x-axis extending horizontally
and the z-axis extending vertically in the figure.
view([0 0]);
EDIT:
Try using three inputs to the view function. I can't experiment myself, but you should be able to do it if you choose the right values here.
From documentation:
view([x,y,z]) sets the view direction to the Cartesian coordinates x,
y, and z. The magnitude of (x,y,z) is ignored.
EDIT 2:
Check out camroll. I think camroll(90) (possibly in combination with view) will work.
From documentation:
camroll(dtheta) rotates the camera around the camera viewing axis by
the amounts specified in dtheta (in degrees). The viewing axis is the
line passing through the camera position and the camera target.
This was posted a while ago, but in case someone else is looking for ways to set y-axis as the vertical one here is a possible fix.
Manually: In the command window type cameratoolbar('show') which will open an interactive toolbar in your plot from which you could change the view. One of the options is to set a principle axis to x, y, or z.
Or in you script you could use cameratoolbar('SetCoordSys',coordsys) command which sets the principal axis of the camera motion. coordsys can be: x, y, z, or none.
http://uk.mathworks.com/help/matlab/ref/cameratoolbar.html

How do I add 2 Y axis to my MATLAB Plot?

I know this question has been asked before - but none of them deal with a 3D plot with 2 Y axis. My question is a continuation of - How do I add a 2D Plot along with a surface or mesh plot in MATLAB? or this.
I have now successfully managed to add a 2D plot along with a surface plot. See image below -
Now my problem is that the range of the 2D plot is so high that the 3D plot is shrunk to look like nothing more than a plane on the ceiling. It is supposed to have variations like the figure in the question I have lined above.
How do I provide a different Y axis for the 2D plot so that the 3D plot is not shrunk like it is here.
It's possible that you can use the DataAspectRatio property to accomplish this. Taking inspiration from the example from the previous post, if we have:
z=peaks(100);
x1=linspace(0,100);
plot3(x1,0*ones(1,numel(x1)),40*sin(x1))
surface(z+40, 'edgecolor', 'none');
and then you can use
set( gca, 'dataaspectratio', [1.25 1.25 .7] )
view( [-37.5 18] )
can work in some cases to help regain some of the range on the surface plot. This method won't work, however, in very extreme cases.

Rotating a plot?

I have obtained an x-y plot in Matlab of the sine curve and I wish to rotate this plot by 90 degrees counter clockwise. How do I do this?
In the figure you have plotted, click 'View'->'Camera Toolbar'. Use the Roll Camera icon, and that should allow you to rotate your plot.
EDIT: You can also use the camroll function to do this programatically
camroll(90)
Note, this actually rotates the camera looking at the plot clockwise, not the plot itself. So if you want to rotate the plot 90 degrees counter-clockwise, you will need to rotate the camera 90 degrees clockwise.
Another solution is function view:
view([90 90])
In my opinion this is better solution because there is a problem with labels when one uses camroll function. See code below:
y = rand(1,10);
subplot(211)
plot(1:10,y)
xlabel('x')
ylabel('y')
view([-90 90])
subplot(212)
plot(1:10,y)
xlabel('x')
ylabel('y')
camroll(90)
The best way is to use view([az,el]) that works also for 3d plots.
plot your graph using surf, mesh, etc. and put the graph manually in the desired position using the interactive rotate 3d tool at the tool bar. You see at the left side bottom of the plot the values for the horizontal rotation (azimuth, az) and the vertical elevation (el).
Note the values for az and el and use view([az,el]) to plot.
(When choosing az and el manually it seems like it gives only 2d-plots since the parameters are to be set correctly. Values like [0,1], [0,1], ... normally not work.)