How to draw a plot like this in MATLAB instead of using SigmaPlot

11 visualizaciones (últimos 30 días)
Hello,
Is there a way of plotting a similar graph to the one shown below using MATLAB?
  1 comentario
Dyuman Joshi
Dyuman Joshi el 17 de Ag. de 2023
It is possible to plot like this, but that would require using a bunch of different functions -
Check out - plot, annotation, text
I am not sure about the grey patches. Are they boxplots?

Iniciar sesión para comentar.

Respuesta aceptada

akshatsood
akshatsood el 31 de Ag. de 2023
Editada: akshatsood el 31 de Ag. de 2023
Hi Nourhan,
I understand that you wish to draw a plot similar to the screenshot attached in the question. As per my understanding, a closely related figure could be achieved by using the boxplot function. Here is the code snippet to plot the figure using random data with some of the specifications resembling with the screenshot attached.
x = abs(100*randn(100,25)); % assuming a random data
bp = boxplot(x,'Color','k'); % create a boxplot for multiple groups
% extract upper and lower whiskers from the box plot and hide them
whiskers = findobj('-regexp','Tag','(Lower|Upper) (Whisker|Adjacent Value)');
set(whiskers,'LineStyle','none')
% select outliers and delete them
h = findobj(gca,'tag','Outliers');delete(h)
% get the handles of the individual boxes
h = findobj(gca, 'Tag', 'Box');
numBoxes = numel(h);
numToColor = 8; % number of boxes to color
% create a vector of random indices to color the boxes
indicesToColor = randperm(numBoxes, numToColor);
for i = 1:numel(h) % iterate through all boxes
x = mean(h(i).XData([1, 3])); % x-coordinate of the marker
yTop = max(h(i).YData); % y-coordinate of the top marker
yBottom = min(h(i).YData); % y-coordinate of the bottom marker
hold on;
plot(x, yTop, 'ks', 'MarkerSize', 5, 'MarkerFaceColor','black'); % set top marker
plot(x, yBottom, 'k^', 'MarkerSize', 5, 'MarkerFaceColor','black'); % set bottom marker
hold off;
end
% coloring random box plots
for i = 1:numToColor
patch(get(h(indicesToColor(i)),'XData'),get(h(indicesToColor(i)),'YData'), ...
[0.5 0.5 0.5], 'FaceAlpha',.5);
end
xlabel('Year');
ylabel('(T&A days)');
ylim([0 160]);
For adding annotations, you can leverage annotation function to add arrows at the required places.
The script attached above outlines a workflow which can be carried forward to achieve the plot with your data combined with required modifications from your end.
Have a look at the below references for better understanding
I hope this helps.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by