How to plot a histogram as a curve?

324 次查看(过去 30 天)
I have a histogram that I want to plot just as a simple line plot. I have tried:
h = histogram(speed,'Normalization','probability')
plot(h)
But then I got an error message: Not enough input arguments.
Does anyone know how I can do it? Many thanks!

采纳的回答

Xun Jing Toh
Xun Jing Toh 2019-3-14
[N,edges] = histcounts(X, 'Normalization','pdf');
edges = edges(2:end) - (edges(2)-edges(1))/2;
plot(edges, N);
Personally I just do a simple calculation from histcounts

更多回答(3 个)

Steven Lord
Steven Lord 2018-10-30
The histogram function itself creates a graphics object. The output argument from that call is a handle to that graphics object, not data that you can pass into plot.
If you want it to be a line (just the top of the bars) set the DisplayStyle property of the histogram object to 'stairs'.
h = histogram(speed,'Normalization','probability', 'DisplayStyle', 'stairs');
If you just want to connect the midpoints of the top edges of the bars, there is a way to use the data in the histogram (or returned from the histcounts function) to generate the data to plot. Average adjacent elements in the bin edges vector to identify the bin centers, then plot using those centers with the counts for the corresponding bin.
  2 个评论
Trey Roy
Trey Roy 2020-10-28
Hi,
I am facing a similar issue in switching from hist to histogram to try and upgrade my code. The original code I used for our graph was in the following format:
axes(h_axes(2,2));
set(gca,'FontSize',14)
sessionSummary.ipsiMT = logData.MT(completeTargetIpsi);
sessionSummary.contraMT = logData.MT(completeTargetContra);
sessionSummary.allMT = logData.MT(sessionSummary.complete);
ipsiHist = hist(sessionSummary.ipsiMT, MTbins);
contraHist = hist(sessionSummary.contraMT, MTbins);
allHist = hist(sessionSummary.allMT, MTbins);
plot(MTbins, ipsiHist, 'color', 'm');
hold on
plot(MTbins, contraHist, 'color', 'b');
plot(MTbins, allHist, 'color', 'k');
title('completed MT','fontsize',fontFigure);
h_leg = legend('Ipsi','Contra','All','Location','NorthEast');
set(h_leg, 'fontsize',10);
This chunk of code was effective at creating a histogram in which the center of the bins were plotted as a curve and the actual histogram was not included in the graph. I am attempting to replicate this exact graph using the histogram function instead, but am having trouble. I replaced the middle chunk of code with:
ipsiHist = histogram(sessionSummary.ipsiRT, RTbins, 'DisplayStyle', 'Stairs', 'EdgeColor', 'm');
contraHist = histogram(sessionSummary.contraRT, RTbins, 'DisplayStyle', 'Stairs', 'EdgeColor', 'b');
allHist = histogram(sessionSummary.allRT, RTbins, 'DisplayStyle', 'Stairs', 'EdgeColor', 'k');
This resulted in a graph that traced the top edges of the histogram in a way that appeared like stairs but did not give the curve result I am aiming for. I also attempted to use the 'normalization' and 'probability' functions, however, when I did this it was not generating any graph. Please let me know if you have any ideas or recommendations for achieving my desired result while using the histogram function.
Steven Lord
Steven Lord 2020-10-28
You mean something like this? This sample code will create three figures: one with the histogram, one with the curve, and one with both.
% Sample data
x = randn(1, 1e4);
% Plot the histogram alone
figure
h = histogram(x, 'Normalization', 'probability');
% Plot the curve alone
%
% I could have retrieved values and edges from h (they are stored in the
% Values and BinEdges properties respectively) but I wanted to show how
% to get this information without actually creating a plot
[values, edges] = histcounts(x, 'Normalization', 'probability');
centers = (edges(1:end-1)+edges(2:end))/2;
figure
plot(centers, values, 'k-')
% Plot both, line superimposed on the histogram
figure
h = histogram(x, 'Normalization', 'probability');
hold on
plot(centers, values, 'k-')

请先登录,再进行评论。


KSSV
KSSV 2018-10-26
data = randn(1,1000); %// example data
num_bars = 15; %// specify number of bars
[n, x] = hist(data,num_bars); %// use two-output version of hist to get values
n_normalized = n/numel(data)/(x(2)-x(1)); %// normalize to unit area
bar(x, n_normalized, 1); %// plot histogram (with unit-width bars)
hold on
plot(x, n_normalized, 'r'); %// plot line, in red (or change color)

TANMOY SINGHA
TANMOY SINGHA 2020-5-28
data = randn(1,1000); %// example data
num_bars = 15; %// specify number of bars
[n, x] = hist(data,num_bars); %// use two-output version of hist to get values
n_normalized = n/numel(data)/(x(2)-x(1)); %// normalize to unit area
bar(x, n_normalized, 1); %// plot histogram (with unit-width bars)
hold on
plot(x, n_normalized, 'r'); %// plot line, in red (or change color)
  1 个评论
Champago
Champago 2020-8-6
with histfit? with transparency set to 0 and edgecolor to white you will get only the line
figure(1);clf;
h=histfit(data);
h(1).EdgeColor=[1,1,1];
alpha(h,0);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Histograms 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by