MATLAB Examples

scattertext documentation

This function creates color-scaled text labels.

Contents

Syntax

scattertext(x,y,c)
scattertext(...,text)
scattertext(...,'colormap',ColorMap)
scattertext(...,'TextProperty',TextValue)
scattertext(...,'clim',[cmin cmax])
h = scattertext(...)

Description

scattertext(x,y,c) places numeric values c as text labels centered on x,y and color-scaled to the values of c using the current colormap.

scattertext(...,text) scales color of cell array text to values of c.

scattertext(...,'colormap',ColorMap) specifies a colormap for text, and does not hijack the current colormap or affect the colorbar in any way.

scattertext(...,'TextProperty',TextValue) formats text labels with 'fontweight', 'fontangle', etc.

scattertext(...,'clim',[cmin cmax]) specifies limits of color scaling. This can be useful if you want to match label colors to current color axis limits. That'd be accomplished with ...,'clim',caxis). By default, limits are [min(c) max(c)].

h = scattertext(...) returns a label handle h.

Examples 1: Formatting

For the following formatting examples we'll use this arbitrary data:

x = rand(6,1);
y = rand(6,1);
z = round(200*rand(6,1)-60)/10;

Example 1a: Simplest-case

The simplest way to use scattertext is akin to a simple call of scatter, but in addition to color-scaling data points, data values are also printed directly. Here's a side-by-side comparison of scatter and scattertext:

figure('pos',[100 100 1000 450])
subplot(121)
scatter(x,y,60,z,'filled')
title scatter
axis([0 1 0 1])

subplot(122)
scattertext(x,y,z)
title scattertext
axis([0 1 0 1])

Example 1b: Text formatting

Text may be formatted easily with any text property name-value pairs:

figure
scattertext(x,y,z,'fontweight','bold','fontsize',40)

Example 1c: Color-scaling non-numeric labels

You may want to color-scale some non-numeric text labels, such as perhaps you'd like to color-scale city names by population. Or perhaps you have some other nonsensical text labels like these:

t = {'dog','cat','pants','lasagna','archduke','sandwiches'}';

figure
scattertext(x,y,z,t)

Example 1d: Specify a colormap

You may specify which does not hijack the current figure's colormap and is in no way related to the colorbar. Simply enter an Mx3 array or RGB values as the colormap like this:

figure
scattertext(x,y,z,t,'colormap',autumn)

Example 2: Match label colors with contour color map.

The clabel function is useful for labeling contour lines, but you may want to place a properly-color-scaled elevation label that does not lie exactly on an elevation contour line. By default, scattertext scales color limits based on the input range of c, but if you're trying to match scattertext colors to a current scatter or contour plot, use 'clim',caxis like this:

% Some gridded surface data:
[X,Y,Z] = peaks(500);
figure
contour(X,Y,Z)

% Find z values at some points:
xi = 2*randn(25,1);
yi = 2*randn(25,1);
zi = interp2(X,Y,Z,xi,yi);
zi = round(zi*10)/10;

% Match label colors to contour colors:
scattertext(xi,yi,zi,'clim',caxis)

Author Info

This function was written by Chad A. Greene of the University of Texas at Austin's Institute for Geophysics, June 2015.