-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conform iplot_histogram and plot_histogram #866
Changes from 1 commit
39ca802
05ae571
7c6f523
1018dc9
4a1ddb6
9f2e2fa
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 |
---|---|---|
|
@@ -17,35 +17,68 @@ | |
import matplotlib.pyplot as plt | ||
|
||
|
||
def plot_histogram(data, number_to_keep=False): | ||
def plot_histogram(data, number_to_keep=False, legend=None, options=None): | ||
"""Plot a histogram of data. | ||
|
||
data is a dictionary of {'000': 5, '010': 113, ...} | ||
number_to_keep is the number of terms to plot and rest is made into a | ||
single bar called other values | ||
Args: | ||
data (list or dict): This is either a list of dictionaries containing: | ||
values to represent (ex {'001': 130}) | ||
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 complete this sentence: "either ... or"? 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. Done |
||
number_to_keep (int): DEPRECATED the number of terms to plot and rest | ||
is made into a single bar called other values | ||
legend(list): A list of strings to use for labels of the data. | ||
The number of entries must match the lenght of data (if data is a | ||
list or 1 if it's a dict) | ||
options (dict): Representation settings containing | ||
- number_to_keep (integer): groups max values | ||
- show_legend (bool): show legend of graph content | ||
Raises: | ||
Exception: When legend is provided and the length doesn't match the | ||
input data | ||
""" | ||
if number_to_keep is not False: | ||
data_temp = dict(Counter(data).most_common(number_to_keep)) | ||
data_temp["rest"] = sum(data.values()) - sum(data_temp.values()) | ||
data = data_temp | ||
|
||
labels = sorted(data) | ||
values = np.array([data[key] for key in labels], dtype=float) | ||
pvalues = values / sum(values) | ||
numelem = len(values) | ||
ind = np.arange(numelem) # the x locations for the groups | ||
width = 0.35 # the width of the bars | ||
if options is None: | ||
options = {} | ||
|
||
if isinstance(data, dict): | ||
data = [data] | ||
if legend and len(legend) != 1: | ||
raise Exception("Length of legendL %s doesn't match number of " | ||
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. We try to use functionality-specific errors, subclassing 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. Done |
||
"input executions: %s" % (len(legend), 1)) | ||
else: | ||
if legend and len(legend) != len(data): | ||
raise Exception("Length of legendL %s doesn't match number of " | ||
"input executions: %s" % (len(legend), len(data))) | ||
|
||
_, ax = plt.subplots() | ||
rects = ax.bar(ind, pvalues, width, color='seagreen') | ||
# add some text for labels, title, and axes ticks | ||
ax.set_ylabel('Probabilities', fontsize=12) | ||
ax.set_xticks(ind) | ||
ax.set_xticklabels(labels, fontsize=12, rotation=70) | ||
ax.set_ylim([0., min([1.2, max([1.2 * val for val in pvalues])])]) | ||
# attach some text labels | ||
for rect in rects: | ||
height = rect.get_height() | ||
ax.text(rect.get_x() + rect.get_width() / 2., 1.05 * height, | ||
'%f' % float(height), | ||
ha='center', va='bottom') | ||
for item, execution in enumerate(data): | ||
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. Non-blocking: do you think is it feasible or even useful to allow customization of graph 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. It should be, and I thought about that when I was refactoring things. But I didn't want to change things too much here. After this merges I'll work on updating this to add the missing options. |
||
if number_to_keep is not False or ( | ||
'number_to_keep' in options and options['number_to_keep']): | ||
data_temp = dict(Counter(execution).most_common(number_to_keep)) | ||
data_temp["rest"] = sum(execution.values()) - sum(data_temp.values()) | ||
execution = data_temp | ||
|
||
labels = sorted(execution) | ||
values = np.array([execution[key] for key in labels], dtype=float) | ||
pvalues = values / sum(values) | ||
numelem = len(values) | ||
ind = np.arange(numelem) # the x locations for the groups | ||
width = 0.35 # the width of the bars | ||
label = None | ||
if legend: | ||
label = legend[item] | ||
adj = width * item | ||
rects = ax.bar(ind+adj, pvalues, width, label=label) | ||
# add some text for labels, title, and axes ticks | ||
ax.set_ylabel('Probabilities', fontsize=12) | ||
ax.set_xticks(ind) | ||
ax.set_xticklabels(labels, fontsize=12, rotation=70) | ||
ax.set_ylim([0., min([1.2, max([1.2 * val for val in pvalues])])]) | ||
# attach some text labels | ||
for rect in rects: | ||
height = rect.get_height() | ||
ax.text(rect.get_x() + rect.get_width() / 2., 1.05 * height, | ||
'%f' % float(height), | ||
ha='center', va='bottom') | ||
if legend and ( | ||
'show_legend' not in options or options['show_legend'] is True): | ||
plt.legend() | ||
plt.show() |
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.
Can we emit deprecation warnings for
number_to_keep
? Change the signature of the function to:And check if
number_to_keep
has been passed. If so, emit a deprecation warning: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.
I don't want to change the signature here, because the order of named kwargs actually matters. For example someone could be using this like:
plot_histogram(data_input, 5)
and that would work fine but if I move numbers_to_keep those users will be broken. But, I'll add the deprecation warning