-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from cyrraz/example-category-axis
Add example showing how to plot categorical data
- Loading branch information
Showing
5 changed files
with
1,691 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
1d integer category | ||
=================== | ||
Plot a 1d histogram with integer categories. | ||
""" | ||
|
||
### | ||
import boost_histogram as bh | ||
from plothist import plot_hist | ||
import matplotlib.pyplot as plt | ||
|
||
# 3 integer categories | ||
int_categories = [-10, 5, 72] | ||
|
||
# Integer category axis with 3 bins | ||
axis = bh.axis.IntCategory(categories=int_categories) | ||
|
||
# 6 data points, | ||
data = [-10, -10, 5, 72, 72, 72] | ||
|
||
# Create and fill the histogram | ||
h = bh.Histogram(axis, storage=bh.storage.Weight()) | ||
h.fill(data) | ||
|
||
# Plot the histogram | ||
fig, ax = plt.subplots() | ||
|
||
plot_hist(h, ax=ax) | ||
|
||
# Set the x-ticks to the middle of the bins and label them | ||
ax.set_xticks([i + 0.5 for i in range(len(int_categories))]) | ||
ax.set_xticklabels(int_categories) | ||
ax.minorticks_off() | ||
|
||
ax.set_xlabel("Integer Category") | ||
ax.set_ylabel("Entries") | ||
ax.set_xlim(0, len(int_categories)) | ||
|
||
fig.savefig("1d_int_category.svg", bbox_inches="tight") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
1d string category | ||
================== | ||
Plot a 1d histogram with string categories. | ||
""" | ||
|
||
### | ||
import boost_histogram as bh | ||
from plothist import plot_hist | ||
import matplotlib.pyplot as plt | ||
|
||
# 3 str categories | ||
str_categories = ["a", "b", "c"] | ||
|
||
# String category axis with 3 bins | ||
axis = bh.axis.StrCategory(categories=str_categories) | ||
|
||
# 6 data points, | ||
data = ["a", "a", "a", "b", "b", "c"] | ||
|
||
# Create and fill the histogram | ||
h = bh.Histogram(axis, storage=bh.storage.Weight()) | ||
h.fill(data) | ||
|
||
# Plot the histogram | ||
fig, ax = plt.subplots() | ||
|
||
plot_hist(h, ax=ax) | ||
|
||
# Set the x-ticks to the middle of the bins and label them | ||
ax.set_xticks([i + 0.5 for i in range(len(str_categories))]) | ||
ax.set_xticklabels(str_categories) | ||
ax.minorticks_off() | ||
|
||
ax.set_xlabel("String Category") | ||
ax.set_ylabel("Entries") | ||
ax.set_xlim(0, len(str_categories)) | ||
|
||
fig.savefig("1d_str_category.svg", bbox_inches="tight") |
Oops, something went wrong.