Main Content

getTheme

Class: matlab.graphics.chartcontainer.ChartContainer
Namespace: matlab.graphics.chartcontainer

Get figure theme of chart container subclass

Since R2025a

Description

gt = getTheme(obj) returns the GraphicsTheme object for the parent figure of the specified ChartContainer object. To determine the theme of the figure, query the BaseColorStyle property of gt.

The update method of the chart container executes when the figure theme changes, so you can call this method from within the update method of your subclass to respond to changes in the figure theme.

example

Input Arguments

expand all

Chart container, specified as an object of the class that inherits from the matlab.graphics.chartcontainer.ChartContainer base class.

Output Arguments

expand all

Graphics theme information, returned as a GraphicsTheme object that has the properties in this table.

PropertyDescription

Name

Short description of the theme, returned as 'Light Theme' or 'Dark Theme'.

BaseColorStyle

Basic color style of the theme, returned as 'light' or 'dark'. This property indicates the overall lightness or darkness of the theme. Query this property when deciding on a color palette for your chart or when writing a ThemeChangedFcn callback.

Attributes

Protectedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Define a class named ThemedSmoothPlot that plots a set of data using a faint line as well as a smoothed version of the same data using a bolder line.

To define the class, create a file named ThemedSmoothPlot.m that contains the following class definition with these features:

  • XData and YData public properties that store the x- and y-coordinate data for the original line

  • SmoothWidth public property that controls the width of the smooth line

  • OriginalLine and SmoothLine private properties that store the Line objects for the original and smoothed data

  • A setup method that initializes OriginalLine and SmoothLine

  • An update method that updates the plot when the user changes the value of a property

  • A createSmoothData method that calculates a smoothed version of YData

  • An updateColor method that selects plot colors based on the theme of the figure

classdef ThemedSmoothPlot < matlab.graphics.chartcontainer.ChartContainer
    properties
        XData (1,:) double = NaN
        YData (1,:) double = NaN
        SmoothWidth (1,1) double = 2
    end
    properties(Access = private,Transient,NonCopyable)
        OriginalLine (1,1) matlab.graphics.chart.primitive.Line
        SmoothLine (1,1) matlab.graphics.chart.primitive.Line
    end

    methods(Access = protected)
        function setup(obj)
            % Create the axes
            ax = getAxes(obj);

            % Create the original and smooth lines
            obj.OriginalLine = plot(ax,NaN,NaN);
            hold(ax,"on")
            obj.SmoothLine = plot(ax,NaN,NaN);
            hold(ax,"off")
        end
        function update(obj)
            % Update line data
            obj.OriginalLine.XData = obj.XData;
            obj.OriginalLine.YData = obj.YData;
            obj.SmoothLine.XData = obj.XData;
            obj.SmoothLine.YData = createSmoothData(obj);

            % Update line colors and smooth line width
            updateColor(obj);
            obj.SmoothLine.LineWidth = obj.SmoothWidth;
        end

        function sm = createSmoothData(obj)
            % Calculate smoothed data
            v = ones(1,10)*0.1;
            sm = conv(obj.YData,v,"same");
        end

        function updateColor(obj)
            OriginalLineColor = [0.6 0.7 1]; 
            SmoothLineColor = [0.3 0.4 0.7];
            thm = getTheme(obj);
            switch thm.BaseColorStyle
                case "light"
                    % Use OriginalLineColor and SmoothLineColor
                    obj.OriginalLine.Color = OriginalLineColor;
                    obj.SmoothLine.Color = SmoothLineColor;
                case "dark"
                    % Flip OriginalLineColor and SmoothLineColor
                    obj.OriginalLine.Color = fliplightness(OriginalLineColor);
                    obj.SmoothLine.Color = fliplightness(SmoothLineColor);
            end
        end
    end
end

Create a light-themed figure. Then plot pair of x and y vectors by calling the ThemedSmoothPlot constructor method and specifying the XData and YData name-value arguments.

f = figure(Theme="light");
f.Position(3:4) = [695 420];
x = 1:1:100;
y = 10*sin(x./5) + 8*sin(10.*x + 0.5);
ThemedSmoothPlot(XData=x,YData=y);

Change the theme of the figure to "dark". The line colors update in response.

theme(gcf,"dark")

Version History

Introduced in R2025a