我一直在使用matplotlib在自动化的pdf报告中呈现各种图表,并具有总体可接受的处理时间。但是,当我在带有两个轴的图表下方添加摘要表时,将图表保存到文件中时性能会大大下降。
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import time
data = [
['2018-01-01', 50.472, 52.2, 45.1305555555556, 50.399,
0.983086079622108, 1.00],
['2018-02-01', 70.907, 77.5, 69.8916666666667, 79.137,
1.01918252884086, 0.98],
['2018-03-01', 86.148, 124.4, 106.855555555556, 116.661,
0.920305088314199, 0.89],
['2018-04-01', 150.314, 152.5, 143.0, 147.185, 0.877857009149397,
None],
['2018-05-01', 162.63, 191.6, 163.786111111111, 173.851,
0.834110398117208, 0.86],
['2018-06-01', 206.60205, 211.6, 208.858333333333, 214.27,
0.846595650655765, 0.91],
['2018-07-01', 97.374, 219.0, 228.866666666667, 232.978,
0.794293255390368, 0.71],
['2018-08-01', 194.064, 186.5, 187.916666666667, 196.659,
0.843036593263637, 0.81],
['2018-09-01', 154.48, 139.1, 150.969444444444, 157.047,
0.866042591545732, 0.82],
['2018-10-01', 85.029, 89.6, 81.4833333333333, 89.923,
0.890580752916001, 0.90],
['2018-11-01', 46.389, 56.7, 45.0388888888889, 49.594,
1.3243466016473, 0.98],
['2018-12-01', 51.132, 46.3, 47.6777777777778, 50.513,
0.904260369658414, 1.08]]
df = pd.DataFrame(data, columns=['day', 'value1',
'value2', 'value3',
'value4',
'value5', 'value6'])
def get_color(column):
# some logic to retrieve a hex color
return '#CCCCCC'
matplotlib.use('agg')
init = time.time()
fig = plt.figure()
ax = plt.subplot(111)
ax2 = ax.twinx()
df.set_index(df['day'])
grouped_columns = ['value1', 'value2', 'value3', 'value4']
grouped_color_list = [get_color(series) for series in grouped_columns]
df[grouped_columns].plot.bar(rot=0, ax=ax, color=grouped_color_list)
for column in ['value5', 'value6']:
ax2.plot(df[column], label=column, color=get_color(column))
ax.xaxis.set_ticks([])
values_df = df.T
table = plt.table(cellText=values_df.values,
rowLabels=df.columns,
colLabels=df.index,
cellLoc='center', rowLoc='center',
loc='bottom')
plotting_time = time.time() - init
print('Chart plot took {:.2f}s'.format(plotting_time))
fig.savefig('output.png', bbox_inches='tight', format='png', dpi=100)
saving_time = time.time() - init
print('savefig took {:.2f}s'.format(saving_time))
这是控制台输出(带有表):
Chart plot took 0.17s
savefig took 4.48s
这是控制台输出(无表):
Chart plot took 0.13s
savefig took 0.62s
在某处我没有正确使用绘图功能吗?