2
$\begingroup$

I would like to display two histograms next to each other, using something like matplotlib's subplot function. This doesn't seem to work because plot_histogram() closes the figure.

It is possible to show a figure that has been closed - see this post. I tried this approach but it doesn't work (see code below). Does anyone know how to do this?

figs = []
for i in range(num):
    counts = result.get_counts(circuits[i])
    figs.append(plot_histogram(counts, title='here', bar_labels=True))
    print(counts)
plt.subplot(121)
new_fig = plt.figure()
new_mngr = new_fig.canvas.manager
new_mngr.canvas.figure = figs[0]
fig.set_canvas(new_mngr.canvas)
plt.show(fig)

plt.subplot(122)
new_fig = plt.figure()
new_mngr = new_fig.canvas.manager
new_mngr.canvas.figure = figs[1]
fig.set_canvas(new_mngr.canvas)
plt.show(fig)
$\endgroup$
3
  • $\begingroup$ If stopping plot_histogram() from closing the figure will fix this issue, then there is a way to do just that. If you go into the qiskit code to qiskit/visualization/counts_visualization.py you will find the plot_histrogram() method. Near the end of this method, you will see the conditional statement if fig: plt.close(fig). If you comment that out, it should stop automatically closing the figure when you call plot_histogram() $\endgroup$ Commented Aug 15, 2019 at 20:22
  • $\begingroup$ I am reluctant to modify the qiskit code because there is some discussion about why qiskit chose to close the figure in this github issue $\endgroup$
    – John
    Commented Aug 16, 2019 at 21:38
  • $\begingroup$ John , it is true that there is discussion going on about why it originally is being closed. However, modifying the code in this way should not be detrimental. If any change is ever made to the way the figure is displayed, it would be pushed in a new version of qiskit. Then you would just need to update and it would take and use the new code. @glS I can move the comment to be an answer. Wasn't sure at first if it matched exactly what John was asking. $\endgroup$ Commented Aug 19, 2019 at 13:55

1 Answer 1

2
$\begingroup$

~Moved from comment to answer~

If stopping plot_histogram() from closing the figure will fix this issue, then there is a way to do just that. If you go into the qiskit code to qiskit/visualization/counts_visualization.py you will find the plot_histrogram() method. Near the end of this method, you will see the conditional statement

if fig: 
    plt.close(fig)

If you comment that out, it should stop automatically closing the figure when you call plot_histogram().

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.