星驰编程网

免费编程资源分享平台_编程教程_代码示例_开发技术文章

Python大数据与量化交易-1-1-5-matplotlib模块的colormap颜色表

代码主要实现了复利计算和数据可视化功能。它通过定义sta001函数计算复利终值,考虑了每年固定投入和复利增长的因素。主程序部分计算了四种不同年利率(5%、10%、15%、20%)下,连续 40 年每年投入 1.4 个单位资金的复利终值,并将结果存储在 DataFrame 中。最核心的功能是dr_cmap函数,它从配置文件中读取颜色映射方案,为同一组复利数据生成不同颜色风格的图表,并保存为 PNG 文件。这种方式允许用户直观比较不同颜色配置的视觉效果,选择最适合特定场景的图表风格。运行代码后,用户可以在 tmp 文件夹中查看所有生成的图表,每个图表都对应一种颜色映射方案。整体来看,这是一个展示复利效应和数据可视化的实用程序。

# 导入数值计算库
import numpy as np
# 导入数据处理库
import pandas as pd

# 导入绘图库
import matplotlib as mpl
# 导入绘图接口
import matplotlib.pyplot as plt

# =======================
# 设置matplotlib使用seaborn风格的白色网格
mpl.style.use('seaborn-whitegrid');

# 复利终值计算函数
def sta001(k, nyear, xd):
    # k: 年利率,nyear: 年数,xd: 每年投入金额
    # 计算复利终值(未来值),包括定期投入和初始投入
    d2 = np.fv(k, nyear, -xd, -xd);
    # 四舍五入取整
    d2 = round(d2)
    
    return d2

# 生成不同颜色映射图表的函数
def dr_cmap(_dat):
    # _dat: 输入的DataFrame数据
    # 读取颜色映射配置文件
    fss = 'dat\\cor_maps.csv'
    cm8 = pd.read_csv(fss, encoding='gbk') 
    i = 0
    # 遍历所有颜色映射方案
    for xss in cm8['name']:
        # 创建新图表
        plt.figure()
        # 使用当前颜色映射绘制数据
        _dat.plot(colormap=xss)
        # 保存图表为PNG文件
        fss = "tmp\\k101cor_" + 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())

# 生成不同颜色映射的图表
dr_cmap(df)
runfile('D:/zwPython/zwrk/6_零起点Python机器学习与量化交易/k101cmap.py', wdir='D:/zwPython/zwrk/6_零起点Python机器学习与量化交易')
D:\zwPython\zwrk\6_零起点Python机器学习与量化交易\k101cmap.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);
     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 Blues , tmp\k101cor_Blues.png
2 BrBG , tmp\k101cor_BrBG.png
3 BuGn , tmp\k101cor_BuGn.png
4 BuPu , tmp\k101cor_BuPu.png
5 CMRmap , tmp\k101cor_CMRmap.png
6 Dark2 , tmp\k101cor_Dark2.png
7 GnBu , tmp\k101cor_GnBu.png
8 Greens , tmp\k101cor_Greens.png
9 Greys , tmp\k101cor_Greys.png
10 OrRd , tmp\k101cor_OrRd.png
11 Oranges , tmp\k101cor_Oranges.png
D:\zwPython\zwrk\6_零起点Python机器学习与量化交易\k101cmap.py:24: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  plt.figure()
d:\zwpython\py37\python-3.7.6.amd64\lib\site-packages\pandas\plotting\_matplotlib\core.py:320: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  fig = self.plt.figure(figsize=self.figsize)
12 PRGn , tmp\k101cor_PRGn.png
13 Paired , tmp\k101cor_Paired.png
14 Pastel1 , tmp\k101cor_Pastel1.png
15 Pastel2 , tmp\k101cor_Pastel2.png
16 PiYG , tmp\k101cor_PiYG.png
17 PuBu , tmp\k101cor_PuBu.png
18 PuBu , tmp\k101cor_PuBu.png
19 PuBuGn , tmp\k101cor_PuBuGn.png
20 PuOr , tmp\k101cor_PuOr.png
21 PuRd , tmp\k101cor_PuRd.png
22 Purples , tmp\k101cor_Purples.png
23 RdBu , tmp\k101cor_RdBu.png
24 RdGy , tmp\k101cor_RdGy.png
25 RdPu , tmp\k101cor_RdPu.png
26 RdYlBu , tmp\k101cor_RdYlBu.png
27 RdYlGn , tmp\k101cor_RdYlGn.png
28 Reds , tmp\k101cor_Reds.png
29 Set1 , tmp\k101cor_Set1.png
30 Set2 , tmp\k101cor_Set2.png
31 Set3 , tmp\k101cor_Set3.png
32 Spectral , tmp\k101cor_Spectral.png
33 Wistia , tmp\k101cor_Wistia.png
34 YlGn , tmp\k101cor_YlGn.png
35 YlGn , tmp\k101cor_YlGn.png
36 YlGnBu , tmp\k101cor_YlGnBu.png
37 YlOrBr , tmp\k101cor_YlOrBr.png
38 YlOrRd , tmp\k101cor_YlOrRd.png
39 Accent , tmp\k101cor_Accent.png
40 afmhot , tmp\k101cor_afmhot.png
41 autumn , tmp\k101cor_autumn.png
42 binary , tmp\k101cor_binary.png
43 bone , tmp\k101cor_bone.png
44 brg , tmp\k101cor_brg.png
45 bwr , tmp\k101cor_bwr.png
46 cool , tmp\k101cor_cool.png
47 coolwarm , tmp\k101cor_coolwarm.png
48 copper , tmp\k101cor_copper.png
49 cubehelix , tmp\k101cor_cubehelix.png
50 flag , tmp\k101cor_flag.png
51 gist_earth , tmp\k101cor_gist_earth.png
52 gist_gray , tmp\k101cor_gist_gray.png
53 gist_heat , tmp\k101cor_gist_heat.png
54 gist_ncar , tmp\k101cor_gist_ncar.png
55 gist_stern , tmp\k101cor_gist_stern.png
56 gist_yarg , tmp\k101cor_gist_yarg.png
57 gnuplot , tmp\k101cor_gnuplot.png
58 gnuplot , tmp\k101cor_gnuplot.png
59 gnuplot2 , tmp\k101cor_gnuplot2.png
60 gray , tmp\k101cor_gray.png
61 hot , tmp\k101cor_hot.png
62 hsv , tmp\k101cor_hsv.png
63 inferno , tmp\k101cor_inferno.png
64 jet , tmp\k101cor_jet.png
65 magma , tmp\k101cor_magma.png
66 nipy_spectral , tmp\k101cor_nipy_spectral.png
67 ocean , tmp\k101cor_ocean.png
68 pink , tmp\k101cor_pink.png
69 plasma , tmp\k101cor_plasma.png
70 prism , tmp\k101cor_prism.png
71 rainbow , tmp\k101cor_rainbow.png
72 seismic , tmp\k101cor_seismic.png
73 spring , tmp\k101cor_spring.png
74 summer , tmp\k101cor_summer.png
75 terrain , tmp\k101cor_terrain.png
76 viridis , tmp\k101cor_viridis.png
77 winter , tmp\k101cor_winter.png
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
<Figure size 832x572 with 0 Axes>
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言