2017-10-06 145 views
0

我的代碼在OSX El Capitan的終端中繪製了一個繪圖,但是沒有在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 /等其他解決方案。看到終端(又名,普通的python/ipython)運行這些情節,我認爲PyCharm必須有一個解決方法來運行一個純粹的控制檯,以避免錯誤的開銷。那麼,遵循我發現的第一個answer's建議,您可以簡單地訪問PyCharm中的Python控制檯,並且它通常輸出圖。

+0

這應該引起Python社區的注意嗎?在那個特定的情況下,我最終使用我的Windows 7機器,因爲我不得不使用PyCharm。 –

+0

我並不是這方面的專家,但它似乎確實是一個重大缺陷,沒有以足夠嚴謹的方式解決,基於我尋找解決方案的不同論壇。 – Coolio2654