Posts Tagged ‘plot’

Plot a bunch of edges in matlab with per-edge color data

Monday, December 19th, 2016

Suppose you have a list of vertices V (#V by dim) and a list of edges E (#E by 2) indexing V, then one hacky to draw all of these edges with per-vertex color data DV (#V by 1) is to use trisurf:

trisurf(E(:,[1 1 2]),V(:,1),V(:,2),V(:,3),'CData',DV,'EdgeColor','interp');

However, if you try to use this trick to draw per-edge color data DE (#E by 1), you’ll find that 'EdgeColor','flat' does not work. Instead you can explode you compact “mesh” of edges into individual edges and repeat you edge color data for each edge point:


Retrieve matlab plot colors

Tuesday, August 4th, 2015

matlab plot line colors

I couldn’t find any documentation about where the plot line colors are stored or how to access them. Here’s a hacky way to retrieve them. Currently plot cycles through 7 colors:

p = plot(repmat(1:7,2,1)); 
plot_colors = reshape([p.Color],3,7)';

matlab plot line colors


The correct way as Daniele writes in the comments is:


Enlarge matlab axis by a specific factor

Tuesday, April 8th, 2014

Here’s the code I use to double or enlarge the axis of a plot window by any factor. Say 150%:

a = axis;
axis(a+1.5*[a(1:2)-(a(1)+a(2))*0.5 a(3:4)-(a(3)+a(4))*0.5]);

Speed up slow scatter and line plots in MATLAB

Tuesday, February 4th, 2014

Matlab seems to default to its CPU renderer 'painters' when creating new figures with scattered dots or lines. So


where X and so on are very large can take a long time (say 1 minute).

Whereas first setting the renderer to the GPU renderer 'OpenGL' is much faster.


for the same large X takes 6 seconds.

Awkwardly, plotting a bunch of (disconnected) line segments from vertices in N and edge indices in E in matlab using something like:

plot3([N(E(:,1),1) N(E(:,2),1)]',[N(E(:,1),2) N(E(:,2),2)]',[N(E(:,1),3) N(E(:,2),3)]','k');


line([N(E(:,1),1) N(E(:,2),1)]',[N(E(:,1),2) N(E(:,2),2)]',[N(E(:,1),3) N(E(:,2),3)]');

is much slower to render and interact with than to just draw a bunch of degenerate triangles using:

trisurf([E E(:,1)],N(:,1),N(:,2),N(:,3))

This has the added bonus that it seems to throw the renderer automatically in to ‘OpenGL’ mode.

Correlation does not imply causation

Tuesday, November 26th, 2013

Correlation does not imply causation

Vector field with actual arrows

Sunday, August 11th, 2013

I recently discovered MATLAB’s quiver function which produces visualizations of vector fields. This function creates “arrows” using three line segments. This is fine for prototyping, but not great for making nice images.

Here’s an example using quiver:

[X,Y] = meshgrid(linspace(0,1,40),linspace(0,1,40));
U = sin(X*2*pi).*sin(Y*2*pi)+X;
V = sin(X*2*pi).*cos(Y*2*pi);

which produces:
quiver plot of trig function

Here’s my updated pipeline for making nice images of vector field using MATLAB and Adobe Illustrator.

In Matlab:

% scale appropriately
s = 1e-1;
% plot just lines
plot([X(:) X(:)+s*U(:)]',[Y(:) Y(:)+s*V(:)]','-b');

which produces:
plot of trig function as lines

Save this as an .eps file and open it with illustrator.

Inevitably, matlab will have saved much more than just the line segments. Clean it up a bit. Select one of the “dot” markers (especially visible if any of your lines were very small). Then choose Select > Same > Stroke Weight. This will select all dots. Then hit Delete.

Now select one of the line segments and choose Select > Same > Stroke Weight. This will select all line segments. Now choose an appropriate arrowhead. This will produce something like:
arrows for vector field in illustrator

Mismatching axis size of subplots due to colorbar

Sunday, April 21st, 2013

When using subplots and axis commands the colorbar command in matlab can mess up alignments:

plot([0 100],[0 100]);
axis equal;
plot([0 100],[0 100]);
axis equal;

mismatching axis in matlab due to colorbar

A quick fix to get the fix axis to be the same size as the second is to use:


So that

plot([0 100],[0 100]);
axis equal;
plot([0 100],[0 100]);
axis equal;

mismatching axis in matlab due to colorbar

Sans-serif not supported in MATLAB text using latex interpreter

Thursday, September 6th, 2012

If I try to issue in MATLAB:


I get the following error:

Warning: Font cmss10 is not supported. 
Warning: Font cmss10 is not supported. 
Font creation failed twice. when creating Font object
Using File /Applications/ to create font mwa_cmss10
File.exists(): false
File.length(): 0
Exception: Can't read /Applications/
	at java.awt.Font.createFont(
	at com.mathworks.hg.util.FontConverter.loadTexFontSecondAttempt(
	at com.mathworks.hg.util.FontConverter.loadTexFont(
	at com.mathworks.hg.util.FontConverter.convertToJavaFont(
	at com.mathworks.hg.uij.GraphicsContext.SetWindowFont(
Caused by:
Warning: Could not set font mwa_cmss10 

I fixed this by finding cmss10.ttf on the web. And copying on top of /Applications/

Then when I issue the above command I get the correct output and only a few warnings:

Warning: Font cmss10 is not supported.
Warning: Font cmss10 is not supported.

Nabla (∇) in matlab plot title

Thursday, September 6th, 2012

I tried:


Which produces no errors or warnings on the terminal, but the title in the plot on my mac is just a little question mark in a black box: �
You can do a little better by switching to the latex interpreter, which unfortunately uses that smelly latex font


Title with hidden axis in matlab figure

Thursday, July 5th, 2012

I often hide the current axis in a matlab plot figure using:

set(gca, 'visible', 'off');

But then if I call


The title is not visible (as it is a child object of a hidden axis).
The fix is simple: