matlab.graphics.chartcontainer.mixin.Colorbar class

Package: 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 'off' or 'on'. 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)

Use c to change the colormap to cool.

c.Colormap = cool;

Introduced in R2019b