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])
Related
I am trying to make a plot like this one:
I want a 2D histogram with bar color proportional to the height and semi-transparent bars.
I tried to put together the examples provided here
x = randn(100, 2);
figure
hist3(x, [20 20]);
colormap(hot) % heat map
grid on
view(3);
%bar color
set(get(gca,'child'),'FaceColor','interp','CDataMode','auto');
%semitransparency
set(gcf,'renderer','opengl');
An example of the result is this:
The semi-transparency is absent.
I do not know if it is a problem of my Matlab version (R2014a) or if ---more probably--- I am messing up something. Maybe the axis handles?
Next
Outside this question: I would also like to add a transparent surface interpolating the histogram values (I got also some problems with this). I think I saw something like this on SO recently, but I cannot find it anymore. Does anyone have some hits?
You didn't actually change the surface transparency in your example. All you did was change renderers. Here's the extra line you would need:
set(get(gca,'child'),'FaceAlpha',0.8);
A value of 1 would be opaque, 0 invisible.
You may also want to change the edge line transparency too:
set(get(gca,'child'),'EdgeAlpha',0.2);
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.
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
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.)
I have an image loaded from disk as a texture, and a same-sized matrix d which has the corresponding depths.
How can I use surf to show me the image as a 3d-model? Simply taking
surf(depthMatrix, img);
doesn't give a nice result since
the camera looks not from the x-y plane in z-direction
It looks fairly black
It doesn't look that smooth although my depth matrix is actually smoothed out when I show it using imshow(depthMatrix, []);
You can use texture mapping to display your image on your surface like so:
surf(depthMatrix,img,... %# depthMatrix is z data, img is an image
'FaceColor','texturemap',... %# Use texture mapping
'EdgeColor','none'); %# Turn off edge coloring
And to address your 3 points:
You can adjust your camera angle with the mouse by pressing the button on the figure, which turns on interactive 3-D rotation. You can also turn interactive rotation on using the function ROTATE3D, or you can change the camera view without the mouse using the function VIEW.
Your plot was looking black because edges are drawn as black lines by default, and there were probably a lot of them.
You can adjust the axis scaling and limits to make your surface appear smoother. For example, axis equal will make data units the same for all 3 axes, so your z axis (which ranges from 0 to 25) will be flattened significantly since your other two axes span ranges in the hundreds. Alternatively, in your call to SURF you can specify x and y data to use for the values on those axes, which can ultimately help you to better adjust the relative scaling between those axes and the z axis.