【发布时间】:2017-03-08 20:23:01
【问题描述】:
我无法从 matplotlib 在 iPython 中显示图像,图像会在弹出窗口中显示一秒钟而不是内联,然后立即关闭。在 Spyder 内部的 iPython 控制台中什么也没有出现。
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import style
style.use('fivethirtyeight')
xs = np.array([1,2,3,4,5,6], dtype=np.float64)
ys = np.array([5,4,6,5,6,7], dtype=np.float64)
def best_fit_slop_and_intercept (xs,ys):
m = ( ((mean(xs)*mean(ys))-mean(xs*ys)) /
((mean(xs)**2)-mean(xs**2)) )
b = mean(ys)-m*mean(xs)
return m,b
m,b = best_fit_slop_and_intercept(xs,ys)
print (m,b)
regression_line = [m*x + b for x in xs ]
print (regression_line)
predict_x = 9
predict_y = predict_x*m + b
plt.scatter(predict_x,predict_y, color = 'g')
plt.scatter(xs,ys)
plt.plot(xs, regression_line)
plt.show()
【问题讨论】:
-
是否尝试在 ipython 中运行它(没有 spyder)?我没有 spyder,但这段代码对我来说运行良好。 (弹出一个窗口并一直停留到我退出)
标签: python matplotlib ipython