Main Content

Create Plot

Interactively create and explore visualizations in the Live Editor

Description

The Create Plot Live Editor task lets you interactively create and explore visualizations for your data. Choose visualizations from a list of categories or let the task recommend them based on your workspace variables. The task automatically generates MATLAB® code and adds labels to the generated visualization.

Using this task, you can:

  • Create a plot by selecting variables and a chart type.

  • Explore different visualizations of the same data. Select some variables first, then try selecting different chart types.

  • Explore different data in the same visualization format. Select the chart type first. The task will prompt you for data.

  • Combine multiple plots. Overlay or combine multiple visualizations by clicking on the ‘Add’ tab.

  • Customize the appearance of your plot. For example, you can change the marker symbol for line charts, and the arrangement of bars for bar charts.

For a list all the types of plots and the functions to generate them programmatically, see Types of MATLAB Plots.

Open the Task

To add the Create Plot task to a live script in the MATLAB Editor you can follow any of these workflows:

  • In the Code section of the Live Editor tab, select Task > Create Plot.

    Image shows the mouse cursor hovering over the Create Plot Live Task option in the Tasks menu

  • In a code block in the live script, type a relevant keyword, such as viz, visualize, create, or hold. Select Create Plot from the suggested command completions.

    Image shows the mouse cursor hovering over the Create Plot Live Task option in the Tab Completion menu for the keyword "create"

  • In a code block in the live script, type the name of a charting function, such as plot, bar, or contour. Select Create Plot from the suggested command completions. When you add the task using this method, then MATLAB automatically selects the corresponding chart type in the Select visualization section of the task.

    Image shows the mouse cursor hovering over the Create Plot Live Task option in the Tab Completion menu for the keyword "plot"

Examples

expand all

This example shows how to use the Create Plot task to create a scatter plot and change the marker symbols.

First, load the patients file, which contains variables such as Age and Diastolic.

load patients

Open the Create Plot task. To open the task, either select Create Plot from the Tasks tab or type the keyword visualize in a code block and click Create Plot when it appears in the menu.

Use the task to create the chart. When the task opens:

  1. Select scatter from the Select visualization section.

  2. Select Age as the X variable and Diastolic as the Y variable in the Select data section. The value of the Size and Color variables is default, which indicates that you do not need to specify a value.

Image shows the interface of the Create Plot task being used to generate a scatter plot of Age vs. Diastolic.

Next, use the task to customize the visualization. For example, to display the scatter plot using '+' symbols instead of circles:

  1. Open the Select optional visualization parameters section.

  2. Select Marker symbol from the drop-down menu.

  3. Select + from the drop-down menu that appears to the right. Note that the scatter plot shows updated symbols.

Image shows the interface of the Create Plot task being used to generate a scatter plot of Age vs. Diastolic. The Select optional visualization parameters menu has been used to change the marker symbol to a plus symbol.

To see the code that this task generates, click the small arrow under the Select optional chart parameters section.

Image shows the interface of the Create Plot task being used to generate a scatter plot of Age vs. Diastolic. The task has generated code to recreate the plot.

In this example, the generated code is a call to the scatter, xlabel, ylabel, title, and legend functions.

Image shows the interface of the Create Plot task being used to generate a scatter plot of Age vs. Diastolic. The task has generated code to recreate the plot.

This example shows how to use the Create Plot task to overlay multiple plots in a single visualization.

First, load the patients file, which contains variables such as Age and Diastolic.

load patients

Open the Create Plot task. To open the task, either select Create Plot from the Tasks tab or type the keyword visualize in a code block and click Create Plot when it appears in the menu.

Use the task to create the chart. When the task opens:

  1. Select scatter from the Select visualization section.

  2. Select Age as the X variable and Diastolic as the Y variable in the Select data section. The value of the Size and Color variables is default, which indicates that you do not need to specify a value.

    Image shows the interface of the Create Plot task being used to generate a scatter plot of Age vs. Diastolic.

  3. Overlay another plot by clicking the Add tab.

    Image shows the cursor hovering over the Add tab and displaying a tooltip which reads, "Overlay plots using hold".

When the new panel opens:

  1. scatter is selected by default in the Select visualization section.

  2. Age is selected by default for the X variable. Select Systolic as the Y variable in the Select data section. The value of the Size and Color variables is default, which indicates that you do not need to specify a value.

Image shows the interface of the Create Plot task being used to generate a scatter plot of Age vs. Diastolic in blue and Age vs. Systolic in orange.

To see the code that this task generates, click the small arrow under the Select optional chart parameters section.

Image shows the interface of the Create Plot task being used to generate a scatter plot of Age vs. Diastolic. The task has generated code to recreate the plot.

In this example, the generated code is two calls to the scatter function. The plots are combined using the hold function.

This image shows code generated by the live task.

Some plotting functions, like surf, accept more than one input configuration. Using the Create Plot task change the configuration to fit the data.

First, generate the data to be plotted using the peaks function.

ZVar = peaks;

Open the Create Plot task. To open the task, either select Create Plot from the Tasks tab or type the keyword visualize in a code block and click Create Plot when it appears in the menu.

Select the surf visualization. This visualization function accepts inputs in multiple formats. The data is contained within a single array, ZVar. Use the Configuration drop-down menu to select Specify Z and Color. In this configuration, surf only requires a single data input, Z.

Image shows the interface of the Create Plot task interface with surf selected. The cursor is hovering over the open configuration drop-down menu over the selection "Specify Z and Color".

Assign the variable ZVar to Z.

Image shows the interface of the Create Plot task with a surf plot of the peaks data set.

Related Examples

Tips

  • To interactively customize the text of titles, labels, and legends, either use the options in the Figure tab or click on the title or label to edit it.

Introduced in R2021a