matlab_graphics


matlab_graphics, a MATLAB code which illustrates how to achieve graphics effects.

I love MATLAB, I really do, but if the default behavior of MATLAB's graphic system doesn't work for you, it can be difficult to discover a technique for getting what you want - moreover, there rarely seems to be a sensible logical structure to explain what is going on, nor a systematic procedure for building an image.

Licensing:

The computer code and data files made available on this web page are distributed under the MIT license

Languages:

matlab_graphics is available in a MATLAB version.

Related Data and Programs:

area_under_curve, a MATLAB code which displays the area under a curve, that is, the points (x,y) between the X axis and the curve Y=F(X).

ball_and_stick_display, a MATLAB code which demonstrates the creation of a 3D ball and stick image;

bezier_surface_display, a MATLAB code which displays a Bezier surface;

box_display, a MATLAB code which displays a box plot, over integer pairs of data, of a function defined by two formulas.

cavity_flow_display, a MATLAB code which displays a single velocity field solution for the driven cavity;

cc_display, a MATLAB code which displays the points used in a 2D Clenshaw-Curtis quadrature rule;

circles, a MATLAB code which can be used, when creating graphics, to draw one or more circles, while controlling the center locations, radii, colors, edge colors, and transparency, by Chad Greene.

fd1d_display, a MATLAB code which reads a pair of files defining a 1D finite difference model, and plots the data.

fem_basis_q4_display, a MATLAB code which displays a finite element method (FEM) basis function associated with a linear quadrilateral (Q4) mesh.

fem_basis_t3_display, a MATLAB code which displays a finite element method (FEM) basis function associated with a 3-node triangle (T3) mesh.

fem_basis_t4_display, a MATLAB code which displays a finite element method (FEM) basis function associated with a 4-node triangle (T4) mesh.

fem_basis_t6_display, a MATLAB code which displays a finite element method (FEM) basis function associated with a 6-node triangle (T6) mesh.

fem1d_display, a MATLAB code which reads three files defining a 1D arbitrary degree finite element function, and displays a plot.

fem1d_function_10_display, a MATLAB code which reads three files defining a 1D piecewise linear finite element function and displays a plot.

gl_display, a MATLAB code which displays the points used in a 2D Gauss-Legendre quadrature rule;

gray_code_display, a MATLAB code which computes the Hamming distance tables for both the binary and Gray codes, and displays 3D plots that illustrate how the Gray code does a better job of providing nearby representations for nearby numbers.

grf_display, a MATLAB code which reads a GRF file defining a mathematical graph and displays it in the MATLAB graphics window.

grid_display, a MATLAB code which reads a file of points on a grid or sparse grid, displays the grid and saves the image in a Portable Network Graphics (PNG) file;

grids_display, a MATLAB code which reads two files of grids or sparse grids, displays the first with hollow blue dots, the second with red dots.

hermite_product_display, a MATLAB code which displays an image of a function created by the Cartesian product of two Hermite polynomials, such as f(x,y) = h(3,x) * h(1,y).

histogram_display, a MATLAB code which makes a bar plot of a set of data stored as columns in a file; the first column is the X values, and all the other columns are Y values to be shown as a stack of bars;

lagrange_basis_display, a MATLAB code which displays the basis functions associated with a given set of nodes used with the Lagrange interpolation scheme.

mesh_display, a MATLAB code which reads data defining a polygonal mesh and displays it, with optional numbering.

nested_sequence_display, a MATLAB code which displays a set of nested sequences.

obj_display, a MATLAB code which reads an OBJ 3D graphics file and displays it on the screen.

polygonal_surface_display, a MATLAB code which displays a surface in 3D described as a set of polygons;

quad_mesh_order1_display, a MATLAB code which plots piecewise constant data associated with a mesh of quadrilaterals;

quad_surface_display, a MATLAB code which plots piecewise bilinear data associated with a QUAD_SURFACE, that is, a 3D surface defined by a quadrilateral mesh;

sequence_streak_display, a MATLAB code which displays a streak plot of a numeric sequence;

shallow_water_1d_display, a MATLAB code which can display a solution of the shallow water equations in 1D, generally as computed by the program shallow_water_1d.

sphere_lebedev_rule_display, a MATLAB code which reads a file defining a Lebedev quadrature rule for the unit sphere and displays the point locations.

sphere_stereograph_display, a MATLAB code which computes and displays the results of several stereographic projections between a sphere and a plane.

stla_display, a MATLAB code which reads an ASCII stereolithography (STL) file and displays it on the screen.

tensor_grid_display, a MATLAB code which can display the grid points of a tensor product rule used for interpolation or quadrature, in 1D, 2D or 3D.

tet_mesh_display, a MATLAB code which reads in the node and element files defining a tet mesh and displays a wireframe image.

tetrahedron_slice_display, a MATLAB code which determines the intersection between a tetrahedron and a plane and displays the result.

tri_surface_display, a MATLAB code which displays the 3D graphics information in a TRI_SURFACE file;

triangle_display, a MATLAB code which displays the nodes and elements of a triangulation on the MATLAB graphics screen, assuming the data has been stored in NODE and ELE files by the TRIANGLE program.

triangulation_display, a MATLAB code which displays the nodes and elements of a triangulation on the MATLAB graphics screen;

triangulation_order1_display, a MATLAB code which plots piecewise constant data associated with a triangulation;

Source Code:

You plot a circle, and it comes out an ellipse. What is going on? Briefly, MATLAB uses a rectangle to display your plot. It takes the X and Y ranges of your data, and stretches them along the horizontal and vertical directions of the rectangle. This guarantees that a plot that is logically square will be displayed as a rectangle, and thus a circle becomes an ellipse. Luckily, the "axis equal" command helps. MATLAB still plots inside a rectangle, but now it stretches your X and Y data by the same single scale factor, resulting in some new white space to the left and right of your plot of a perfect circle.

Surprisingly, a typical MATLAB plot can involve a lot of empty space. In the simple case where you are plotting a circle, you will get white space in the grid region itself if you enforce equal axis units, since MATLAB wants to display the grid within a rectangle. But you will also get a substantial margin of white space around the plot area itself. This can be annoying when you are trying to prepare graphics for use in a publication, or if you want to put several images side by side - the white space, which contains no information, eats up lots of space.

Can you cut back that white space? In particular, if you want to plot a circle (or anything else whose shape doesn't correspond to MATLAB's "golden rectangle"), can you make an image that is mathematically correct, and economic in terms of wasted white space?

You may try various combinations of "axis equal", "axis square", "axis tight", "axis ( [0,1,0,1] )" and so on, all the time coming halfway to your goal, without ever actually achieving an image that is square (or whatever aspect ratio you want), doesn't have wasted graph space, and doesn't have that excessive margin around the plot.

After wasting a lot of time, I stumbled across a few commands that, used together, seem to achieve the correct result. However, in my case, the image still looks rectangular in the MATLAB interactive viewer - but comes out nice and square in the PNG image rendered by the PRINT command.


Last revised on 30 May 2013.