简体   繁体   English

剧情在PyCharm中不显示,但在OSX终端的终端中显示

[英]Plot doesn't show in PyCharm but shows in Terminal in OSX terminal

My code draws a plot in Terminal in OSX El Capitan but not in PyCharm. 我的代码在OSX El Capitan的Terminal中绘制图,但在PyCharm中未绘制图。 Both Terminal and PyCharm interpreters are set to Anaconda Python 3.6.2 and have the necessary packages all installed. 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)

Also, I tried the solutions in Pycharm does not show plot and none of them really worked. 另外,我尝试了在Pycharm中的解决方案未显示出图,而且都没有真正起作用。 For example, adding the following lines after plt.show(block=True) didn't solve the problem. 例如,在plt.show(block=True)之后添加以下行并不能解决问题。

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

I recently encountered pretty much the same irritance, and had also tried the other solutions mentioned everywhere for matplotlib/etc. 我最近遇到了几乎相同的烦恼,并且还尝试了其他地方提到的matplotlib / etc的其他解决方案。 Seeing as Terminal (aka, regular python/ipython) runs these plots, I figured that there must be a workaround in PyCharm to run a pure console that would avoid the buggy overhead. 看到Terminal(也就是常规的python / ipython)运行这些图时,我发现PyCharm中必须有一种变通办法来运行纯控制台,这样可以避免越野车的开销。 Well, following the first answer's advice I found, you can simply access the Python Console that is in PyCharm, and it outputs plots normally. 好吧,按照我发现的第一个答案的建议,您只需访问PyCharm中的Python控制台,它就可以正常输出绘图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM