From b27f33cc667a157a8f0a6a01f99f5215d1a14404 Mon Sep 17 00:00:00 2001 From: Naoki Kanazawa Date: Tue, 19 Sep 2023 22:32:34 +0900 Subject: [PATCH] Replace deprecated mpl function in visualization test (#1274) ### Summary All PRs in Qiskit Experiments are currently failing in tests due to `TestPlotterAndMplDrawer.test_series_names_different_types` which includes deprecated functions in the recently updated matplotlib package. ### Details and comments No release note is included because this is update for unittest. --- test/visualization/test_plotter_mpldrawer.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test/visualization/test_plotter_mpldrawer.py b/test/visualization/test_plotter_mpldrawer.py index b39f9fcd4d..fc5799535a 100644 --- a/test/visualization/test_plotter_mpldrawer.py +++ b/test/visualization/test_plotter_mpldrawer.py @@ -118,9 +118,10 @@ def test_series_names_different_types(self, series_names: Dict[type, List[Any]]) """ # Create Matplotlib axes that use a PNG backend. The default backend, FigureCanvasSVG, does not - # have `tostring_rgb()` which is needed to compute the difference between two figures in this + # have `buffer_rgba()` which is needed to compute the difference between two figures in this # method. We need to set the axes as MplDrawer will use # `qiskit_experiments.framework.matplotlib.get_non_gui_ax` by default; which uses an SVG backend. + plt.close("all") plt.switch_backend("Agg") axes = {} for key in series_names.keys(): @@ -161,16 +162,12 @@ def test_series_names_different_types(self, series_names: Dict[type, List[Any]]) for plot_type in legend_plot_types: plotter.enable_legend_for(series_name, plot_type) - # Generate figure and save to buffers for comparison. This requires a pixel backend, like AGG, so - # that `tostring_rgb()` is available. + # Generate figure and save to buffers for comparison. figure_data = {} for plotter_type, plotter in plotters.items(): figure = plotter.figure().figure figure.canvas.draw() - figure_data[plotter_type] = np.frombuffer( - figure.canvas.tostring_rgb(), - dtype=np.uint8, - ).reshape(figure.canvas.get_width_height() + (3,)) + figure_data[plotter_type] = np.asarray(figure.canvas.buffer_rgba(), dtype=np.uint8) # Compare root-mean-squared error between two images. for (fig1_type, fig1), (fig2_type, fig2) in combinations(figure_data.items(), 2):