python - PyPlot 不绘制图像

标签 python

我创建了以下测试代码,代码运行良好。但执行时剧情并没有出现。我错过了什么?我使用 pyplot 来创建绘图。当我使用 plt.savefig("test.png") 时,会创建并保存图表。

import numpy
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
from studentRegression import studentReg
from class_vis import prettyPicture, output_image
from ages_net_worth import ageNetWorthData

ages_train, ages_test, net_worths_train, net_worth_test = ageNetWorthData()

plt.clf()
plt.scatter(ages_train, net_worths_train, color="b", label="train data")
plt.legend(loc=2)
plt.xlabel("ages")
plt.ylabel("net worths")
plt.show()


def ageNetWorthData():

    random.seed(42)
    numpy.random.seed(42)

    ages = []
    for ii in range(100):
        ages.append( random.randint(20,65) )
    net_worths = [ii * 6.25 + numpy.random.normal(scale=40.) for ii in ages]
### need massage list into a 2d numpy array to get it to work in LinearRegression
    ages       = numpy.reshape( numpy.array(ages), (len(ages), 1))
    net_worths = numpy.reshape( numpy.array(net_worths), (len(net_worths), 1))

    from sklearn.cross_validation import train_test_split
    ages_train, ages_test, net_worths_train, net_worths_test = train_test_split(ages, net_worths)

    return ages_train, ages_test, net_worths_train, net_worths_test

最佳答案

您正在使用“非交互式”后端 (agg)。只需删除该行:

matplotlib.use('agg')

您可以查看文档 here .

关于python - PyPlot 不绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34492616/

相关文章:

python - 为什么 Django 日志不能与 Gunicorn 一起使用?

python - 大量在线对话文本的情感分析

python - 无法从多个 NumPy 数组中检索所需的索引

python - 使用 Python 创建动态更新图

Python - 一行 if-elif-else 语句

python - 如何为具有物理限制的随机点创建修改后的 voronoi 算法

python - 合并 2 个表并汇总数据

python - 这是 python eval() 的安全使用吗?

python - 如何使用 ElementTree 解析 HTML 以查找特定的 RegEx?

python - 使用 ctypes 将 python 字符串传递给 Fortran 子例程