質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Qt

QtはGUIプログラムの開発で広く使われているクロスプラットフォーム開発のフレームワークです。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

11745閲覧

couldn't connect to display :0.0

lupus

総合スコア0

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Qt

QtはGUIプログラムの開発で広く使われているクロスプラットフォーム開発のフレームワークです。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/07/31 02:47

編集2020/08/02 08:10

前提・実現したいこと

matplotlibを使ってGUIに表示するようなプログラムをGUIで行しようとすると以下のエラーが出ました。

_tkinter.TclError: couldn't connect to display "localhost:0.0"
環境はsshや仮想環境ではありません。(venvは使っています)

また、pyqt5を使ったプログラムでも同様に以下のエラーが出ました。
Invalid MIT-MAGIC-COOKIE-1 keyqt.qpa.xcb: could not connect to display :0.0

2時間ほど検索などで情報収集しても、ssh環境での想定がされていることが多く、解決法を見つけられなかったのでお願いします。

発生している問題・エラーメッセージ

・matplotlibを使ったプログラムでのエラーは以下です Traceback (most recent call last): File "test.py", line 8, in <module> fig = plt.figure() File "/home/hogehoge/ドキュメント/env/tvenv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 670, in figure figManager = new_figure_manager(num, figsize=figsize, File "/home/hogehoge/ドキュメント/env/tvenv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 298, in new_figure_manager return _backend_mod.new_figure_manager(*args, **kwargs) File "/home/hogehoge/ドキュメント/env/tvenv/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 3490, in new_figure_manager return cls.new_figure_manager_given_figure(num, fig) File "/home/hogehoge/ドキュメント/env/tvenv/lib/python3.8/site-packages/matplotlib/backends/_backend_tk.py", line 859, in new_figure_manager_given_figure window = tk.Tk(className="matplotlib") File "/usr/local/lib/python3.8/tkinter/__init__.py", line 2261, in __init__ self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: couldn't connect to display "localhost:0.0" ・pyqt5を使ったプログラムでのエラーは以下です Invalid MIT-MAGIC-COOKIE-1 keyqt.qpa.xcb: could not connect to display :0.0 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, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.

該当のソースコード

python3

1・matplotlibでのプログラム 2import matplotlib 3#matplotlib.use('pdf') 4from matplotlib import pyplot as plt 5from PIL import Image 6import numpy as np 7im = np.array(Image.open("hogehoge.png")) 8 9fig = plt.figure() 10plt.imshow(im) 11 12x=[100,100,400,400] 13y=[200,500,200,500] 14 15plt.plot(x[:2],y[:2],'r*') 16 17fig.savefig("./test.png") 18fig.show() 19 20 21・bashrcの中身 22.bashrcの中身 23# ~/.bashrc: executed by bash(1) for non-login shells. 24# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) 25# for examples 26 27# If not running interactively, don't do anything 28case $- in 29 *i*) ;; 30 *) return;; 31esac 32 33# don't put duplicate lines or lines starting with space in the history. 34# See bash(1) for more options 35HISTCONTROL=ignoreboth 36 37# append to the history file, don't overwrite it 38shopt -s histappend 39 40# for setting history length see HISTSIZE and HISTFILESIZE in bash(1) 41HISTSIZE=1000 42HISTFILESIZE=2000 43 44# check the window size after each command and, if necessary, 45# update the values of LINES and COLUMNS. 46shopt -s checkwinsize 47 48# If set, the pattern "**" used in a pathname expansion context will 49# match all files and zero or more directories and subdirectories. 50#shopt -s globstar 51 52# make less more friendly for non-text input files, see lesspipe(1) 53[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" 54 55# set variable identifying the chroot you work in (used in the prompt below) 56if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then 57 debian_chroot=$(cat /etc/debian_chroot) 58fi 59 60# set a fancy prompt (non-color, unless we know we "want" color) 61case "$TERM" in 62 xterm-color|*-256color) color_prompt=yes;; 63esac 64 65# uncomment for a colored prompt, if the terminal has the capability; turned 66# off by default to not distract the user: the focus in a terminal window 67# should be on the output of commands, not on the prompt 68#force_color_prompt=yes 69 70if [ -n "$force_color_prompt" ]; then 71 if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; the 72 # We have color support; assume it's compliant with Ecma-48 73 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such 74 # a case would tend to support setf rather than setaf.) 75 color_prompt=yes 76 else 77 color_prompt= 78 fi 79fi 80 81if [ "$color_prompt" = yes ]; then 82 PS1='${debian_chroot:+($debian_chroot)}[\033[01;32m]\u@\h[\033[00m]:[\033[01;34m]\w[\033[00m]$ ' 83else 84 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$ ' 85fi 86unset color_prompt force_color_prompt 87 88# If this is an xterm set the title to user@host:dir 89case "$TERM" in 90xterm*|rxvt*) 91 PS1="[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a]$PS1" 92 ;; 93*) 94 ;; 95esac 96 97# enable color support of ls and also add handy aliases 98if [ -x /usr/bin/dircolors ]; then 99 test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" 100 alias ls='ls --color=auto' 101 #alias dir='dir --color=auto' 102 #alias vdir='vdir --color=auto' 103 104 alias grep='grep --color=auto' 105 alias fgrep='fgrep --color=auto' 106 alias egrep='egrep --color=auto' 107fi 108 109# colored GCC warnings and errors 110#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' 111 112# some more ls aliases 113alias ll='ls -alF' 114alias la='ls -A' 115alias l='ls -CF' 116 117# Add an "alert" alias for long running commands. Use like so: 118# sleep 10; alert 119alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]+\s*//;s/[;&|]\s*alert$//'\'')"' 120 121# Alias definitions. 122# You may want to put all your additions into a separate file like 123# ~/.bash_aliases, instead of adding them here directly. 124# See /usr/share/doc/bash-doc/examples in the bash-doc package. 125 126if [ -f ~/.bash_aliases ]; then 127 . ~/.bash_aliases 128fi 129 130# enable programmable completion features (you don't need to enable 131# this, if it's already enabled in /etc/bash.bashrc and /etc/profile 132# sources /etc/bash.bashrc). 133if ! shopt -oq posix; then 134 if [ -f /usr/share/bash-completion/bash_completion ]; then 135 . /usr/share/bash-completion/bash_completion 136 elif [ -f /etc/bash_completion ]; then 137 . /etc/bash_completion 138 fi 139fi 140 141## CUDA paths 142export PATH=/usr/local/cuda-10.1/bin:/usr/local/cuda-10.1/NsightCompute-2019.1${PATH:+:${PATH}} 143export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} 144 145## CUDA paths 146export PATH=/usr/local/cuda-10.1/bin:/usr/local/cuda-10.1/NsightCompute-2019.1${PATH:+:${PATH}} 147export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} 148 149## CUDA paths 150export PATH=/usr/local/cuda-10.2/bin:/usr/local/cuda-10.2/NsightCompute-2019.1${PATH:+:${PATH}} 151 152##openpose_path 153export PYTHONPATH=/media/hegehoge/0746e58c-9c06-45a5-8029-92750018f1d0/openpose/code/pytorch_Realtime_Multi-Person_Pose_Estimation/lib:$PYTHONPATH 154export DISPLAY=localhost:0.0

試したこと

2行目のコメントアウトを外すことで、16行目の画像の保存はできましたが、17行目の画像の表示ができません。
echo $DISPLAYではlocalhost:0.0が表示されます。

補足情報(FW/ツールのバージョンなど)

Ubuntu 18.04.4 LTS
python 3.8.3
matplotlib 3.3.0
numpy 1.19.1
Pillow 7.2.0
pip 19.2.3

数ヶ月前に,sshでx11を使おうとして、少し設定を変更してしまった可能性が有ります。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/07/31 08:30

.bashrcの部分を教えてください。個人情報部分は飛ばして構いません。
yymmt

2020/08/02 05:26

1. ログインしたユーザとpythonを実行しているユーザは同じでしょうか? 2. ping localhostとしてlocalhostと通信できるでしょうか?IPv4を切ったりしていませんか?
lupus

2020/08/02 08:12

遅くなってしまいすみません。 .bashrcについての情報を該当のソース欄に記入しました。
lupus

2020/08/02 08:15

@yymmtさん> 1.同じです。 2.ping localhostの結果は以下で,接続できていると思います。 PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.029 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.043 ms 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.044 ms
yymmt

2020/08/02 12:07

問題点の切り分けのために.bashrcの一番下にある export DISPLAY=localhost:0.0 を削除(コメント化)した結果がどうなるか知りたいです。
lupus

2020/08/02 13:55

yymmtさん> コメントアウト後も同様の結果でした。
guest

回答1

0

自己解決

export DISPLAY:=1.0
でdisplayの環境変数を1.0にすると直りました。
おそらく、sshでxサーバーを使うときに番号を変えてしまったんだと思います。
もっと早くにこの方法(環境変数をあてずっぽうで変えてみる)試せばよかったです。
回答してくださった方々ありがとうございました。

投稿2020/08/03 04:31

lupus

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問