Python大数据与量化交易-1-1-4-matplotlib模块中的style绘图风格

yumo6663个月前 (05-16)技术文章65

代码主要实现了复利计算和多样式图表生成功能。它通过定义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>

相关文章

Python常用函数整理

以下是Python中常用函数整理,涵盖内置函数、标准库及常用操作,按类别分类并附带示例说明:一、基础内置函数print()输出内容到控制台。pythonprint("Hello, World!...

Python语言的12个基础知识点小结

python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序、去重、字典排序、字典、列表、字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出,进...

Python浮点数保留两位小数的方法

技术背景在Python编程中,经常会遇到需要将浮点数保留特定小数位数的情况,比如在处理货币、统计数据等场景。然而,由于浮点数在计算机中采用二进制表示,存在精度问题,导致直接使用round函数有时无法得...

深入剖析Python基本函数:从基础到进阶的完整指南

引言Python作为一门简洁高效的编程语言,其函数系统是支撑代码模块化的核心机制。掌握Python函数的使用方法不仅能提升代码的可读性和复用性,还能帮助开发者理解面向对象编程和函数式编程的精髓。本文将...

8-Python内置函数

Python 提供了丰富的内置函数,这些函数可以直接使用而无需导入任何模块。以下是一些常用的内置函数及其示例:1-print()1-1-说明输出指定的信息到控制台。1-2-例子2-len()2-1-说...

Python 最常用的语句、函数有哪些?

1. #coding=utf-8① 代码中有中文字符,最好在代码前面加#coding=utf-8② pycharm不加可能不会报错,但是代码最终是会放到服务器上,放到服务器上的时候运行可能会报错。③...