2017-10-06 14 views
0

私のコードはOSX El CapitanのTerminalにプロットを描画しますが、PyCharmではプロットしません。 TerminalとPyCharmの両方のインタプリタは、Anaconda Python 3.6.2に設定され、必要なパッケージがすべてインストールされています。プロットはPyCharmには表示されませんが、OSX端末の端末に表示されます

def plot_data(samples, centroids, clusters=None): 
    """ 
    Plot samples and color it according to cluster centroid. 
    :param samples: samples that need to be plotted. 
    :param centroids: cluster centroids. 
    :param clusters: list of clusters corresponding to each sample. 
    """ 

    colors = ['blue', 'green', 'gold'] 
    assert centroids is not None 

    if clusters is not None: 
     sub_samples = [] 
     for cluster_id in range(centroids[0].shape[0]): 
      sub_samples.append(np.array([samples[i] for i in range(samples.shape[0]) if clusters[i] == cluster_id])) 
    else: 
     sub_samples = [samples] 

    plt.figure(figsize=(7, 5)) 

    for clustered_samples in sub_samples: 
     cluster_id = sub_samples.index(clustered_samples) 
     #print(cluster_id) 
     #print(clustered_samples[:,0]) 
     #print(clustered_samples[:,1]) 
     plt.plot(clustered_samples[:, 0], clustered_samples[:, 1], 'o', color=colors[cluster_id], alpha=0.75, 
       label='Data Points: Cluster %d' % cluster_id) 

    plt.xlabel('x1', fontsize=14) 
    plt.ylabel('x2', fontsize=14) 
    plt.title('Plot of X Points', fontsize=16) 
    plt.grid(True) 

    # Drawing a history of centroid movement 
    tempx, tempy = [], [] 
    for mycentroid in centroids: 
     tempx.append(mycentroid[:, 0]) 
     tempy.append(mycentroid[:, 1]) 

    for cluster_id in range(len(tempx[0])): 
     plt.plot(tempx, tempy, 'rx--', markersize=8) 

    plt.legend(loc=4, framealpha=0.5) 

    plt.show(block=True) 

また、私はPycharm does not show plotで解決策を試してみましたが、それらのどれも実際に働きました。たとえば、plt.show(block=True)の後に次の行を追加しても問題は解決しませんでした。

matplotlib.get_backend() 
plt.interactive(False) 
plt.figure() 

答えて

1

私は最近、ほとんど同じirritanceが発生し、また、matplotlibのは/ etcのためにどこでも言及した他のソリューションをしようとしました。ターミナル(別名、通常のpython/ipython)がこれらのプロットを実行しているのを見て、バグのオーバーヘッドを避ける純粋なコンソールを実行するには、PyCharmの回避策が必要であると考えました。さて、私が見つけた最初のanswer'sのアドバイスに続いて、PyCharmにあるPython Consoleにアクセスするだけで、プロットが正常に出力されます。

+0

これはPythonコミュニティの注目を集めるべきでしょうか?その特定のケースでは、私はPyCharmを使用しなければならなかったので、私はWindows 7マシンを使い終わった。 –

+0

私はまだこの問題について熟知していませんが、解決策を見つけるために検索したさまざまなフォーラムに基づいて、適切に厳格に対処されていない大きなバグのようです。 – Coolio2654

関連する問題