Pyplot 中的空白图

Blank Figure in Pyplot

我使用下面的函数,我得到了 window 中显示的图,但保存的图是空白的。

import matplotlib.pyplot as plt
from sklearn.manifold import TSNE

def plot_embeddings(embeddings, names):
model = TSNE(n_components=2, random_state=0)
vectors = model.fit_transform(embeddings)
x, y = vectors[:, 0], vectors[:, 1]
fig, ax = plt.subplots()
ax.scatter(x, y)
for i, tname in enumerate(names):
    ax.annotate(tname, (x[i], y[i]))
plt.show()
plt.savefig('foo.png', bbox_inches='tight')

我还没有找到有效的解决方案。

show()之前使用savefig()

show() 打开 window 并等到你关闭它,也许当它关闭时 window 然后它会清除图像。