-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrate optional plotting capabilities into ashist() #636
base: main
Are you sure you want to change the base?
Changes from all commits
1019793
fa48672
cd5fdd1
69f6f6b
5df4b75
b5261b2
2b77a7c
b31374e
65e5943
3cab05e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,8 +162,16 @@ def aspandas(self): | |
""" | ||
return pd.Series(self._val, name=self.name) | ||
|
||
def ashist(self, bins=10, bin_edges=False, density=False, log_binning=False): | ||
"""Return the distribution of a numpy array. | ||
def ashist( | ||
self, | ||
bins=10, | ||
bin_edges=False, | ||
density=False, | ||
log_binning=False, | ||
plot=False, | ||
plot_kwargs=None, | ||
): | ||
"""Return the distribution of a numpy array and optionally plot it. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -172,34 +180,71 @@ def ashist(self, bins=10, bin_edges=False, density=False, log_binning=False): | |
bins : int, list, or Numpy array | ||
The number of bins or the bin edges. | ||
bin_edges : bool | ||
Whether to also output the min and max of each bin, | ||
by default, False. | ||
Whether to also output the min and max of each bin. | ||
density : bool | ||
Whether to normalize the resulting distribution. | ||
Whether to normalize the distribution. | ||
log_binning : bool | ||
Whether to bin the values with log-sized bins. | ||
By default, False. | ||
|
||
plot : bool or str | ||
If True, plots histogram using matplotlib. | ||
Can also be 'bar', 'line', or 'step' to specify plot type. | ||
plot_kwargs : dict | ||
Additional keyword arguments for plotting function. | ||
|
||
Returns | ||
------- | ||
Pandas DataFrame | ||
A two-column table with "bin_center" and "value" columns, | ||
where "value" is a count or a probability. If `bin_edges` | ||
is True, outputs two additional columns, `bin_lo` and `bin_hi`, | ||
which outputs the left and right bin edges respectively. | ||
DataFrame with histogram data and optional plot. | ||
|
||
Notes | ||
----- | ||
Originally from https://github.com/jkbren/networks-and-dataviz | ||
Examples | ||
-------- | ||
>>> H.nodes.degree.ashist(plot='bar', plot_kwargs={'color': 'red'}) | ||
""" | ||
|
||
# if there is one unique value and more than one bin is specified, | ||
# sets the number of bins to 1. | ||
# Handle single value case | ||
if isinstance(bins, int) and len(set(self.aslist())) == 1: | ||
bins = 1 | ||
|
||
return hist(self.asnumpy(), bins, bin_edges, density, log_binning) | ||
# Get histogram data | ||
df = hist(self.asnumpy(), bins, bin_edges, density, log_binning) | ||
|
||
# Only execute plotting code if plot is True or a string | ||
if plot: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you break this out into a modular helper function with an underscore prefix like |
||
try: | ||
import matplotlib.pyplot as plt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If users have installed xgi, I believe that they will also have matplotlib by default, so not sure that you need to check this. |
||
except ImportError: | ||
raise ImportError("Matplotlib is required for plotting.") | ||
|
||
# Set default plotting parameters | ||
plot_kwargs = plot_kwargs or {} | ||
plot_type = "bar" if plot is True else plot | ||
|
||
# Create plot | ||
fig, ax = plt.subplots() | ||
|
||
# Plot based on type | ||
if plot_type == "bar": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a "scatter" option or replace the "line" option with a scatterplot only plotting the points? |
||
ax.bar(df["bin_center"], df["value"], **plot_kwargs) | ||
elif plot_type == "line": | ||
ax.plot(df["bin_center"], df["value"], **plot_kwargs) | ||
elif plot_type == "step": | ||
ax.step(df["bin_center"], df["value"], where="mid", **plot_kwargs) | ||
else: | ||
raise ValueError(f"Unknown plot type: {plot_type}") | ||
|
||
# Set labels | ||
ax.set_xlabel("Value") | ||
ax.set_ylabel("Count" if not density else "Probability") | ||
ax.set_title("Histogram") | ||
|
||
# Add bin edges if requested | ||
if bin_edges: | ||
for _, row in df.iterrows(): | ||
ax.axvline(row["bin_lo"], color="gray", linestyle="--", alpha=0.5) | ||
nwlandry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ax.axvline(row["bin_hi"], color="gray", linestyle="--", alpha=0.5) | ||
|
||
plt.show() | ||
|
||
return df | ||
|
||
def max(self): | ||
"""The maximum value of this stat.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define what
H
is. maybe a small example? like