matplotlib - 在 matplotlib.axes.Axes.table 中设置 alpha?
问题描述
我试图了解如何在 matplotlib 表中设置 alpha 级别。我尝试使用全局 rcParams 设置它,但不太确定该怎么做?(我想更改标题颜色的透明度)。一般来说,我不确定这可以在全球范围内完成,如果不能,我如何将参数传递给表?提前谢谢。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler
import six
# Set universal font and transparency
plt.rcParams["font.family"] = "DejaVu Sans"
plt.rcParams['axes.prop_cycle'] = cycler(alpha=[0.5])
raw_data = dict(Simulation=[42, 39, 86, 15, 23, 57],
SP500=[52, 41, 79, 80, 34, 47],
NASDAQ=[62, 37, 84, 51, 67, 32],
Benchmark=[72, 43, 36, 26, 53, 88])
df = pd.DataFrame(raw_data, index=pd.Index(
['Sharpe Ratio', 'Sortino Ratio', 'Calmars Ratio', 'VaR', 'CVaR', 'Max DD'], name='Metric'),
columns=pd.Index(['Simulation', 'SP500', 'NASDAQ', 'Benchmark'], name='Series'))
def create_table(data, col_width=None, row_height=None, font_size=None,
header_color='#000080', row_colors=None, edge_color='w',
header_columns=0, ax=None, bbox=None):
if row_colors is None:
row_colors = ['#D8D8D8', 'w']
if bbox is None:
bbox = [0, 0, 1, 1]
if ax is None:
size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
fig, ax = plt.subplots(figsize=size)
ax.axis('off')
ax.axis([0, 1, data.shape[0], -1])
data_table = ax.table(cellText=data.values, colLabels=data.columns, rowLabels=data.index,
bbox=bbox, cellLoc='center', rowLoc='left', colLoc='center',
colWidths=([col_width] * len(data.columns)))
cell_map = data_table.get_celld()
for i in range(0, len(data.columns)):
cell_map[(0, i)].set_height(row_height * 0.20)
data_table.auto_set_font_size(False)
data_table.set_fontsize(font_size)
for k, cell in six.iteritems(data_table._cells):
cell.set_edgecolor(edge_color)
if k[0] == 0 or k[1] < header_columns:
cell.set_text_props(weight='bold', color='w')
cell.set_facecolor(header_color)
else:
cell.set_facecolor(row_colors[k[0] % len(row_colors)])
return ax
ax = create_table(df, col_width=1.5, row_height=0.5, font_size=8)
ax.set_title("Risk Measures", fontweight='bold')
ax.axis('off')
plt.tight_layout()
plt.savefig('risk_parameter_table[1].pdf')
plt.show()
解决方案
推荐阅读
- kotlin - 我们什么时候使用launch(SupervisorJob())?
- javascript - Vuejs如何将日期与列表中的过滤器进行比较
- python - 如何在 ubuntu 18.04 上为已安装的 python 3.8.1 安装 numpy
- matlab - 不清楚如何对该函数进行矢量化以消除 Matlab 中的 fsurf 警告
- javascript - Select2:使用引导网格
- java - 为什么扫描仪在输出之前先读取?
- python - 获取当前收件人的电子邮件,Django
- validation - 混淆矩阵完美的结果。这可能是真的吗?
- react-native - 在命令行上运行 react-native run-ios 时出错
- angular - 在 angualr 中发送 multipart/form-data POST API 请求