学习笔记系列-Python量化交易(1)-matplotlib库plot后图像不显示问题

最近买了本书《零起点python大数据与量化交易》,第一个例子(如下)[操作系统mint linux 18 Mate, python3]:

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt


# =======================
mpl.style.use('seaborn-whitegrid')


def sta001(k, nyear, xd):
    d2 = np.fv(k, nyear, -xd, -xd)
    d2 = round(d2)
    return d2


# =======================


d40 = 1.4 * 40
print("d40,40 x 1.4=", d40)
d = sta001(0.05, 40 - 1, 1.4)
print("01保守投资模式,", d, round(d / d40))

d2 = sta001(0.20, 40 - 1, 1.4)
print("02激进投资模式,", d2, round(d2 / d40))

dk = round(d2 / d)
print("dk,两者差别(xx倍):", dk)


dx05 = [sta001(0.05, x, 1.4) for x in range(0, 40)]
dx10 = [sta001(0.10, x, 1.4) for x in range(0, 40)]
dx15 = [sta001(0.15, x, 1.4) for x in range(0, 40)]
dx20 = [sta001(0.20, x, 1.4) for x in range(0, 40)]
# print(dx05)print(dx20)

df = pd.DataFrame(columns=['dx05', 'dx10', 'dx15', 'dx20'])
df['dx05'] = dx05
df['dx10'] = dx10
df['dx15'] = dx15
df['dx20'] = dx20
print("")
print(df.tail())
df.plot()

df.plot后结果如图:

学习笔记系列-Python量化交易(1)-matplotlib库plot后图像不显示问题_第1张图片

曲线图没显示, why?
google一下,少了plt.show()

......

df['dx20'] = dx20
print("")
print(df.tail())
df.plot()
plt.show() 

加了还是没反应?继续google, 结果是matplot backend设置问题,
在ipython里输入如下代码,得到系统支持的backend:
学习笔记系列-Python量化交易(1)-matplotlib库plot后图像不显示问题_第2张图片
到python安装目录下site-packages/matplotlib/mpl-data, 打开matplotlibrc, 搜索backend,默认设置是agg, 果然不在支持列表里,果断改为TkAgg. 再次运行代码,结果如下:

学习笔记系列-Python量化交易(1)-matplotlib库plot后图像不显示问题_第3张图片

你可能感兴趣的:(学习笔记系列)