WSLのAnacondaでmatplotlib が使えなかった
前書きと注意
WSL2のUbuntuにおいてanacondaで新たに仮想環境を作成し,matplotlibを利用しようとソースコードを実行したところ,次のエラーが出た。
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, minimal, minimalegl, offscreen, vnc, webgl, xcb.
matplotlibがQtを利用しようとしているが,xcbがあるにもかかわらずロードできないというエラーっぽい。この場合,xcbを使うためのライブラリが不足している可能性がある。
2025/02/08のChatGPT o3-mini-highにエラーを入力したところ,次のような回答が得られた。また,指定されたプラグインをインストールすることでmatplotlibによる描画ができることを確認した。
やったこと
・ChatGPTへの入力
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.Available platform plugins are: eglfs, minimal, minimalegl, offscreen, vnc, webgl, xcb.
というエラーが出ます。回避策,解決策はありますか?エディタはVScodeでWSL2を利用しています。
・回答
1. 必要なライブラリをインストールする
WSL2内で、以下のコマンドを実行して不足しているライブラリ(例:libxcb-xinerama0など)をインストールします。多くの場合、以下のライブラリが必要になります。
sudo apt update sudo apt install libxcb-xinerama0 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-render-util0
これらのライブラリは、Qtの"xcb"プラグインが正しく動作するために必要です。
あとは指定されたライブラリをインストールした後にスクリプトを再度実行すると,plotが描画された。
あとがき
・正直余計なプラグインが入っている可能性がある。
・次回同様の問題が発生した場合,より細かく必要なライブラリをチェックする。