bubblechart3 - 3-D bubble chart - MATLAB (original) (raw)

3-D bubble chart

Since R2020b

Syntax

Description

Vector and Matrix Data

bubblechart3([x](#mw%5F3de19022-926b-4661-b77d-abcef87956da),[y](#mw%5F175294fd-3244-4579-89c0-54755329f213),[z](#mw%5F2a84976a-15a5-45f5-bb15-00985aa12b5f),[sz](#mw%5F1dcb83d4-eba4-4467-a19f-6c307197c29d)) displays colored circular markers (bubbles) at the locations specified byx, y, and z, with bubble sizes specified by sz.

example

bubblechart3([x](#mw%5F3de19022-926b-4661-b77d-abcef87956da),[y](#mw%5F175294fd-3244-4579-89c0-54755329f213),[z](#mw%5F2a84976a-15a5-45f5-bb15-00985aa12b5f),[sz](#mw%5F1dcb83d4-eba4-4467-a19f-6c307197c29d),[c](#mw%5F3ff8b1ff-0a8a-44dc-93fa-9b2142216840)) specifies the colors of the bubbles. You can specify one color for all the bubbles, or you can vary the color. For example, you can plot all red bubbles by specifyingc as "red".

example

Table Data

bubblechart3([tbl](#mw%5F5c5d588b-0134-4219-acf2-813f67159f9f%5Fsep%5Fmw%5F1f5b8358-45d8-4c4d-8a0d-17e2da07c841),[xvar](#mw%5F0dac202d-a608-450e-9030-8ead9335b095),[yvar](#mw%5F2035d968-664a-49e9-809f-520df85c9f9c),[zvar](#mw%5F547731d2-2edf-4f10-b904-1009cb7b1f05),[sizevar](#mw%5Fb33cbd03-f54c-4079-b596-6cd87d786723)) plots the variables xvar, yvar, andzvar from the table tbl and uses the variablesizevar for the bubble sizes. To plot one data set, specify one variable each for xvar, yvar,zvar, and sizevar. To plot multiple data sets, specify multiple variables for at least one of those arguments. The arguments that specify multiple variables must specify the same number of variables.

example

bubblechart3([tbl](#mw%5F5c5d588b-0134-4219-acf2-813f67159f9f%5Fsep%5Fmw%5F1f5b8358-45d8-4c4d-8a0d-17e2da07c841),[xvar](#mw%5F0dac202d-a608-450e-9030-8ead9335b095),[yvar](#mw%5F2035d968-664a-49e9-809f-520df85c9f9c),[zvar](#mw%5F547731d2-2edf-4f10-b904-1009cb7b1f05),[sizevar](#mw%5Fb33cbd03-f54c-4079-b596-6cd87d786723),[cvar](#mw%5F93bbd5b1-4c73-4860-b49b-43c7a71561fc)) plots the specified variables from the table using the colors specified in the variablecvar. To specify colors for multiple data sets, specifycvar as multiple variables. The number of variables must match the number of data sets.

example

Additional Options

bubblechart3([ax](#mw%5F34049a0e-efa5-45ce-a916-f3314384619d),___) displays the bubble chart in the target axes ax. Specify the axes before all other input arguments.

example

bubblechart3(___,[Name,Value](#namevaluepairarguments)) specifies BubbleChart properties using one or more name-value arguments. Specify the properties after all other input arguments. For example,bubblechart3(x,y,z,'LineWidth',2) creates a bubble chart with 2-point marker outlines. For a list of properties, see BubbleChart Properties.

example

bc = bubblechart3(___) returns theBubbleChart object. Use bc to modify properties of the chart after creating it. For a list of properties, see BubbleChart Properties.

example

Examples

collapse all

Define a set of bubble coordinates as the vectors x, y, and z. Define sz as a vector that specifies the bubble sizes. Then create a bubble chart of x, y, and z.

x = rand(1,20); y = rand(1,20); z = rand(1,20); sz = rand(1,20); bubblechart3(x,y,z,sz);

Figure contains an axes object. The axes object contains an object of type bubblechart.

Define a set of bubble coordinates as the vectors x, y, and z. Define sz as a vector that specifies the bubble sizes. Then create a bubble chart of x, y, and z, and specify the color as red. By default, the bubbles are partially transparent.

x = rand(1,20); y = rand(1,20); z = rand(1,20); sz = rand(1,20); bubblechart3(x,y,z,sz,'red');

Figure contains an axes object. The axes object contains an object of type bubblechart.

For a custom color, you can specify an RGB triplet or a hexadecimal color code. For example, the hexadecimal color code '#7031BB', specifies a shade of purple.

bubblechart3(x,y,z,sz,'#7031BB');

Figure contains an axes object. The axes object contains an object of type bubblechart.

You can also specify a different color for each bubble. For example, specify a vector to select colors from the figure's colormap.

c = 1:20; bubblechart3(x,y,z,sz,c)

Figure contains an axes object. The axes object contains an object of type bubblechart.

Define a set of bubble coordinates as the vectors x, y, and z. Define sz as a vector that specifies the bubble sizes. Then create a bubble chart of x, y, and z. By default, the bubbles are 60% opaque, and the edges are completely opaque with the same color.

x = rand(1,20); y = rand(1,20); z = rand(1,20); sz = rand(1,20); bubblechart3(x,y,z,sz);

Figure contains an axes object. The axes object contains an object of type bubblechart.

You can customize the opacity and the outline color by setting the MarkerFaceAlpha and MarkerEdgeColor properties, respectively. One way to set a property is by specifying a name-value pair argument when you create the chart. For example, you can specify 20% opacity by setting the MarkerFaceAlpha value to 0.20.

bc = bubblechart3(x,y,z,sz,'MarkerFaceAlpha',0.20);

Figure contains an axes object. The axes object contains an object of type bubblechart.

If you create the chart by calling the bubblechart3 function with a return argument, you can use the return argument to set properties on the chart after creating it. For example, you can change the outline color to purple.

bc.MarkerEdgeColor = [0.5 0 0.5];

Figure contains an axes object. The axes object contains an object of type bubblechart.

Define a data set that shows the contamination levels of a certain toxin across different towns in a metropolitan area.

towns = randi([25000 500000],[1 30]); nsites = randi(10,1,30); nregulated = (-3 * nsites) + (5 * randn(1,30) + 20); levels = (3 * nsites) + (7 * randn(1,30) + 20);

Display the data in a bubble chart. Create axis labels using the xlabel, ylabel, and zlabel functions. Use the bubblesize function to make all the bubbles between 5 and 30 points in diameter. Then add a bubble legend that shows the relationship between bubble size and population.

bubblechart3(nsites,nregulated,levels,towns) xlabel('Industrial Sites') ylabel('Regulated Sites') zlabel('Contamination Level')

bubblesize([5 30]) bubblelegend('Town Population','Location','eastoutside')

Figure contains an axes object and an object of type bubblelegend. The axes object with xlabel Industrial Sites, ylabel Regulated Sites contains an object of type bubblechart.

A convenient way to plot data from a table is to pass the table to the bubblechart3 function and specify the variables you want to plot. For example, create a table with five variables of random numbers. Plot the X1, Y, Z and Sz variables by passing the table as the first argument to the bubblechart3 function followed by the variable names. By default, the axis labels match the variable names.

tbl = table(randn(15,1)-10,randn(15,1)+10,rand(15,1), ... rand(15,1),rand(15,1), ... 'VariableNames',{'X1','X2','Y','Z','Sz'});

bubblechart3(tbl,'X1','Y','Z','Sz')

Figure contains an axes object. The axes object with xlabel X1, ylabel Y contains an object of type bubblechart.

You can also plot multiple variables at the same time. For example, plot X1 and X2 on the _x_-axis by specifying the xvar argument as the cell array {'X1','X2'}. Then add a legend. The legend labels match the variable names.

bubblechart3(tbl,{'X1','X2'},'Y','Z','Sz') legend

Figure contains an axes object. The axes object with ylabel Y contains 2 objects of type bubblechart.

You can plot data from a table and customize the colors by specifying the cvar argument when you call bubblechart3.

For example, create a table with five variables of random numbers, and plot the X, Y, and Z variables. Vary the bubble sizes according to the Sz variable, and vary the colors according to the Colors variable.

tbl = table(randn(15,1)-10,randn(15,1)+10,rand(15,1), ... rand(15,1),rand(15,1), ... 'VariableNames',{'X','Y','Z','Sz','Colors'});

bubblechart3(tbl,'X','Y','Z','Sz','Colors');

Figure contains an axes object. The axes object with xlabel X, ylabel Y contains an object of type bubblechart.

Define two sets of data that show the contamination levels of a certain toxin across different towns on the east and west sides of a certain metropolitan area.

towns1 = randi([25000 500000],[1 30]); towns2 = towns1/3; nsites1 = randi(10,1,30); nsites2 = randi(10,1,30); nregulated1 = (-3 * nsites1) + (5 * randn(1,30) + 20); nregulated2 = (-2 * nsites2) + (5 * randn(1,30) + 20); levels1 = (3 * nsites1) + (7 * randn(1,30) + 20); levels2 = (5 * nsites2) + (7 * randn(1,30) + 20);

Create a tiled chart layout so you can visualize the data side-by-side. Then create an axes object in the first tile and plot the data for the east side of the city. Add a title and axis labels. Then repeat the process in the second tile to plot the west side data.

tiledlayout(2,1,'TileSpacing','compact') ax1 = nexttile;

% East side bubblechart3(ax1,nsites1,nregulated1,levels1,towns1); title('East Side') xlabel('Industrial Sites') ylabel('Regulated Sites') zlabel('Contamination Level')

% West side ax2 = nexttile; bubblechart3(ax2,nsites2,nregulated2,levels2,towns2); title('West Side') xlabel('Industrial Sites') ylabel('Regulated Sites') zlabel('Contamination Level')

Figure contains 2 axes objects. Axes object 1 with title East Side, xlabel Industrial Sites, ylabel Regulated Sites contains an object of type bubblechart. Axes object 2 with title West Side, xlabel Industrial Sites, ylabel Regulated Sites contains an object of type bubblechart.

Reduce all the bubble sizes to make it easier to see all the bubbles. In this case, change the range of diameters to be between 5 and 20 points.

bubblesize(ax1,[5 20]) bubblesize(ax2,[5 20])

Figure contains 2 axes objects. Axes object 1 with title East Side, xlabel Industrial Sites, ylabel Regulated Sites contains an object of type bubblechart. Axes object 2 with title West Side, xlabel Industrial Sites, ylabel Regulated Sites contains an object of type bubblechart.

The east side towns are three times the size of the west side towns, but the bubble sizes do not reflect this information in the preceding charts. This is because the smallest and largest bubbles map to the smallest and largest data points in each of the axes. To display the bubbles on the same scale, define a vector called alltowns that includes the populations from both sides of the city. The use the bubblelim function to reset the scaling for both charts.

alltowns = [towns1 towns2]; newlims = [min(alltowns) max(alltowns)]; bubblelim(ax1,newlims) bubblelim(ax2,newlims)

Figure contains 2 axes objects. Axes object 1 with title East Side, xlabel Industrial Sites, ylabel Regulated Sites contains an object of type bubblechart. Axes object 2 with title West Side, xlabel Industrial Sites, ylabel Regulated Sites contains an object of type bubblechart.

Input Arguments

collapse all

_x_-coordinates, specified as a scalar, vector, or matrix. The size and shape of x depends on the shape of your data. This table describes the most common situations.

Type of Bubble Chart How to Specify Coordinates
Single bubble Specify x, y,z, and sz as scalars. For example:bubblechart3(1,2,3,10)
One set of bubbles Specify x, y,z, and sz as any combination of row or column vectors of the same length. For example:x = [1 2 3 4]; y = [4; 5; 6; 7]; z = [8 9 10 11]; sz = [12 13 14 15]; bubblechart3(x,y,z,sz)
Multiple sets of bubbles If all the data sets share coordinates in one or more dimensions, specify the shared coordinates as a vector and the other coordinates as matrices. The length of the vector must match one of the dimensions of the matrices. For example, plot two data sets that share the same_x_-coordinates and size values.x = [1 2 3 4]; y = [4 5 6 7; 7 8 9 10]; z = [10 11 12 13; 14 15 16 17]; sz = [1 2 3 4]; bubblechart3(x,y,z,sz)If the matrices are square, bubblechart3 plots a separate set of bubbles for each column in the matrices.Alternatively, specify x, y, z, and sz as matrices of equal size. In this case,bubblechart3 plots the columns of the matrices. For example, plot two data sets.x = [1 1; 2 2; 3 3; 4 4]; y = [4 7; 5 8; 6 9; 7 10]; z = [10 14; 11 15; 12 16; 13 17]; sz = [1 1; 2 2; 3 3; 4 4]; bubblechart3(x,y,z,sz)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

_y_-coordinates, specified as a scalar, vector, or matrix. The size and shape of y depends on the shape of your data. This table describes the most common situations.

Type of Bubble Chart How to Specify Coordinates
Single bubble Specify x, y,z, and sz as scalars. For example:bubblechart3(1,2,3,10)
One set of bubbles Specify x, y,z, and sz as any combination of row or column vectors of the same length. For example:x = [1 2 3 4]; y = [4; 5; 6; 7]; z = [8 9 10 11]; sz = [12 13 14 15]; bubblechart3(x,y,z,sz)
Multiple sets of bubbles If all the data sets share coordinates in one or more dimensions, specify the shared coordinates as a vector and the other coordinates as matrices. The length of the vector must match one of the dimensions of the matrices. For example, plot two data sets that share the same_x_-coordinates and size values.x = [1 2 3 4]; y = [4 5 6 7; 7 8 9 10]; z = [10 11 12 13; 14 15 16 17]; sz = [1 2 3 4]; bubblechart3(x,y,z,sz)If the matrices are square, bubblechart3 plots a separate set of bubbles for each column in the matrices.Alternatively, specify x, y, z, and sz as matrices of equal size. In this case,bubblechart3 plots the columns of the matrices. For example, plot two data sets.x = [1 1; 2 2; 3 3; 4 4]; y = [4 7; 5 8; 6 9; 7 10]; z = [10 14; 11 15; 12 16; 13 17]; sz = [1 1; 2 2; 3 3; 4 4]; bubblechart3(x,y,z,sz)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

_z_-coordinates, specified as a scalar, vector, or matrix. The size and shape of z depends on the shape of your data. This table describes the most common situations.

Type of Bubble Chart How to Specify Coordinates
Single bubble Specify x, y,z, and sz as scalars. For example:bubblechart3(1,2,3,10)
One set of bubbles Specify x, y, z, andsz as any combination of row or column vectors of the same length. For example:x = [1 2 3 4]; y = [4; 5; 6; 7]; z = [8 9 10 11]; sz = [12 13 14 15]; bubblechart3(x,y,z,sz)
Multiple sets of bubbles If all the data sets share coordinates in one or more dimensions, specify the shared coordinates as a vector and the other coordinates as matrices. The length of the vector must match one of the dimensions of the matrices. For example, plot two data sets that share the same_x_-coordinates and size values.x = [1 2 3 4]; y = [4 5 6 7; 7 8 9 10]; z = [10 11 12 13; 14 15 16 17]; sz = [1 2 3 4]; bubblechart3(x,y,z,sz)If the matrices are square, bubblechart3 plots a separate set of bubbles for each column in the matrix.Alternatively, specify x, y, z, and sz as matrices of equal size. In this case,bubblechart3 plots the columns of the matrices. For example, plot two data sets.x = [1 1; 2 2; 3 3; 4 4]; y = [4 7; 5 8; 6 9; 7 10]; z = [10 14; 11 15; 12 16; 13 17]; sz = [1 1; 2 2; 3 3; 4 4]; bubblechart3(x,y,z,sz)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

Relative bubble sizes, specified as a numeric scalar, vector, or matrix.

The sz values control the relative distribution of the bubble sizes. By default, bubblechart3 linearly maps a range of bubble areas across the range of the sz values for all the bubble charts in the axes. For more control over the absolute bubble sizes, and how they map across the range of the sz values, see bubblesize and bubblelim.

Whether you specify sz as a scalar, vector, or matrix depends on how you specify x, y, and z and how you want the chart to look. This table describes the most common situations.

Type of Bubble Chart x, y, and z sz Example
One set of bubbles Vectors of the same length A vector with the same length as x,y, and z Specify x, y,z, and sz as vectors.x = [1 2 3 4]; y = [4 5 6 7]; z = [8 9 10 11]; sz = [80 150 700 50]; bubblechart3(x,y,z,sz)
Multiple sets of bubbles that have varied coordinates and bubble sizes At least one of x, y, orz is a matrix for plotting multiple data sets A matrix that has the same size as the x,y, or z matrix Specify x as a vector and y,z and sz as matrices.x = [1 2 3 4]; y = [1 6; 3 8; 2 7; 4 9]; z = [10 11; 12 13; 14 15; 16 17]; sz = [80 30; 150 900; 50 2000; 200 350]; bubblechart3(x,y,z,sz)
Multiple sets of bubbles, where all the coordinates are shared, but the sizes are different for each data set Vectors of the same length A matrix with at least one dimension that matches the lengths ofx, y, andz Specify x, y, andz as vectors and sz as a matrix.x = [1 2 3 4]; y = [5 6 7 8]; z = [9 10 11 12]; sz = [80 30; 150 900; 50 500; 200 350]; bubblechart3(x,y,z,sz)
Multiple sets of bubbles, where the coordinates vary in at least one dimension, but the sizes are shared between data sets At least one of x, y, orz is a matrix for plotting multiple data sets A vector with the same number of elements as there are bubbles in each data set Specify x as a vector, y andz as matrices, and sz as a vector.x = [1 2 3 4]; y = [1 6; 3 8; 2 7; 4 9]; z = [2 8; 3 10; 4 7; 4 12]; sz = [80 150 200 350]; bubblechart3(x,y,z,sz)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Bubble color, specified as a color name, RGB triplet, matrix of RGB triplets, or a vector of colormap indices.

The way you specify the color depends on the preferred color scheme and whether you are plotting one set of bubbles or multiple sets of bubbles. This table describes the most common situations.

Color Scheme How to Specify the Color Example
Use one color for all the bubbles. Specify a color name or a short name from the table below, or specify one RGB triplet. Display one set of bubbles, and specify the color as"red".x = [1 2 3 4]; y = [2 5 3 6]; z = [10 6 4 7]; sz = [1 2 3 4]; bubblechart3(x,y,z,sz,"red")Display two sets of bubbles, and specify the color as red using the RGB triplet[1 0 0].x = [1 2 3 4]; y = [2 5 3 6]; z = [2 5; 1 2; 8 4; 7 9]; sz = [1 2; 3 4; 5 6; 7 8]; bubblechart3(x,y,z,sz,[1 0 0])
Assign different colors to each bubble using a colormap. Specify a row or column vector of numbers. The numbers map into the current colormap array. The smallest value maps to the first row in the colormap, and the largest value maps to the last row. The intermediate values map linearly to the intermediate rows.If your chart has three bubbles, specify a column vector to ensure the values are interpreted as colormap indices.You can use this method only whenx, y, z, andsz are all vectors. Create a vector c that specifies four colormap indices. Display four bubbles using the colors from the current colormap. Then, change the colormap towinter.c = [1 2 3 4]; x = [1 2 3 4]; y = [5 6 7 8]; z = [7 8 9 10]; sz = [1 2 3 4]; bubblechart3(x,y,z,sz,c) colormap(gca,"winter")
Create a custom color for each bubble. Specify an m-by-3 matrix of RGB triplets, where m is the number of bubbles. You can use this method only whenx, y, z, andsz are all vectors. Create a matrix c that specifies RGB triplets for green, red, gray, and purple. Then create a bubble chart using those colors.c = [0 1 0; 1 0 0; 0.5 0.5 0.5; 0.6 0 1]; x = [1 2 3 4]; y = [5 6 7 8]; z = [7 8 9 10]; sz = [1 2 3 4]; bubblechart3(x,y,z,sz,c)
Create a different color for each data set. Specify an n-by-3 matrix of RGB triplets, where n is the number of data sets.You can use this method only when at least one ofx, y, z, orsz is a matrix. Create a matrix c that contains two RGB triplets. Then display two sets of bubbles using those colors.c = [1 0 0; 0.6 0 1]; x = [1 2 3 4]; y = [5 6 7 8]; z = [2 5; 1 2; 8 4; 11 9]; sz = [1 1; 2 2; 3 3; 4 4]; bubblechart3(x,y,z,sz,c)

Color Names and RGB Triplets for Common Colors

Color Name Short Name RGB Triplet Hexadecimal Color Code Appearance
"red" "r" [1 0 0] "#FF0000" Sample of the color red
"green" "g" [0 1 0] "#00FF00" Sample of the color green
"blue" "b" [0 0 1] "#0000FF" Sample of the color blue
"cyan" "c" [0 1 1] "#00FFFF" Sample of the color cyan
"magenta" "m" [1 0 1] "#FF00FF" Sample of the color magenta
"yellow" "y" [1 1 0] "#FFFF00" Sample of the color yellow
"black" "k" [0 0 0] "#000000" Sample of the color black
"white" "w" [1 1 1] "#FFFFFF" Sample of the color white

This table lists the default color palettes for plots in the light and dark themes.

Palette Palette Colors
"gem" — Light theme default_Before R2025a: Most plots use these colors by default._ Sample of the "gem" color palette
"glow" — Dark theme default Sample of the "glow" color palette

You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.

RGB = orderedcolors("gem"); H = rgb2hex(RGB);

Before R2023b: Get the RGB triplets using RGB = get(groot,"FactoryAxesColorOrder").

Before R2024a: Get the hexadecimal color codes using H = compose("#%02X%02X%02X",round(RGB*255)).

Source table containing the data to plot, specified as a table or a timetable.

Table variables containing the _x_-coordinates, specified as one or more table variable indices.

Specifying Table Indices

Use any of the following indexing schemes to specify the desired variable or variables.

Indexing Scheme Examples
Variable names: A string, character vector, or cell array.A pattern object. "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Variable type: A vartype subscript that selects variables of a specified type. vartype("categorical") — All the variables containing categorical values

Plotting Your Data

The table variables you specify can contain numeric, categorical, datetime, or duration values.

To plot one data set, specify one variable each for xvar,yvar, zvar, sizevar, and optionally cvar. For example, read Patients.xls into the table tbl. Plot the Height,Weight, and Diastolic variables, and vary the bubble sizes according to the Age variable.

tbl = readtable('Patients.xls'); bubblechart3(tbl,'Height','Weight','Diastolic','Age')

To plot multiple data sets together, specify multiple variables for at least one ofxvar, yvar, zvar,sizevar, or optionally cvar. If you specify multiple variables for more than one argument, the number of variables must be the same for each of those arguments.

For example, plot the Weight variable on the _x_-axis, theHeight variable on the _y_-axis, and theSystolic and Diastolic variables on the_z_-axis. Specify the Age variable for the bubble sizes.

bubblechart3(tbl,'Weight','Height',{'Systolic','Diastolic'},'Age')

You can also use different indexing schemes for the table variables. For example, specify xvar and xvar as variable names, zvar as an index number, and sizevar as a logical vector.

bubblechart3(tbl,'Height','Weight',9,[false false true])

Table variables containing the _y_-coordinates, specified as one or more table variable indices.

Specifying Table Indices

Use any of the following indexing schemes to specify the desired variable or variables.

Indexing Scheme Examples
Variable names: A string, character vector, or cell array.A pattern object. "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Variable type: A vartype subscript that selects variables of a specified type. vartype("categorical") — All the variables containing categorical values

Plotting Your Data

The table variables you specify can contain numeric, categorical, datetime, or duration values.

To plot one data set, specify one variable each for xvar,yvar, zvar, sizevar, and optionally cvar. For example, read Patients.xls into the table tbl. Plot the Height,Weight, and Diastolic variables, and vary the bubble sizes according to the Age variable.

tbl = readtable('Patients.xls'); bubblechart3(tbl,'Height','Weight','Diastolic','Age')

To plot multiple data sets together, specify multiple variables for at least one ofxvar, yvar, zvar,sizevar, or optionally cvar. If you specify multiple variables for more than one argument, the number of variables must be the same for each of those arguments.

For example, plot the Weight variable on the _x_-axis, theHeight variable on the _y_-axis, and theSystolic and Diastolic variables on the_z_-axis. Specify the Age variable for the bubble sizes.

bubblechart3(tbl,'Weight','Height',{'Systolic','Diastolic'},'Age')

You can also use different indexing schemes for the table variables. For example, specify xvar and xvar as variable names, zvar as an index number, and sizevar as a logical vector.

bubblechart3(tbl,'Height','Weight',9,[false false true])

Table variables containing the _z_-coordinates, specified as one or more table variable indices.

Specifying Table Indices

Use any of the following indexing schemes to specify the desired variable or variables.

Indexing Scheme Examples
Variable names: A string, character vector, or cell array.A pattern object. "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Variable type: A vartype subscript that selects variables of a specified type. vartype("categorical") — All the variables containing categorical values

Plotting Your Data

The table variables you specify can contain numeric, categorical, datetime, or duration values.

To plot one data set, specify one variable each for xvar,yvar, zvar, sizevar, and optionally cvar. For example, read Patients.xls into the table tbl. Plot the Height,Weight, and Diastolic variables, and vary the bubble sizes according to the Age variable.

tbl = readtable('Patients.xls'); bubblechart3(tbl,'Height','Weight','Diastolic','Age')

To plot multiple data sets together, specify multiple variables for at least one ofxvar, yvar, zvar,sizevar, or optionally cvar. If you specify multiple variables for more than one argument, the number of variables must be the same for each of those arguments.

For example, plot the Weight variable on the _x_-axis, theHeight variable on the _y_-axis, and theSystolic and Diastolic variables on the_z_-axis. Specify the Age variable for the bubble sizes.

bubblechart3(tbl,'Weight','Height',{'Systolic','Diastolic'},'Age')

You can also use different indexing schemes for the table variables. For example, specify xvar and xvar as variable names, zvar as an index number, and sizevar as a logical vector.

bubblechart3(tbl,'Height','Weight',9,[false false true])

Table variables containing the bubble size data, specified as one or more table variable indices.

Specifying Table Indices

Use any of the following indexing schemes to specify the desired variable or variables.

Indexing Scheme Examples
Variable names: A string, character vector, or cell array.A pattern object. "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Variable type: A vartype subscript that selects variables of a specified type. vartype("categorical") — All the variables containing categorical values

Plotting Your Data

The table variables you specify can contain any type of numeric values.

If you are plotting one data set, specify one variable forsizevar. For example, read Patients.xls into the table tbl. Plot the Height,Weight, and Diastolic variables, and vary the bubble sizes according to the Age variable.

tbl = readtable('Patients.xls'); bubblechart3(tbl,'Height','Weight','Diastolic','Age')

If you are plotting multiple data sets, you can specify multiple variables for at least one of xvar, yvar,zvar, sizevar, or optionallycvar. If you specify multiple variables for more than one argument, the number of variables must be the same for each of those arguments.

For example, plot the Weight variable on the_x_-axis, the Height variable on the_y_-axis, and the Age variable on the_z_-axis. Specify the Systolic andDiastolic variables for the bubble sizes. The resulting plot shows two sets of bubbles with the same coordinates, but different bubble sizes.

bubblechart3(tbl,'Weight','Height','Age',{'Systolic','Diastolic'})

Table variables containing the bubble color data, specified as one or more table variable indices.

Specifying Table Indices

Use any of the following indexing schemes to specify the desired variable or variables.

Indexing Scheme Examples
Variable names: A string, character vector, or cell array.A pattern object. "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Variable type: A vartype subscript that selects variables of a specified type. vartype("categorical") — All the variables containing categorical values

Plotting Your Data

The table variables you specify can contain values of any numeric type. Each variable can be:

If you are plotting one data set, specify one variable forcvar. For example, create a table with seven variables of random numbers. Plot the X1, Y, andZ variables. Vary the bubble sizes according to theSZ variable, and vary the colors according to theColor1 variable.

tbl = table(randn(50,1)-10,randn(50,1)+10,rand(50,1), ... rand(50,1),rand(50,1),rand(50,1),rand(50,1),... 'VariableNames',{'X1','X2','Y','Z','SZ','Color1','Color2'});

bubblechart3(tbl,'X1','Y','Z','SZ','Color1')

If you are plotting multiple data sets, you can specify multiple variables for at least one of xvar, yvar,zvar, sizevar, or cvar. If you specify multiple variables for more than one argument, the number of variables must be the same for each of those arguments.

For example, plot the X1 and X2 variables on the _x_-axis, the Y variable on the_y_-axis, and the Z variable on the_z_-axis. Vary the bubble sizes according to theSZ variable. Specify the Color1 andColor2 variables for the colors. The resulting plot shows two sets of bubbles with the same _y_-coordinates,_z_-coordinates, and bubble sizes, but different_x_-coordinates and colors.

bubblechart3(tbl,{'X1','X2'},'Y','Z','SZ',{'Color1','Color2'})

Target axes, specified as an Axes object. If you do not specify the axes, MATLAB® plots into the current axes, or it creates an Axes object if one does not exist.

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: bubblechart3([2 1 5],[4 10 9],[1 2 3],[1 2 3],'MarkerFaceColor','red') creates red bubbles.

Marker fill color, specified as 'flat', 'auto', an RGB triplet, a hexadecimal color code, a color name, or a short name. The 'flat' option uses the CData values. The 'auto' option uses the same color as the Color property for the axes.

For a custom color, specify an RGB triplet or a hexadecimal color code.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and the hexadecimal color codes.

Color Name Short Name RGB Triplet Hexadecimal Color Code Appearance
"red" "r" [1 0 0] "#FF0000" Sample of the color red
"green" "g" [0 1 0] "#00FF00" Sample of the color green
"blue" "b" [0 0 1] "#0000FF" Sample of the color blue
"cyan" "c" [0 1 1] "#00FFFF" Sample of the color cyan
"magenta" "m" [1 0 1] "#FF00FF" Sample of the color magenta
"yellow" "y" [1 1 0] "#FFFF00" Sample of the color yellow
"black" "k" [0 0 0] "#000000" Sample of the color black
"white" "w" [1 1 1] "#FFFFFF" Sample of the color white
"none" Not applicable Not applicable Not applicable No color

This table lists the default color palettes for plots in the light and dark themes.

Palette Palette Colors
"gem" — Light theme default_Before R2025a: Most plots use these colors by default._ Sample of the "gem" color palette
"glow" — Dark theme default Sample of the "glow" color palette

You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.

RGB = orderedcolors("gem"); H = rgb2hex(RGB);

Before R2023b: Get the RGB triplets using RGB = get(groot,"FactoryAxesColorOrder").

Before R2024a: Get the hexadecimal color codes using H = compose("#%02X%02X%02X",round(RGB*255)).

Example: [0.3 0.2 0.1]

Example: 'green'

Example: '#D2F9A7'

Marker edge transparency, specified as a scalar in the range [0,1] or 'flat'. A value of 1 is opaque and 0 is completely transparent. Values between 0 and 1 are semitransparent.

To set the edge transparency to a different value for each point in the plot, set theAlphaData property to a vector the same size as theXData property, and set theMarkerEdgeAlpha property to 'flat'.

Marker face transparency, specified as a scalar in the range [0,1] or 'flat'. A value of 1 is opaque and 0 is completely transparent. Values between 0 and 1 are partially transparent.

To set the marker face transparency to a different value for each point, set the AlphaData property to a vector the same size as the XData property, and set the MarkerFaceAlpha property to 'flat'.

Version History

Introduced in R2020b

expand all

When you pass a table and one or more variable names to the bubblechart3 function, the axis and legend labels now display any special characters that are included in the table variable names, such as underscores. Previously, special characters were interpreted as TeX or LaTeX characters.

For example, if you pass a table containing a variable named Sample_Number to the bubblechart3 function, the underscore appears in the axis and legend labels. In R2022a and earlier releases, the underscores are interpreted as subscripts.

Release Label for Table Variable "Sample_Number"
R2022b Label for the variable name "Sample_Number" in R2022b. The underscore is included in the label.
R2022a Label for the variable name "Sample_Number" in R2022a. The N character displays as a subscript.

To display axis and legend labels with TeX or LaTeX formatting, specify the labels manually. For example, after plotting, call the xlabel orlegend function with the desired label strings.

xlabel("Sample_Number") legend(["Sample_Number" "Another_Legend_Label"])

The bubblechart3 function now accepts combinations of vectors and matrices for the coordinates and size data. As a result, you can visualize multiple data sets at once rather than using the hold function between plotting commands.

Create plots by passing a table to the bubblechart3 function followed by the variables you want to plot. When you specify your data as a table, the axis labels and the legend (if present) are automatically labeled using the table variable names.