Main Content

matlab.graphics.chartcontainer.mixin.Colorbar Class

Namespace: matlab.graphics.chartcontainer.mixin

Add colorbar support to chart container subclass

Description

matlab.graphics.chartcontainer.mixin.Colorbar is a class for adding colorbar support charts that inherit from matlab.graphics.chartcontainer.ChartContainer. By inheriting from this class, you can display a colorbar in your chart. Your users can select options in the figure menu bar or the figure toolbar to show or hide that colorbar.

Use this syntax to enable colorbar support for your chart:

classdef MyChart < matlab.graphics.chartcontainer.ChartContainer & ...
                   matlab.graphics.chartcontainer.mixin.Colorbar
  ...
end

The matlab.graphics.chartcontainer.mixin.Colorbar class is a handle class.

Class Attributes

Abstract
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Properties

expand all

Colorbar visibility, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

When this property is set to 'on', the colorbar is visible in the chart.

Attributes:

GetAccess
public
SetAccess
public
Dependent
true
NonCopyable
true

Methods

expand all

Examples

collapse all

Define a class named SurfImagePlot that displays a surface with an image underneath it.

To define the class, create a file named SurfImagePlot.m that contains the following code. The setup method for this class performs these tasks:

  • Creates a Surface object with an offset on the ZData to provide enough space to display the image

  • Creates an Image object

  • Configures the axes

  • Makes the colorbar visible by setting the ColorbarVisible property to 'on'

classdef SurfImagePlot < matlab.graphics.chartcontainer.ChartContainer & ...
        matlab.graphics.chartcontainer.mixin.Colorbar
    
    properties
        ZData (:,:) double = []
        Offset (1,1) double = 10
        Colormap (:,3) double {mustBeGreaterThanOrEqual(Colormap,0),...
            mustBeLessThanOrEqual(Colormap,1)} = parula
    end
    properties(Access = private,Transient,NonCopyable)
        Surface (1,1) matlab.graphics.chart.primitive.Surface
        Image (1,1) matlab.graphics.primitive.Image
    end
    
    methods(Access = protected)
        function setup(obj)
            % Get the axes
            ax = getAxes(obj);
            
            % Create surface and image objects
            obj.Surface = surf(ax,[],[],[]);
            hold(ax,'on')
            obj.Image = imagesc(ax,[]);
            
            % Configure axes, make colorbar visible
            view(ax,3)
            axis(ax,'tight')
            grid(ax,'on')
            obj.ColorbarVisible = 'on';
            hold(ax,'off')
        end
        function update(obj)
            % Update Data and Colormap
            ax = getAxes(obj);
            [r,c] = size(obj.ZData);
            [X,Y] = meshgrid(1:c,1:r);
            obj.Surface.XData = X;
            obj.Surface.YData = Y;
            obj.Surface.ZData = obj.ZData + obj.Offset;
            obj.Image.CData = obj.ZData;
            colormap(ax,obj.Colormap)
        end
    end
end

Next, define matrix Z as the z-coordinates of a surface. Plot Z by calling the SurfImagePlot constructor method, which is provided by the ChartContainer class. Specify the 'ZData' name-value pair argument and return the object as c.

[X,Y] = meshgrid(-10:1:10);
Z = X.^2 + Y.^2;
c = SurfImagePlot('ZData',Z)
c = 

  SurfImagePlot with properties:

       ZData: [21x21 double]
      Offset: 10
    Colormap: [256x3 double]
    Position: [0.1300 0.1100 0.7232 0.8150]
       Units: 'normalized'

  Use GET to show all properties

Use c to change the colormap to cool.

c.Colormap = cool;

Version History

Introduced in R2019b