绘图不显示在PyCharm中,但在OSX终端的终端中显示
问题描述:
我的代码在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()
答
我最近遇到几乎相同的irritance,并且还曾试图处处提到matplotlib /等其他解决方案。看到终端(又名,普通的python/ipython)运行这些情节,我认为PyCharm必须有一个解决方法来运行一个纯粹的控制台,以避免错误的开销。那么,遵循我发现的第一个answer's建议,您可以简单地访问PyCharm中的Python控制台,并且它通常输出图。
这应该引起Python社区的注意吗?在那个特定的情况下,我最终使用我的Windows 7机器,因为我不得不使用PyCharm。 –
我并不是这方面的专家,但它似乎确实是一个重大缺陷,没有以足够严谨的方式解决,基于我寻找解决方案的不同论坛。 – Coolio2654