I have
using Plots
classIntervals = range(start=1, step=2, stop=20)
frequencyAbsolute::AbstractVector = [1; 1; 11; 21; 25; 17; 9; 4; 1]
frequencyAbsoluteCumulative = sum(frequencyAbsolute)
frequencyRelative::AbstractVector = (frequencyAbsolute ./ frequencyAbsoluteCumulative)
histogram(x=frequencyRelative, bins=classIntervals, label="Experimental", color=:gray, normalize=:pdf)
xlims!(1, 19)
ylims!(0, 30)
xlabel!("x")
plotHistogram = ylabel!("P(x)")
savefig(plotHistogram, "out/plot-histogram.svg")
what is wrong with it that the x bars won't show up? i adapted example from https://docs.juliaplots.org/latest/series_types/histogram/
Not an expert on Plots, but it seems to me that the histogram
function expects as x
the actual values of the samples, not the binned counts (it will do that automatically, I guess). Since you're giving it a vector that is less than 1, it won't show up in bins that start at 1.
By the way, I believe annotating the vector as AbstractVector
does not do anything beyond erroring out if the right hand side of the assignment is not a subtype of AbstractVector
@Daniel González thanks for the reply!
expects as x the actual values of the samples, not the binned counts (it will do that automatically, I guess).
do you mean the data set variable values, instead of any pre-processed frequency, right?
so i tried this
using Plots
x::AbstractVector = [
02.97; 04.00; 05.20; 05.56; 05.94; 05.98; 06.35; 06.62; 06.72; 06.78;
06.80; 06.85; 06.94; 07.15; 07.16; 07.23; 07.29; 07.62; 07.62; 07.69;
07.73; 07.87; 07.93; 08.00; 08.26; 08.29; 08.37; 08.47; 08.54; 08.58;
08.61; 08.67; 08.69; 08.81; 09.07; 09.27; 09.37; 09.43; 09.52; 09.58;
09.60; 09.76; 09.82; 09.83; 09.83; 09.84; 09.96; 10.04; 10.21; 10.28;
10.28; 10.30; 10.35; 10.36; 10.40; 10.49; 10.50; 10.64; 10.95; 11.09;
11.12; 11.21; 11.29; 11.43; 11.62; 11.70; 11.70; 12.16; 12.19; 12.28;
12.31; 12.62; 12.69; 12.71; 12.91; 12.92; 13.11; 13.38; 13.42; 13.43;
13.47; 13.60; 13.96; 14.24; 14.35; 15.12; 15.24; 16.06; 16.90; 18.26;
]
classIntervals = range(start=1, step=2, length=19)
histogram(x=x, bins=classIntervals, label="Experimental", color=:gray, normalize=:pdf)
xlims!(1, 19)
ylims!(0, 30)
xlabel!("x")
plotHistogram = ylabel!("P(x)")
savefig(plotHistogram, "out/plot-histogram.svg")
but i have same issue
Since you're giving it a vector that is less than 1
what do you mean by that?
That works for me, but ylims!
squeezes the plot (since the maximum frequency is <0.15). Regarding my previous comment, I meant that all the entries of the relative frequency vector are smaller than 1.0; therefore, since they are interpreted as samples, do not enter into any bin (the first bin is from 1 to 3 in your example)
Oh, I think for some reason it works if you do
histogram(x, bins=classIntervals, label="Experimental", color=:gray, normalize=:pdf)
but not if you do
histogram(x=x, bins=classIntervals, label="Experimental", color=:gray, normalize=:pdf)
Looking at the examples and documentation, it seems that histogram
takes the data as the first positional argument, not as a keyword argument x
.
@Daniel González
Following your suggestions, I
using Plots
# Define data
x::AbstractVector = [
02.97; 04.00; 05.20; 05.56; 05.94; 05.98; 06.35; 06.62; 06.72; 06.78;
06.80; 06.85; 06.94; 07.15; 07.16; 07.23; 07.29; 07.62; 07.62; 07.69;
07.73; 07.87; 07.93; 08.00; 08.26; 08.29; 08.37; 08.47; 08.54; 08.58;
08.61; 08.67; 08.69; 08.81; 09.07; 09.27; 09.37; 09.43; 09.52; 09.58;
09.60; 09.76; 09.82; 09.83; 09.83; 09.84; 09.96; 10.04; 10.21; 10.28;
10.28; 10.30; 10.35; 10.36; 10.40; 10.49; 10.50; 10.64; 10.95; 11.09;
11.12; 11.21; 11.29; 11.43; 11.62; 11.70; 11.70; 12.16; 12.19; 12.28;
12.31; 12.62; 12.69; 12.71; 12.91; 12.92; 13.11; 13.38; 13.42; 13.43;
13.47; 13.60; 13.96; 14.24; 14.35; 15.12; 15.24; 16.06; 16.90; 18.26;
]
classIntervals = range(start=1, step=2, stop=19)
# Set and export plot
plotHistogram = histogram(x, bins=classIntervals, label="Experimental", color=:cool)
xlims!(0, 19)
ylims!(0, 30)
title!("energy consuption")
xlabel!("x")
ylabel!("Percent(x)")
savefig(plotHistogram, "out/plot-histogram.svg")
it works now. Thank you very much!
The only issue left is the exported image height and width which seems to cut the xlabel and ylabel. lookin at savefig help, it does not show any config for that.
Yuu Yin has marked this topic as resolved.
Weird; when I run the code, the SVG has the labels as well!
As a side note, you don´t need any of those ::AbstractVector
annotations , and usually in Julia we separate values with commas (although what you did is not incorrect, just not common).
Last updated: Nov 22 2024 at 04:41 UTC