Skip to content

Commit 36924bf

Browse files
nkanazawa1989mergify[bot]
authored andcommitted
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. (cherry picked from commit b27f33c)
1 parent 48bd2aa commit 36924bf

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

test/visualization/test_plotter_mpldrawer.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ def test_series_names_different_types(self, series_names: Dict[type, List[Any]])
108108
"""
109109

110110
# Create Matplotlib axes that use a PNG backend. The default backend, FigureCanvasSVG, does not
111-
# have `tostring_rgb()` which is needed to compute the difference between two figures in this
111+
# have `buffer_rgba()` which is needed to compute the difference between two figures in this
112112
# method. We need to set the axes as MplDrawer will use
113113
# `qiskit_experiments.framework.matplotlib.get_non_gui_ax` by default; which uses an SVG backend.
114+
plt.close("all")
114115
plt.switch_backend("Agg")
115116
axes = {}
116117
for key in series_names.keys():
@@ -151,16 +152,12 @@ def test_series_names_different_types(self, series_names: Dict[type, List[Any]])
151152
for plot_type in legend_plot_types:
152153
plotter.enable_legend_for(series_name, plot_type)
153154

154-
# Generate figure and save to buffers for comparison. This requires a pixel backend, like AGG, so
155-
# that `tostring_rgb()` is available.
155+
# Generate figure and save to buffers for comparison.
156156
figure_data = {}
157157
for plotter_type, plotter in plotters.items():
158158
figure = plotter.figure().figure
159159
figure.canvas.draw()
160-
figure_data[plotter_type] = np.frombuffer(
161-
figure.canvas.tostring_rgb(),
162-
dtype=np.uint8,
163-
).reshape(figure.canvas.get_width_height() + (3,))
160+
figure_data[plotter_type] = np.asarray(figure.canvas.buffer_rgba(), dtype=np.uint8)
164161

165162
# Compare root-mean-squared error between two images.
166163
for (fig1_type, fig1), (fig2_type, fig2) in combinations(figure_data.items(), 2):

0 commit comments

Comments
 (0)