Skip to content
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

fixing plot_histogram when data is sparse #13294

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions qiskit/visualization/counts_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def plot_histogram(
figsize=None,
color=None,
number_to_keep=None,
ignore_under=None,
sort="asc",
target_string=None,
legend=None,
Expand All @@ -82,6 +83,8 @@ def plot_histogram(
applies to each dataset individually, which may result in more bars than
``number_to_keep + 1``. The ``number_to_keep`` applies to the total values, rather than
the x-axis sort.
ignore_under (int): The count of values below which to ignore the bitstrings. It will return
only those bitstrings which have count greater or equal to ``ignore_under``.
sort (string): Could be `'asc'`, `'desc'`, `'hamming'`, `'value'`, or
`'value_desc'`. If set to `'value'` or `'value_desc'` the x axis
will be sorted by the number of counts for each bitstring.
Expand Down Expand Up @@ -137,6 +140,8 @@ def plot_histogram(
# one bitstring to the other) from a target string.
hist2 = plot_histogram(counts, sort='hamming', target_string='001')
"""
if ignore_under is not None:
data = {key: count for key, count in data.items() if count > (ignore_under - 1)}
if not isinstance(data, list):
data = [data]

Expand Down
14 changes: 14 additions & 0 deletions releasenotes/notes/ignore_under_parameter-e06eab711a713571.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
features_visualization:
- |
New parameter added to the function plot_histogram. Paramerer is named `ignore_under`.
The introduction of this parameter ignores the values in the data dictionary values
strictly lower than `ignore_under`. This will solve the sparsity problem faced by the
function when the data contains a lot of small values returning an unreadable histogram.


fixes:
- |
Fixes sparsity problem wit the histograms ``foo()``. Refer to
`#13066 <https://github.com/Qiskit/qiskit/issues/13066>` for more
details.