我的脚本使用matplotlib.pyplot.subplots生成三个表。附图显示第一个表在顶部部分可见,其他表重叠。我还附上了一个工作代码片段。我需要在每个表的顶部添加一个标题,并在每个表的顶部创建一些空格。我怎么能做到这一点?任何帮助表示赞赏。
import pandas as pd
import numpy as np
import argparse
from pylab import *
import matplotlib.pyplot as plt
import datetime
def _draw_table1( axis):
colLabels = ('', '')
rowLabels = ('Start Date - End Date',
'Total Return since inception',
'Average Annual ROR',
'Winning / Losing Months',
'Winning Month Gain / Losing Month Loss',
'Sharpe Ratio',
'Risk of Ruin',
'Return to Drawdown (Calmar)',
'% Winning / Losing Days [Flat Days]')
data = [['2015-01-01 - 2018-04-27'], ['12.50 %'], [' 3.09 %'], [' 1.35'], [' 2.01 / -1.99 %'], [' 0.34'], [' 0.96'], [' 0.25'], ['50.05 / 49.95 [ 0.00 ]']]
axis.axis('off')
axis.axis('tight')
axis.get_frame_on()
colorscell = [["#D0DDAF"],[ "#E6E9E9"],[ "#D0DDAF"],[ "#E6E9E9"],[ "#D0DDAF"],[ "#E6E9E9"],[ "#D0DDAF"],[ "#E6E9E9"],[ "#D0DDAF"]]
colorsrow = ["#D0DDAF", "#E6E9E9", "#D0DDAF", "#E6E9E9", "#D0DDAF", "#E6E9E9", "#D0DDAF", "#E6E9E9", "#D0DDAF"]
the_table = axis.table(cellText=data,
rowLabels=rowLabels,
colLabels=None,
loc='center right',
cellColours=colorscell,
rowColours=colorsrow,
bbox=[0.3, -0.2, 0.5, 1.5])
table_props = the_table.properties()
def _draw_year_analysis( axis):
rowLabels = ('Annual return',
'Best 21 days',
'Worst 21 days',
'1 year sharpe')
data = [['-5.20 %', '15.83 %', ' 4.10 %' ,'-0.87 %'],
[' 4.79 %', ' 9.12 %', ' 4.25 %', ' 1.70 %'], ['-4.89 %', '-6.87 %' ,'-4.80 %' ,'-2.01 %'],
[-0.49, 1.09, 0.45, -0.4]]
colLabels = [2015, 2016, 2017, 2018]
colorscell = [["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"],[ "#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"],[ "#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"],[ "#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"]]
colorsrow = ["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"]
colorscolumn = ["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"]
axis.axis('off')
the_table = axis.table(cellText=data,
rowLabels=rowLabels,
colLabels= colLabels,
cellColours= colorscell,
colColours= colorscolumn,
rowColours = colorsrow,
loc='left',
bbox=[0.0, -0.1, 1.0, 1.5])
table_props = the_table.properties()
table_cells = table_props['children']
def _draw_drawdowns_table(axis):
data = [['-13.06 %', datetime.date(2015, 1, 7), datetime.date(2016, 2, 22), 351.0],
['-8.80 %', datetime.date(2016, 10, 13), datetime.date(2017, 8, 6), 254.0],
['-7.27 %', datetime.date(2017, 8, 30), datetime.date(2018, 4, 27), 205.0],
['-6.60 %', datetime.date(2016, 4, 8), datetime.date(2016, 6, 22), 65.0],
['-3.67 %', datetime.date(2016, 7, 6), datetime.date(2016, 8, 9), 30.0],
['-3.60 %' ,datetime.date(2016, 8, 16), datetime.date(2016, 9, 30), 40.0]]
colLabels = ('Depth', 'Start', 'End', 'Length (days)')
axis.axis('off')
axis.axis('tight')
colorscell=[]
for i in range(len(data)):
tmpList= []
for x in range(4):
tmpList.append("#D0DDAF")
colorscell.append(tmpList)
colorsrow = ["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"]
colorscolumn = ["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"]
the_table = axis.table(cellText=data,
colLabels=colLabels,
cellColours = colorscell,
loc='right',
bbox=[0.52, -0.2, 0.5, 1.5])
def _draw_day_analysis_table( axis):
days = [1, 5, 21, 63, 126, 252]
rowLabels = list(map(lambda x: str(x)+" days", days))
colLabels = ['Best', 'Worst', 'Avg', 'Curr']
data = [[' 6.38 %', '-2.65 %', ' 0.01 %', ' 1.22 %'], [' 8.36 %', '-4.25 %', ' 0.06 %',
' 0.53 %'], [' 9.12 %', '-6.87 %', ' 0.25 %', ' 0.97 %'], ['13.28 %', '-8.23 %'
, ' 1.05 %', '-1.19 %'], ['17.53 %', '-8.50 %', ' 2.53 %', '-0.08 %'],
['24.12 %', '-8.68 %', ' 6.58 %', ' 0.43 %']]
axis.axis('off')
axis.axis('tight')
colorscell = [["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"],["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"],["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"],["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"],["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"],["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"]]
colorsrow = ["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"]
colorscolumn = ["#D0DDAF","#E6E9E9", "#D0DDAF","#E6E9E9"]
the_table = axis.table(cellText=data,
rowLabels=rowLabels,
colLabels=colLabels,
cellColours = colorscell,
rowColours = colorsrow,
colColours = colorscolumn,
loc='center',
bbox=[0.0, -0.2, 0.5, 1.5])
def plot_returns():
f, axarr = plt.subplots(3)
print(type(axarr) )
print((axarr[0]) )
_draw_table1( axarr[0])
_draw_year_analysis( axarr[1])
_draw_day_analysis_table( axarr[2])
_draw_drawdowns_table(axarr[2])
#f.suptitle('test title', fontsize=20)
plt.subplots_adjust(left=0.1, bottom=0.1,top=1.5)
f.tight_layout()
plt.show()
def main():
plot_returns()
if __name__ == '__main__':
main()