How to plot a data which 3 columns using bar3 function

8 vues (au cours des 30 derniers jours)
Kushagra Kirtiman
Kushagra Kirtiman le 21 Mar 2022
Modifié(e) : Adam Danz le 29 Mar 2022
I have three column data. I want to plot it in MATLAB using bar3 function. Is there a way to do it
  6 commentaires
Ive J
Ive J le 22 Mar 2022
Is this what you need?
data = readmatrix('sampledata.xlsx');
bar3(data)
Kushagra Kirtiman
Kushagra Kirtiman le 22 Mar 2022
thanks, but i want the values in the first and second column to be represented in the x and y axis and the third column in z axis

Connectez-vous pour commenter.

Réponses (1)

Adam Danz
Adam Danz le 22 Mar 2022
Modifié(e) : Adam Danz le 29 Mar 2022
bar() is not designed to set the x-location of bars. One workaround is to use histogram2 but histograms are designed for continuous data and therefore have contiguous bar edges rather than having gaps between bars like bar/bar3 have. It would also require a bit of prep work.
Another alternative that is shown below is to produce the bar chart using bar3 with all data in one row and then to adjust the bar positions and widths after plotting. bar3 produces surface objects that contain properties for XData, YData, and ZData which are moved to the specified coordinates. Consider setting colors to the bars or changing the axes rotation for better visibility.
% Load data (see readmatrix to follow this demo)
% [x, y, height]
data = [200 200 10.8
200 300 11.4
200 400 12.5
200 500 14.2
200 600 16
300 200 13
300 300 12
300 400 15
300 500 12
300 600 18.2
400 200 19.2
400 300 11.2
400 400 14
400 500 12
400 600 15
500 200 12
500 300 11.5
500 400 15
500 500 17
500 600 20];
% Plot a single row of 3D bars
clf
ax = axes();
h = bar3(ax, data(:,3));
% Move the bars to the specified (x,y) coordinates
xFactor = max(diff(unique(data(:,1))))/2; % /2 decreases bar width
yFactor = max(diff(unique(data(:,2))))/2; % /2 decreases bar width
xOffset = repelem(data(:,1),6,4);
yOffset = repelem(data(:,2),6,4);
faceIdx = repelem((1:size(data,1))',6,1);
yCentered = h.YData - faceIdx;
xCentered = h.XData - 1;
h.XData = xFactor * xCentered + xOffset;
h.YData = yFactor * yCentered + yOffset;
% Change some axis properties set by bar3
ax.XTickMode = 'auto';
xlim(ax, [min(h.XData(:)), max(h.XData(:))])
ylim(ax, [min(h.YData(:)), max(h.YData(:))])
ax.PlotBoxAspectRatio(1) = ax.PlotBoxAspectRatio(2);
xlabel(ax,'x')
ylabel(ax,'y')

Community Treasure Hunt

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

Start Hunting!

Translated by