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

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

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

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

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Python

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

Q&A

解決済

1回答

1308閲覧

Pythonで、matplotlib にてグラフを表示しようとしているのですが、エラー表示もされず、グラフが表示されません。

maku_cyan

総合スコア2

Matplotlib

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

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Python

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

0グッド

0クリップ

投稿2020/05/14 04:22

編集2020/05/14 04:33

前提・実現したいこと

Pythonで、matplotlib にてグラフを表示しようとしているのですが、エラー表示もされず、グラフが表示されません。
簡単なグラフはmatplotlib にて表示ができるのですが、下記コードではグラフが表示されません。

コード自体は、
https://github.com/istellartech/OpenGoddard/blob/master/examples/10_Low_Thrust_Orbit_Transfer.py
のオープンソースを自分のパソコンで試しているだけで、コード自体に問題はないと思っております。

このコードはOpenGoddardと呼ばれる、最適化問題に使われているコードだそうで、
https://github.com/istellartech/OpenGoddard
から、
$ pip install OpenGoddard

にて、ダウンロードをして使っています。

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

エラーは発生していません。plt.show()が動いていません。

該当のソースコード

python

1# -*- coding: utf-8 -*- 2# Copyright 2017 Interstellar Technologies Inc. All Rights Reserved. 3 4from __future__ import print_function 5import numpy as np 6import matplotlib.pyplot as plt 7from OpenGoddard.optimize import Problem, Guess, Condition, Dynamics 8 9class Ball: 10 def __init__(self): 11 self.g = 1.0 # gravity 12 self.l = 1.0 # goal 13 self.h = 0.1 # limit 14 self.theta0 = np.deg2rad(30) # limit and initial angle 15 16 17def dynamics(prob, obj, section): 18 x = prob.states(0, section) 19 y = prob.states(1, section) 20 v = prob.states(2, section) 21 theta = prob.controls(0, section) 22 23 g = obj.g 24 25 dx = Dynamics(prob, section) 26 dx[0] = v * np.sin(theta) 27 dx[1] = v * np.cos(theta) 28 dx[2] = g * np.cos(theta) 29 return dx() 30 31 32def equality(prob, obj): 33 x = prob.states_all_section(0) 34 y = prob.states_all_section(1) 35 v = prob.states_all_section(2) 36 theta = prob.controls_all_section(0) 37 tf = prob.time_final(-1) 38 39 result = Condition() 40 41 # event condition 42 result.equal(x[0], 0.0) 43 result.equal(y[0], 0.0) 44 result.equal(v[0], 0.0) 45 result.equal(x[-1], obj.l) 46 47 return result() 48 49 50def inequality(prob, obj): 51 x = prob.states_all_section(0) 52 y = prob.states_all_section(1) 53 v = prob.states_all_section(2) 54 theta = prob.controls_all_section(0) 55 tf = prob.time_final(-1) 56 57 result = Condition() 58 59 # lower bounds 60 result.lower_bound(tf, 0.1) 61 result.lower_bound(y, 0) 62 result.lower_bound(theta, 0) 63 64 # upper bounds 65 # result.upper_bound(theta, np.pi/2) 66 # result.upper_bound(y, x * np.tan(obj.theta0) + obj.h) 67 68 return result() 69 70 71def cost(prob, obj): 72 tf = prob.time_final(-1) 73 return tf 74 75 76def cost_derivative(prob, obj): 77 jac = Condition(prob.number_of_variables) 78 # index_tf = prob.index_time_final(0) 79 index_tf = prob.index_time_final(-1) 80 jac.change_value(index_tf, 1) 81 return jac() 82 83# ======================== 84plt.close("all") 85plt.ion() 86# Program Starting Point 87time_init = [0.0, 2.0] 88n = [20] 89num_states = [3] 90num_controls = [1] 91max_iteration = 30 92 93flag_savefig = False 94 95savefig_dir = "01_Brachistochrone/normal_" 96 97# ------------------------ 98# set OpenGoddard class for algorithm determination 99prob = Problem(time_init, n, num_states, num_controls, max_iteration) 100obj = Ball() 101 102# ======================== 103# Initial parameter guess 104theta_init = Guess.linear(prob.time_all_section, np.deg2rad(30), np.deg2rad(30)) 105# Guess.plot(prob.time_all_section, theta_init, "gamma", "time", "gamma") 106# if(flag_savefig):plt.savefig(savefig_dir + "guess_gamma" + savefig_add + ".png") 107 108x_init = Guess.linear(prob.time_all_section, 0.0, obj.l) 109# Guess.plot(prob.time_all_section, x_init, "x", "time", "x") 110# if(flag_savefig):plt.savefig(savefig_dir + "guess_x" + savefig_add + ".png") 111 112y_init = Guess.linear(prob.time_all_section, 0.0, obj.l / np.sqrt(3)) 113# Guess.plot(prob.time_all_section, theta_init, "y", "time", "y") 114# if(flag_savefig):plt.savefig(savefig_dir + "guess_y" + savefig_add + ".png") 115 116prob.set_states_all_section(0, x_init) 117prob.set_states_all_section(1, y_init) 118prob.set_controls_all_section(0, theta_init) 119 120# ======================== 121# Main Process 122# Assign problem to SQP solver 123prob.dynamics = [dynamics] 124prob.knot_states_smooth = [] 125prob.cost = cost 126prob.cost_derivative = cost_derivative 127prob.equality = equality 128prob.inequality = inequality 129 130prob.solve(obj) 131 132# ======================== 133# Post Process 134# ------------------------ 135# Convert parameter vector to variable 136x = prob.states_all_section(0) 137y = prob.states_all_section(1) 138gamma = prob.controls_all_section(0) 139time = prob.time_update() 140 141# ------------------------ 142# Visualizetion 143print("--Visualizetion--") 144plt.figure() 145plt.subplot(2, 1, 1) 146plt.plot(time, x, marker="o", label="x") 147plt.plot(time, y, marker="o", label="y") 148for line in prob.time_knots(): 149 plt.axvline(line, color="k", alpha=0.5) 150plt.grid() 151plt.ylabel("velocity [m/s]") 152plt.legend(loc="best") 153 154plt.subplot(2, 1, 2) 155plt.plot(time, gamma, marker="o", label="gamma") 156for line in prob.time_knots(): 157 plt.axvline(line, color="k", alpha=0.5) 158plt.grid() 159plt.xlabel("time [s]") 160plt.ylabel("angle [rad]") 161plt.legend(loc="best") 162plt.show() 163if(flag_savefig): plt.savefig(savefig_dir + "plot" + ".png") 164 165x_wall = np.linspace(0, obj.l) 166y_wall = x_wall * np.tan(obj.theta0) + obj.h 167plt.figure() 168plt.plot(x, y, marker="o", label="trajectry") 169#plt.plot(x_wall, y_wall, color = "k", label = "wall") 170plt.axhline(0, color="k") 171plt.axvline(0, color="k") 172plt.axvline(obj.l, color="k") 173plt.grid() 174plt.xlabel("x [m]") 175plt.ylabel("y [m]") 176plt.ylim([-0.02, 0.6]) 177plt.legend(loc="best") 178plt.gca().invert_yaxis() 179if(flag_savefig): plt.savefig(savefig_dir + "trajectry" + ".png") 180 181plt.show()

試したこと

先日、tkinterを使えるように実装をし直しました。
元々のパイソンではtkinterが使えず、他のコードで_tkinterエラーが出たため、
https://github.com/pyenv/pyenv/issues/1375#issuecomment-524280004
上記のやり方で、tkinterを使えるように、pythonをダウンロードし直しました。

これ自体に問題があったのでしょうか。
下記のような

python

1import matplotlib.pyplot as plt 2import numpy as np 3 4x = np.linspace(0, 10, 10000) 5 6plt.figure() 7 8plt.plot(x, np.sin(x)) 9plt.plot(x, np.cos(x)) 10plt.show() 11

簡単なグラフを、別ファイルで作れば、グラフは表示できますが、
該当のコードに、このコードを書いた場合、グラフは表示されません。
何故、ファイルによって、plt.show()が表示できないのでしょうか。
(全て同じディレクトリ内で試しました。)

パイソンに詳しい方、どうか教えていただけないでしょうか。

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

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

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

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

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

meg_

2020/05/14 05:10

・グラフの保存はしていますか? ・スクリプトの実行は何でされていますか?
maku_cyan

2020/05/14 05:17

お早い回答有り難うございます。 グラフの保存はしていません。 flag_savefig=Falseとしているのですが、セーブをしないと見れないコードがあるのでしょうか。 Atom python3.8.1 で行っております。
meg_

2020/05/14 05:24

plt.show()の後にplt.savefigすると保存できなかったり、jupyterでは問題があったりすることもあるので確認させていただきました。 どちらも今回のケースには当てはまらないようです。
x98000

2020/05/14 05:28

「該当のコードに、このコードを書いた場合、グラフは表示されません。」 というのは具体的に何をやったのでしょうか。
meg_

2020/05/14 05:28

plt.ion()をコメントアウトして実行したらどうなりますか?
maku_cyan

2020/05/14 05:36

本当に有り難うございます。表示ができました。 plt.ion()には、そういう意味があったのですね、、 確認不足でした。本当に有り難うございました。
hayataka2049

2020/05/14 06:43

問題が解決済みであれば、meg_さんの回答かmaku_cyanさんの自己回答で解説方法を説明し、ベストアンサーとして解決済みの状態とした方が後に見る人のために親切です。
guest

回答1

0

自己解決

plt.ion() 
には、ターミナルでのコマンド入力を待って描画をinteractiveに行う.
という意味があるため、描画ができなかった。

投稿2020/05/14 19:33

maku_cyan

総合スコア2

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問