2017-05-27 6 views
0

私はプログラミングが初めてで、与えられた2つのリストからポイントの接続プロットを描くように頼んだ質問があります。私のロジックは、私はあなたが他のすべてのとすべての点を結ぶプロットを意味コネクティビティプロットで想定ポイントの接続性プロット

from matplotlib import pyplot as plt 
import numpy as np 

def Question_3(): 

    x = [0, 0.7, 1, 0.7, 0, -0.7, -1, -0.7] 
    y = [1, 0.7, 0, -0.7, -1, -0.7, 0, 0.7] 

    plt.subplot(121) 
    plt.title("Scatter plot of points", fontsize = 16) 
    plt.plot(x, y, ".k") 
    plt.show() 

    plt.subplot(122) 
    plt.title("Connectivity plot of points", fontsize = 16) 

    for C1 in zip(x, y): 
     for C2 in zip(x, y):    
      plt.plot(C1, C2, "-r")  

    plt.show() 
Question_3() 

答えて

1

を助けてください...ループのために他のをプロットしながら、一定の単一の座標を維持するが、これは動作しませんでしたポイント。

その意味で、あなたが取ったアプローチは正しいです。 plt.plot(x,y)には、第1引数としてのx座標のリストまたはシーケンスと、第2引数としてのy座標に対するリストまたはシーケンスがあります。したがって、変数を2つのループから解凍して、xおよびyコンポーネントに分割する必要があります。

from matplotlib import pyplot as plt 

def Question_3(): 

    x = [0, 0.7, 1, 0.7, 0, -0.7, -1, -0.7] 
    y = [1, 0.7, 0, -0.7, -1, -0.7, 0, 0.7] 

    plt.subplot(121) 
    plt.title("Scatter plot of points", fontsize = 16) 
    plt.plot(x, y, ".k") 


    plt.subplot(122) 
    plt.title("Connectivity plot of points", fontsize = 16) 

    for x0,y0 in zip(x, y): 
     for x1,y1 in zip(x, y):    
      plt.plot((x0,x1), (y0,y1), "-r")  

    plt.show() 
Question_3() 

enter image description here

+0

感謝!!私はそれを試みたが、私は 'plt.plot((x0、y0)、(x1、y1)、" -r ")'を使った。 –

関連する問題