MATLAB Answers

Plotting different chart types in the same figure?

1 view (last 30 days)
Francisco Mendes
Francisco Mendes on 16 Nov 2015
Edited: Yazan on 13 Jul 2021
Hello Matlab community. I am a basic Matlab user and I am very interested about plotting in Matlab.
I would like to know if is it possible to plot different chart types in the same figure? And if yes, how to do it?
Here's an example: Let's suppose I would like to plot an Histogram to compare different data.
At the same time I have a specification limit for the data I am analysing and I would like to plot the limit as a continuous line in the same chart just to check if any histogram bars would pass it.
Thank you in advance.

Answers (3)

Jan on 16 Nov 2015
You can create either different axes objects directly using the axes('Position', [X, Y, W, H]]) command. Or the subplot() command does this also internally, but uses an easier syntax to determin the positions. Reading the documentation for the commands will help, most of all the examples there.

Star Strider
Star Strider on 16 Nov 2015
To plot two different plots on the same axes, use the hold function:
histogram( ... )
hold on
plot(xlim, [1 1]*limit_line_value,'-r')
hold off

Yazan on 13 Jul 2021
Edited: Yazan on 13 Jul 2021
There are several ways to obtain that. One of them is through creating panels, each with its own axes. You can plot on the axes separately. Remember, pannels are children of a figure (or a uitab), the axes are children of a panel, and the data are children of the axes. An example is provided below, which plots the histogram of a random vector and the data itself on different panels.
% figure: father of the panels
f = figure;
% panels: children of a figure and father to axes
p1 = uipanel(f, 'Position', [0 0 0.5 1], 'Title', 'First panel', 'TitlePosition', 'centertop');
p2 = uipanel(f, 'Position', [0.5 0 0.5 1], 'Title', 'Second Panel', 'TitlePosition', 'centertop');
x = randn(128, 1);
% create axes on each panel
ax1 = axes(p1);
% specify the axes on which to plot the histogram
histogram(ax1, x);
xlabel(ax1, 'Bins'); ylabel(ax1, 'Histogram');
ax2 = axes(p2);
% specify the axes on which to plot the data
plot(ax2, x);
xlabel(ax2, 'Index'); ylabel(ax2, 'Data');


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!