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

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

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

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

Python

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

Q&A

解決済

1回答

1081閲覧

シェルピンスキーの三角形が繰り返されてしまう件

humuhimi

総合スコア49

Matplotlib

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

Python

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

0グッド

0クリップ

投稿2019/01/10 00:46

イメージ説明

python

1''' 2sierpinski.py 3Draw Sierpinski Triangle 4''' 5import random 6import matplotlib.pyplot as plt 7 8def transformation_1(p): 9 x = p[0] 10 y = p[1] 11 x1 = 0.5*x 12 y1 = 0.5*y 13 return x1, y1 14 15def transformation_2(p): 16 x = p[0] 17 y = p[1] 18 x1 = 0.5*x + 0.5 19 y1 = 0.5*y + 0.5 20 return x1, y1 21 22def transformation_3(p): 23 x = p[0] 24 y = p[1] 25 x1 = 0.5*x + 1 26 y1 = 0.5*y 27 return x1, y1 28 29def get_index(probability): 30 r = random.random() 31 c_probability = 0 32 sum_probability = [] 33 for p in probability: 34 c_probability += p 35 sum_probability.append(c_probability) 36 for item, sp in enumerate(sum_probability): 37 if r <= sp: 38 return item 39 return len(probability)-1 40 41def transform(p): 42 # list of transformation functions 43 transformations = [transformation_1, transformation_2, transformation_3] 44 probability = [1/3, 1/3, 1/3] 45 # pick a random transformation function and call it 46 tindex = get_index(probability) 47 t = transformations[tindex] 48 x, y = t(p) 49 return x, y 50 51def draw_sierpinski(n): 52 # We start with (0, 0) 53 x = [0] 54 y = [0] 55 56 x1, y1 = 0, 0 57 for i in range(n): 58 x1, y1 = transform((x1, y1)) 59 x.append(x1) 60 y.append(y1) 61 return x, y 62 63if __name__ == '__main__': 64 n = int(input('Enter the desired number of points' 65 'in the Sierpinski Triangle: ')) 66 x, y = draw_sierpinski(n) 67 # Plot the points 68 plt.plot(x, y, 'o') 69 plt.title('Sierpinski with {0} points'.format(n)) 70 plt.show()

イメージ説明

python

1import matplotlib.pyplot as plt 2import random 3 4 5def transform_1(p): 6 x = p[0] 7 y = p[1] 8 x1 = 0.5*x 9 y1 = 0.5*y 10 11 return x1,y1 12 13 14def transform_2(p): 15 x = p[0] 16 y = p[1] 17 x1 = 0.5 * x + 0.5 18 y1 = 0.5 * y + 0.5 19 20 return x1, y1 21 22 23def transform_3(p): 24 x = p[0] 25 y = p[1] 26 27 x1 = 0.5 * x + 1.0 28 y1 = 0.5 * y 29 30 return x1, y1 31 32 33def sierpinski_gasket(n): 34 transformations = [transform_1, transform_2, transform_3] 35 # p[0] = 0 36 # p[1] = 0 37 x = [0] 38 y = [0] 39 40 x1, y1 = 0,0 41 42 while n > 0: 43 rand_index = random.randint(0, 2) 44 t = transformations[rand_index] 45 p = (x1, y1) 46 x1, y1 = t(p) 47 x.append(x1) 48 y.append(y1) 49 n -= 1 50 51 return x, y 52 53 54if __name__ == '__main__': 55 56 n = int(input('試行回数を入力してください')) 57 x, y = sierpinski_gasket(n) 58 ax = plt.axis(xmin=0,ymin=0) 59 plt.plot(x,y,'o') 60 plt.title('sierpinski with {0}point'.format(n)) 61 plt.show() 62

下の図を上の図の正しいシェルピンスキーの三角形にしたいけどやり方がわかりません。どなたか知恵を
貸してください

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

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

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

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

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

guest

回答1

0

ベストアンサー

(A)先のコードは質問者さんが参考にしておられるコード
(B)後のコードは質問者さんご自身で書いたもの
ということだと思います。

両者でグラフの描画範囲を比べると垂直軸の範囲は両者とも同じ0~1ですが、水平軸は(A)が0~2であるのに対し(B)は0~1になってしまってますね。それが期待通りにならなかった原因であることがまずわかります。

さて(A),(B)を比べてみますと質問者さんが追加した1行に目がいきます。

ax = plt.axis(xmin=0, ymin=0)

matplotlibは賢くできてまして「描こうとするグラフがちょうど収まるような軸範囲を自動的に仮定してくれる」のですが、明示的に範囲を指定することもでき、上記のaxisは後者とするための一つの方法のようです。さてこのコードでは水平方向の最小値を指定してますが最大値を指定してません。結果がどうなるかいくつか試してみると次のようになりますので、軸範囲を固定する際のデフォルトの範囲は水平・垂直ともに0~1、最小や最大を省略すると単にデフォルト値が仮定されるようです。

python

1plt.axis() # ==> (0.0, 1.0, 0.0, 1.0) 2plt.axis(xmin=0.5) # ==> (0.5, 1.0, 0.0, 1.0) 3plt.axis(xmax=0.5) # ==> (0.0, 0.5, 0.0, 1.0)

ということで解決法は

(1) plt.axis(xmax=2)とする
(2) この行を取り去り単にmatplotlibの「軸範囲の自動仮定」に期待する

のいずれでもよさそうです。実際やってみるとどちらの方法でも期待どおりの描画結果になりました。

投稿2019/01/10 02:50

編集2019/01/10 02:51
KSwordOfHaste

総合スコア18394

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

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

humuhimi

2019/01/10 04:58

ありがとうございます。 気持ちいいほど綺麗な回答に感動しました????
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問