How can I create a custom histogram with specific bin widths using Wolfram?

  • Context: MHB 
  • Thread starter Thread starter Jameson
  • Start date Start date
Click For Summary
SUMMARY

This discussion focuses on creating a custom histogram with specific bin widths using Wolfram Alpha and Mathematica. The user aims to graph data over defined ranges: $[(0,.5],(.5,1],(1,1.5],(1.5,2],(2,2.5],(2.5,3],(3,3.5])$. The correct syntax for generating the histogram in Mathematica is provided as Histogram[data, {.5}], but it is noted that this functionality is limited in the online version of Wolfram Alpha. Additionally, the user seeks to create a box plot, which requires the command BoxWhiskerChart instead of BoxWhiskerPlot.

PREREQUISITES
  • Familiarity with Wolfram Alpha and Mathematica syntax
  • Understanding of histogram and box plot concepts
  • Basic knowledge of data visualization techniques
  • Experience with LaTeX and PGFPlots for creating plots
NEXT STEPS
  • Research how to customize bin widths in Mathematica's Histogram function
  • Learn about the differences between BoxWhiskerChart and BoxWhiskerPlot in Mathematica
  • Explore creating histograms and box plots using Python libraries such as Matplotlib
  • Investigate PGFPlots documentation for advanced plotting in LaTeX
USEFUL FOR

Students, data analysts, and researchers looking to visualize data effectively using Wolfram Mathematica or LaTeX. This discussion is particularly beneficial for those needing to create custom histograms and box plots for academic purposes.

Jameson
Insights Author
Gold Member
MHB
Messages
4,533
Reaction score
13
I'm trying to create a simple histogram with custom bin widths. I want the data to be graphed over the ranges:

$[(0,.5],(.5,1],(1,1.5],(1.5,2],(2,2.5],(2.5,3],(3,3.5])$

Here is my data set:

{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}

If I enter this into Wolfram Alpha:

Code:
Histogram[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}]

I get a graph but I can't seem to customize the bin width. Any ideas?
 
Physics news on Phys.org
Re: Histogram on Wolfram

Jameson said:
I'm trying to create a simple histogram with custom bin widths. I want the data to be graphed over the ranges:

$[(0,.5],(.5,1],(1,1.5],(1.5,2],(2,2.5],(2.5,3],(3,3.5])$

Here is my data set:

{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}

If I enter this into Wolfram Alpha:

Code:
Histogram[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}]

I get a graph but I can't seem to customize the bin width. Any ideas?

The general syntax for Histogram is Histogram[data,bspec,hspec] (but hspec isn't needed here). So the bspec you want to specify is {.5} (since you seem to be splitting the data into intervals that are .5 units long); thus, the command

Code:
Histogram[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725},{.5}]

gives me the following graph in Mathematica:


Is this what you're after?
 
Re: Histogram on Wolfram

Yep that's it and exactly what I typed into Wolfram Alpha, but apparently it doesn't work on the online version. :(
 
Now I'm trying to do a boxplot (this is ridiculously simple I know - it's a class I have to take) and according to the online guide this code should work:

Code:
BoxWhiskerPlot[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}]

but alas, nothing. I found another site to make the box plot but I wish this could be done online at W|A.
 
Jameson said:
Now I'm trying to do a boxplot (this is ridiculously simple I know - it's a class I have to take) and according to the online guide this code should work:

Code:
BoxWhiskerPlot[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}]

but alas, nothing. I found another site to make the box plot but I wish this could be done online at W|A.

It should be BoxWhiskerChart. Plugging that code (after making that correction) into Mathematica gives me the following:


There was also some additional information you get when you hover over the output in Mathematica:

Max: 3.469
75%: 2.588
Median: 1.256
25%: 0.507
Min: 0.45
 
I just need to get a copy of Mathematica on my laptop. Wolfram Alpha doesn't have the full functionality Mathematica does and this code still doesn't work there. Thanks for the help!
 
Jameson said:
I just need to get a copy of Mathematica on my laptop. Wolfram Alpha doesn't have the full functionality Mathematica does and this code still doesn't work there. Thanks for the help!

Yea, no problem. Since you're a student, you can get Mathematica for about $140 (if you get the standard edition [which is the one I have]; its cheaper if you want to have it for a year or even a semester [see here for more info]). If you get the standard edition, it's yours to keep even after you're done with school.
 
Jameson said:
I just need to get a copy of Mathematica on my laptop. Wolfram Alpha doesn't have the full functionality Mathematica does and this code still doesn't work there. Thanks for the help!

Why pay $140 when Python is FREE. Also, every school I have been to gave me a site license. You should look into that before paying for it to. However, still go with Python.

If you are going to put this graphics in a \(\LaTeX\) document, you could always create it in PGFplots. Here is your code with 7 bins. You would have to look up how to specify bin width but that shouldn't be too hard.

PGFPlots documentation: CTAN: Package pgfplots



Latex Code:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.8}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xticklabel =
{$(\pgfmathprintnumber\tick, %
\pgfmathprintnumber\nexttick]$},
font = \scriptsize,
width = 15cm
]
\addplot+[hist = {bins = 7}]
table[row sep = \\, y index = 0]{
data\\
0.45\\ 0.45\\ 0.473\\ 0.507\\ 0.457\\ 0.452\\ 0.453\\ 1.215\\ 1.256\\
1.145\\ 1.085\\ 1.066\\ 1.111\\ 1.364\\ 1.254\\ 1.396\\ 1.575\\ 1.617\\
1.733\\ 2.753\\ 3.186\\ 3.227\\ 3.469\\ 1.911\\ 2.588\\ 2 .635\\ 2.725\\
};
\end{axis}
\end{tikzpicture}
\end{document}
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
13K
  • · Replies 0 ·
Replies
0
Views
6K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K
  • · Replies 152 ·
6
Replies
152
Views
11K