[英]Plot doesn't show in PyCharm but shows in Terminal in OSX terminal
我的代碼在OSX El Capitan的Terminal中繪制圖,但在PyCharm中未繪制圖。 Terminal和PyCharm解釋器都設置為Anaconda Python 3.6.2,並已安裝了所有必需的軟件包。
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中的解決方案未顯示出圖,而且都沒有真正起作用。 例如,在plt.show(block=True)
之后添加以下行並不能解決問題。
matplotlib.get_backend()
plt.interactive(False)
plt.figure()
我最近遇到了幾乎相同的煩惱,並且還嘗試了其他地方提到的matplotlib / etc的其他解決方案。 看到Terminal(也就是常規的python / ipython)運行這些圖時,我發現PyCharm中必須有一種變通辦法來運行純控制台,這樣可以避免越野車的開銷。 好吧,按照我發現的第一個答案的建議,您只需訪問PyCharm中的Python控制台,它就可以正常輸出繪圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.