# How to do subplots when plot_histogram etc. closes the figure

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)

• 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() – Matthew Stypulkoski Aug 15 at 20:22
• 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 – John Aug 16 at 21:38
• 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. – Matthew Stypulkoski Aug 19 at 13:55

## 1 Answer

~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().