Skip to content

Commit 1eaaa06

Browse files
authored
adding histogram subsection for plotting chapter (#960)
1 parent 93d4a17 commit 1eaaa06

File tree

4 files changed

+158
-4
lines changed

4 files changed

+158
-4
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This is the size of each bin
2+
bin_width = 1;
3+
4+
# This takes the data and determins which bin id it should fall into
5+
bin_id(x) = floor(x/bin_width)
6+
7+
# This modifies each bin to be the correct width and also centers it over the
8+
# correct number
9+
bin(x) = bin_width * ( bin_id(x) + 0.5 )
10+
11+
# Starts the y-axis at 0
12+
set yrange [0:]
13+
14+
# Removes legend
15+
unset key
16+
17+
# Sets a fill style for "fillsteps"
18+
set style fill solid 1.00 border
19+
20+
# The column number to be histogrammed is 1, change $1 to another number if
21+
# you want to plot another column
22+
plot '../../data/rand.dat' u (bin($1)):(1) t 'data' smooth frequency w fillsteps

contents/plotting/data/rand.dat

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
9
2+
6
3+
8
4+
8
5+
3
6+
7
7+
1
8+
4
9+
2
10+
9
11+
6
12+
1
13+
5
14+
1
15+
7
16+
3
17+
7
18+
4
19+
6
20+
5
21+
6
22+
1
23+
4
24+
6
25+
4
26+
8
27+
9
28+
3
29+
9
30+
9
31+
8
32+
3
33+
6
34+
9
35+
2
36+
1
37+
2
38+
1
39+
3
40+
6
41+
3
42+
1
43+
4
44+
1
45+
2
46+
9
47+
1
48+
4
49+
8
50+
2
51+
5
52+
5
53+
5
54+
1
55+
3
56+
2
57+
6
58+
3
59+
1
60+
6
61+
8
62+
5
63+
2
64+
1
65+
7
66+
8
67+
6
68+
1
69+
9
70+
2
71+
2
72+
4
73+
9
74+
5
75+
3
76+
1
77+
8
78+
7
79+
7
80+
8
81+
7
82+
3
83+
8
84+
1
85+
7
86+
5
87+
5
88+
5
89+
8
90+
6
91+
8
92+
9
93+
9
94+
1
95+
3
96+
8
97+
2
98+
6
99+
7
100+
3

contents/plotting/plotting.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Plotting
22

33
Plotting is an essential tool for visualizing and understanding important details of several algorithms and methods and is necessary for studies in various areas of computational science.
4-
For many languages, such as python, julia, and matlab, it is relatively straightforward to create simple plots for various types of data; however, for several other languages, like fortran, C/C++, and java, plotting can be a chore.
4+
For many languages, such as python, julia, and matlab, it is relatively straightforward to create simple plots for various types of data; however, for several other languages, like Fortran, C/C++, and java, plotting can be a chore.
55
Because the Algorithm Archive strives to be language agnostic, we do not want to favor any particular set of languages and have decided instead to output all data that needs plotting into a file format that can easily be read in by various plotting scripts separate from the algorithm implementations.
66

77
If you are implementing any algorithm in a language found on this page, you should be able to modify your existing code to allow for on-the-fly plotting.
@@ -485,7 +485,7 @@ In the case of two-dimensional image output, the data file be similar, but this
485485

486486
[import](data/2d_sample_low_res.dat)
487487

488-
It is expected that the number of columns does not vary in each row and that we are working with an $$n \times m$$ matrix which can be simply plotted as a series of pixels that scale in color according to some defined colorbar.
488+
It is expected that the number of columns does not vary in each row and that we are working with an $$n \times m$$ matrix which can be simply plotted as a series of pixels that scale in color according to some defined color bar.
489489

490490
{% method %}
491491
{% sample lang="gnuplot" %}
@@ -503,7 +503,7 @@ splot "sample_data.dat" matrix with image
503503

504504
{% endmethod %}
505505

506-
#### changing the colorbar
506+
#### changing the color bar
507507

508508
For plotting images from data files, we will often need to specify how we color the image by setting a custom color bar
509509

@@ -537,7 +537,7 @@ For the purposes of the Algorithm Archive, this space is mainly two-dimensional;
537537
We will update this section if three-dimensional scatter plots are required.
538538

539539
For the purposes of the Algorithm Archive, scatter plot data will be output as a series of $$x$$ and $$y$$ pairs, where each row has an $$x$$ and a $$y$$ value, separated by a tab character.
540-
For example, a datafile might look like this:
540+
For example, a data file might look like this:
541541

542542
[import:1-10](data/scatterplot_data.dat)
543543

@@ -561,6 +561,37 @@ Here, we have chosen `pointtype 7`, simply because it is easier to see when comp
561561

562562
{% endmethod %}
563563

564+
# Histograms
565+
566+
Many different algorithms will output data as a series of points that must be organized into separate bins before anyone can make sense of the data.
567+
For example, here are 10 values from a set of 100 randomly generated integers between 1 and 9:
568+
569+
[import:50-60](data/rand.dat)
570+
571+
Someone might ask, "How many 1s show up in this string of numbers?"
572+
Similarly, someone might want to know how many 1s we have *in comparison* to the number of 2s (or 3s or 4s, etc).
573+
To do this, we would create a set of bins and then iterate through the data, adding one to a bin every time we find a corresponding number.
574+
Note that the bins do not necessarily need to be sequential integer values and that for floating point numbers, the input might need to be rounded.
575+
You can even histograms objects or anything that else that can be categorized.
576+
577+
For the data that we have shown above, we might create a histogram that looks like this:
578+
579+
<p>
580+
<img class="center" src="res/gnuplot/histogram.png" style="width:70%" />
581+
</p>
582+
583+
And here is a plotting script to generate it:
584+
585+
{% method %}
586+
[import](code/gnuplot/histogram.gp)
587+
{% sample lang="gnuplot" %}
588+
589+
For this, we are using a fill style to use with `fillsteps` so the histogram is colored, but if you just want a line, you could remove the fill style and use `histeps` instead.
590+
As another note, we are using `t 'data' smooth frequency`, which essentially turns the input numbers into a small, binned array to plot.
591+
{% endmethod %}
592+
593+
Note that this code rounds the input in the case of floating point numbers.
594+
564595
If you are interested in seeing this type of plot generate fractal patterns, please look at the chapter on [iterated function systems](../IFS/IFS.md).
565596

566597
## Conclusions
@@ -607,6 +638,7 @@ The text of this chapter was written by [James Schloss](https://github.com/leios
607638
- The image "[gnuplot_2d_sample](res/gnuplot/2d_sample.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
608639
- The image "[gnuplot_2d_sample_colorbar](res/gnuplot/2d_sample_cb.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
609640
- The image "[gnuplot_scatterplot](res/gnuplot/scatterplot.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
641+
- The image "[gnuplot_histogram](res/gnuplot/histogram.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
610642

611643
{% endmethod %}
612644

8.64 KB
Loading

0 commit comments

Comments
 (0)