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

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

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

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1698閲覧

sympyで点をプロットと凡例を表示させたい

iwakuratomomi50

総合スコア8

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/11/28 04:38

前提・実現したいこと

タイトル通りです.matplotしか分からずsympyで詰まったので質問させていただきました.
点はソースコード中の(x1,y1)(x2,y2)(x3,y3)をプロットさせたいです

該当のソースコード

python

1import sympy 2from sympy.plotting import plot_implicit 3 4x1 = -7 5y1 = 0 6x2 = 7 7y2 = 0 8x3 = 0 9y3 = 7 10c = 1 11t1 = 10 12t2 = 6 13t3 = 5 14x0 = sympy.Symbol('x' ,real=True) 15y0 = sympy.Symbol('y' ,real=True) 16equation1 = sympy.sqrt((x2 - x0)**2 + (y2 - y0)**2) - sympy.sqrt((x1 - x0)**2 + (y1 - y0)**2) - c*(t2-t1)#マイク1と2 17equation2 = sympy.sqrt((x3 - x0)**2 + (y3 - y0)**2) - sympy.sqrt((x1 - x0)**2 + (y1 - y0)**2) - c*(t3-t1)#マイク1と3 18equation3 = sympy.sqrt((x3 - x0)**2 + (y3 - y0)**2) - sympy.sqrt((x2 - x0)**2 + (y2 - y0)**2) - c*(t3-t2)#マイク3と2 19sympy.init_printing() 20print(sympy.solve([equation1, equation2,equation3])) 21 22plot1 = plot_implicit(equation1,(x0,-10,10),(y0,-10,10),legend = True, line_color="blue", show=False) 23plot2 = plot_implicit(equation2,(x0,-10,10),(y0,-10,10),legend = True, line_color="green", show=False) 24plot3 = plot_implicit(equation3,(x0,-10,10),(y0,-10,10),legend = True, line_color="red", show=False) 25 26plot1.label = 'mic1-2' 27plot2.label = 'mic1-3' 28plot3.label = 'mic2-3' 29 30plot1.extend(plot3) 31plot1.extend(plot2) 32 33plot1.show() 34

試したこと

イメージ説明

このように凡例が表示されるべきところが□になってしまっています.
また点プロットの仕方が分かりません

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

陰関数がプロットできるならsympyでなくとも構いません.
お力添えいただけると幸いです

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

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

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

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

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

guest

回答1

0

ベストアンサー

以下のコードで大体のことはできましたが、いかがでしょうか。

処理内容としては、
1、sympyで作成した関数の座標をリスト化する
2、座標リストからプロットする部分を抽出する
3、抽出部分をmatplotlibのfigにプロットする

どうやら点のプロットはsympy単体ではできないようです。

python3

1import sympy 2from sympy.plotting import plot_implicit 3#matplotlibと連携 4import matplotlib.pyplot as plt 5%matplotlib inline 6 7x1 = -7 8y1 = 0 9x2 = 7 10y2 = 0 11x3 = 0 12y3 = 7 13c = 1 14t1 = 10 15t2 = 6 16t3 = 5 17x0 = sympy.Symbol('x' ,real=True) 18y0 = sympy.Symbol('y' ,real=True) 19equation1 = sympy.sqrt((x2 - x0)**2 + (y2 - y0)**2) - sympy.sqrt((x1 - x0)**2 + (y1 - y0)**2) - c*(t2-t1)#マイク1と2 20equation2 = sympy.sqrt((x3 - x0)**2 + (y3 - y0)**2) - sympy.sqrt((x1 - x0)**2 + (y1 - y0)**2) - c*(t3-t1)#マイク1と3 21equation3 = sympy.sqrt((x3 - x0)**2 + (y3 - y0)**2) - sympy.sqrt((x2 - x0)**2 + (y2 - y0)**2) - c*(t3-t2)#マイク3と2 22sympy.init_printing() 23print(sympy.solve([equation1, equation2,equation3])) 24 25rangex, rangey = (-10, 10), (-10, 10) 26 27fig = plt.figure() 28ax = fig.add_subplot(1, 1, 1) 29ax.set_xlim(rangex) 30ax.set_ylim(rangey) 31ax.set_aspect('equal') 32 33#点のプロット 34ax.plot(x1, y1, marker='.', markersize=6, color='blue',label = 'point1') 35ax.plot(x2, y2, marker='.', markersize=6, color='green',label = 'point2') 36ax.plot(x3, y3, marker='.', markersize=6, color='red',label = 'point3') 37 38#関数の座標をリストとして取得 39plot1 = plot_implicit(equation1,(x0,-10,10),(y0,-10,10), show=False)[0].get_points()[0] 40plot2 = plot_implicit(equation2,(x0,-10,10),(y0,-10,10), show=False)[0].get_points()[0] 41plot3 = plot_implicit(equation3,(x0,-10,10),(y0,-10,10), show=False)[0].get_points()[0] 42 43#プロットする部分を抽出 44px1, py1 = [], [] 45for (ix, iy) in plot1: 46 px1.extend([ix.start, ix.start, ix.end, ix.end, None]) 47 py1.extend([iy.start, iy.end, iy.end, iy.start, None]) 48ax.fill(px1, py1, facecolor='blue',label = 'mic1-2') 49 50px2, py2 = [], [] 51for (ix, iy) in plot2: 52 px2.extend([ix.start, ix.start, ix.end, ix.end, None]) 53 py2.extend([iy.start, iy.end, iy.end, iy.start, None]) 54ax.fill(px2, py2, facecolor='green',label = 'mic1-3') 55 56px3, py3 = [], [] 57for (ix, iy) in plot3: 58 px3.extend([ix.start, ix.start, ix.end, ix.end, None]) 59 py3.extend([iy.start, iy.end, iy.end, iy.start, None]) 60ax.fill(px3, py3, facecolor='red',label = 'mic2-3') 61 62plt.legend(loc='lower right',fontsize=8) 63 64plt.show()

参考:sympyで作成されるグラフについて

投稿2019/11/30 11:32

amahara_waya

総合スコア1029

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

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

iwakuratomomi50

2019/11/30 13:07

大変参考になりました.ほんとにありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問