Python大数据与量化交易-1-1-4-matplotlib模块中的style绘图风格
代码主要实现了复利计算和多样式图表生成功能。它通过定义sta001函数计算复利终值,考虑了每年固定投入和复利增长的因素。主程序部分计算了四种不同年利率(5%、10%、15%、20%)下,连续 40 年每年投入 1.4 个单位资金的复利终值,并将结果存储在 DataFrame 中。
最核心的功能是dr_xtyp函数,它会遍历当前 matplotlib 环境中所有可用的样式(如 ggplot、fivethirtyeight 等),为同一组复利数据生成不同风格的图表,并保存为 PNG 文件。这种方式允许用户直观比较不同样式的视觉效果,选择最适合特定场景的图表风格。运行代码后,用户可以在 tmp 文件夹中查看所有生成的图表,每个图表都对应一种 matplotlib 内置样式。
# 导入数值计算库
import numpy as np
# 导入数据处理库
import pandas as pd
# 导入绘图库
import matplotlib as mpl
# 导入绘图接口
import matplotlib.pyplot as plt
# =======================
# 注释掉的样式设置,原计划使用seaborn风格的白色网格
#mpl.style.use('seaborn-whitegrid');
# 复利终值计算函数
def sta001(k, nyear, xd):
# k: 年利率,nyear: 年数,xd: 每年投入金额
# 计算复利终值(未来值),包括定期投入和初始投入
d2 = np.fv(k, nyear, -xd, -xd);
# 四舍五入取整
d2 = round(d2)
# 注释掉的打印语句,可用于调试查看中间结果
#print(nyear,d2)
return d2
# 生成不同matplotlib样式图表的函数
def dr_xtyp(_dat):
# _dat: 输入的DataFrame数据
# 注释掉的预设样式列表
#xtyp=['bmh','dark_background','fivethirtyeight','ggplot','grayscale','default'];
i = 0
# 遍历matplotlib所有可用样式
for xss in plt.style.available:
# 创建新图表
plt.figure()
# 应用当前样式
plt.style.use(xss);
# 绘制数据
_dat.plot()
# 保存图表为PNG文件
fss = "tmp\\k101_" + xss + ".png"; plt.savefig(fss);
i += 1
# 输出进度信息
print(i, xss, ",", fss)
# 显示图表
plt.show()
# =======================
# 计算不同年利率下的复利终值(5%)
dx05 = [sta001(0.05, x, 1.4) for x in range(0, 40)]
# 计算不同年利率下的复利终值(10%)
dx10 = [sta001(0.10, x, 1.4) for x in range(0, 40)]
# 计算不同年利率下的复利终值(15%)
dx15 = [sta001(0.15, x, 1.4) for x in range(0, 40)]
# 计算不同年利率下的复利终值(20%)
dx20 = [sta001(0.20, x, 1.4) for x in range(0, 40)]
# 创建DataFrame存储不同利率的计算结果
df = pd.DataFrame(columns=['dx05', 'dx10', 'dx15', 'dx20']);
df['dx05'] = dx05; df['dx10'] = dx10;
df['dx15'] = dx15; df['dx20'] = dx20;
# 打印最后几行数据
print(df.tail())
# 生成不同matplotlib样式的图表
dr_xtyp(df)
runfile('D:/zwPython/zwrk/6_零起点Python机器学习与量化交易/k101sty.py', wdir='D:/zwPython/zwrk/6_零起点Python机器学习与量化交易')
dx05 dx10 dx15 dx20
35 134.0 419.0 1420.0 4955.0
36 142.0 462.0 1634.0 5947.0
37 151.0 510.0 1881.0 7138.0
38 160.0 562.0 2165.0 8567.0
39 169.0 620.0 2491.0 10281.0
1 Solarize_Light2 , tmp\k101_Solarize_Light2.png
D:\zwPython\zwrk\6_零起点Python机器学习与量化交易\k101sty.py:13: DeprecationWarning: numpy.fv is deprecated and will be removed from NumPy 1.20. Use numpy_financial.fv instead (https://pypi.org/project/numpy-financial/).
d2=np.fv(k,nyear,-xd,-xd);
<Figure size 432x288 with 0 Axes>
2 _classic_test_patch , tmp\k101__classic_test_patch.png
<Figure size 432x288 with 0 Axes>
3 bmh , tmp\k101_bmh.png
<Figure size 432x288 with 0 Axes>
4 classic , tmp\k101_classic.png
<Figure size 432x288 with 0 Axes>
5 dark_background , tmp\k101_dark_background.png
<Figure size 640x480 with 0 Axes>
6 fast , tmp\k101_fast.png
<Figure size 640x480 with 0 Axes>
7 fivethirtyeight , tmp\k101_fivethirtyeight.png
<Figure size 640x480 with 0 Axes>
8 ggplot , tmp\k101_ggplot.png
<Figure size 640x480 with 0 Axes>
9 grayscale , tmp\k101_grayscale.png
<Figure size 640x480 with 0 Axes>
10 seaborn , tmp\k101_seaborn.png
<Figure size 640x480 with 0 Axes>
11 seaborn-bright , tmp\k101_seaborn-bright.png
<Figure size 640x440 with 0 Axes>
12 seaborn-colorblind , tmp\k101_seaborn-colorblind.png
<Figure size 640x440 with 0 Axes>
13 seaborn-dark , tmp\k101_seaborn-dark.png
<Figure size 640x440 with 0 Axes>
14 seaborn-dark-palette , tmp\k101_seaborn-dark-palette.png
<Figure size 640x440 with 0 Axes>
15 seaborn-darkgrid , tmp\k101_seaborn-darkgrid.png
<Figure size 640x440 with 0 Axes>
16 seaborn-deep , tmp\k101_seaborn-deep.png
<Figure size 640x440 with 0 Axes>
17 seaborn-muted , tmp\k101_seaborn-muted.png
<Figure size 640x440 with 0 Axes>
18 seaborn-notebook , tmp\k101_seaborn-notebook.png
<Figure size 640x440 with 0 Axes>
19 seaborn-paper , tmp\k101_seaborn-paper.png
<Figure size 640x440 with 0 Axes>
20 seaborn-pastel , tmp\k101_seaborn-pastel.png
<Figure size 512x352 with 0 Axes>
21 seaborn-poster , tmp\k101_seaborn-poster.png
<Figure size 512x352 with 0 Axes>
22 seaborn-talk , tmp\k101_seaborn-talk.png
<Figure size 1024x704 with 0 Axes>
23 seaborn-ticks , tmp\k101_seaborn-ticks.png
<Figure size 832x572 with 0 Axes>
24 seaborn-white , tmp\k101_seaborn-white.png
<Figure size 832x572 with 0 Axes>
25 seaborn-whitegrid , tmp\k101_seaborn-whitegrid.png
<Figure size 832x572 with 0 Axes>
26 tableau-colorblind10 , tmp\k101_tableau-colorblind10.png
<Figure size 832x572 with 0 Axes>