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

yumo6661周前 (05-16)技术文章16

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

相关文章

DAY4-step5 Python示例说明 round()函数

Round()Round()是python提供的内置函数。 它将返回一个浮点数,该浮点数将四舍五入到指定的精度。如果未指定要舍入的小数位,则将其视为0,并将舍入到最接近的整数。语法:round(flo...

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

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

Python 中限制浮点数为两位小数的方法

在 Python 编程里,我们常常会碰到需要把浮点数限制为两位小数的情况。无论是处理财务数据,还是输出需要特定精度的结果,掌握相关方法都很有必要。下面就为大家详细介绍几种实现这一目标的方法。方法一:使...

全国计算机等级考试二级Python易错真题详解-流程控制-单选题

流程控制(单选题)说明:本文中的题目,全都来自全国计算机等级考试二级Python语言程序设计考试的真题,且都为易错题。题干最后如有编号,则是 python123 平台上的题号,以方便学生查找和索引。在...

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

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

python基础函数

Python 函数是代码复用的核心工具,掌握基础函数的使用是编程的关键。以下是 Python 函数的系统总结,包含 内置函数 和 自定义函数 的详细用法,以及实际应用场景。一、Python 内置函数(...