62 lines
3.0 KiB
Plaintext
62 lines
3.0 KiB
Plaintext
---
|
||
title: plot_histogram (v0.31)
|
||
description: API reference for qiskit.visualization.plot_histogram in qiskit v0.31
|
||
in_page_toc_min_heading_level: 1
|
||
python_api_type: function
|
||
python_api_name: qiskit.visualization.plot_histogram
|
||
---
|
||
|
||
<span id="qiskit-visualization-plot-histogram" />
|
||
|
||
# qiskit.visualization.plot\_histogram
|
||
|
||
<Function id="qiskit.visualization.plot_histogram" isDedicatedPage={true} github="https://github.com/qiskit/qiskit/tree/stable/0.18/qiskit/visualization/counts_visualization.py" signature="plot_histogram(data, figsize=(7, 5), color=None, number_to_keep=None, sort='asc', target_string=None, legend=None, bar_labels=True, title=None, ax=None)">
|
||
Plot a histogram of data.
|
||
|
||
**Parameters**
|
||
|
||
* **data** (*list or dict*) – This is either a list of dictionaries or a single dict containing the values to represent (ex \{‘001’: 130})
|
||
* **figsize** (*tuple*) – Figure size in inches.
|
||
* **color** (*list or str*) – String or list of strings for histogram bar colors.
|
||
* **number\_to\_keep** (*int*) – The number of terms to plot and rest is made into a single bar called ‘rest’.
|
||
* **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 maximum probability for each bitstring. Defaults to ‘asc’.
|
||
* **target\_string** (*str*) – Target string if ‘sort’ is a distance measure.
|
||
* **legend** (*list*) – A list of strings to use for labels of the data. The number of entries must match the length of data (if data is a list or 1 if it’s a dict)
|
||
* **bar\_labels** (*bool*) – Label each bar in histogram with probability value.
|
||
* **title** (*str*) – A string to use for the plot title
|
||
* **ax** (*matplotlib.axes.Axes*) – An optional Axes object to be used for the visualization output. If none is specified a new matplotlib Figure will be created and used. Additionally, if specified there will be no returned Figure since it is redundant.
|
||
|
||
**Returns**
|
||
|
||
A figure for the rendered histogram, if the `ax` kwarg is not set.
|
||
|
||
**Return type**
|
||
|
||
matplotlib.Figure
|
||
|
||
**Raises**
|
||
|
||
* [**MissingOptionalLibraryError**](qiskit.aqua.MissingOptionalLibraryError "qiskit.aqua.MissingOptionalLibraryError") – Matplotlib not available.
|
||
* [**VisualizationError**](qiskit.visualization.VisualizationError "qiskit.visualization.VisualizationError") – When legend is provided and the length doesn’t match the input data.
|
||
|
||
**Example**
|
||
|
||
```python
|
||
from qiskit import QuantumCircuit, BasicAer, execute
|
||
from qiskit.visualization import plot_histogram
|
||
%matplotlib inline
|
||
|
||
qc = QuantumCircuit(2, 2)
|
||
qc.h(0)
|
||
qc.cx(0, 1)
|
||
qc.measure([0, 1], [0, 1])
|
||
|
||
backend = BasicAer.get_backend('qasm_simulator')
|
||
job = execute(qc, backend)
|
||
plot_histogram(job.result().get_counts(), color='midnightblue', title="New Histogram")
|
||
```
|
||
|
||
![../\_images/qiskit.visualization.plot\_histogram\_0\_0.png](/images/api/qiskit/0.31/qiskit.visualization.plot_histogram_0_0.png)
|
||
</Function>
|
||
|