怎樣更改 Matplotlib 表格的透明度/不透明度?


要更改 matplotlib 表格的透明度/不透明度,我們可以採取以下步驟

步驟

  • 設定圖片大小並調整子圖間和周圍的填充。

  • 建立圖片和一組子圖。

  • 使用 10×3 尺寸建立隨機資料集。

  • 建立元組列。

  • 使用軸(‘關閉’)擺脫軸標記。

  • 使用資料和列建立表格。

  • 迭代表格的每個單元格並使用 set_alpha() 方法更改其透明度/不透明度。

  • 要顯示圖片,請使用Show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, axs = plt.subplots(1, 1)
data = np.random.random((10, 3))
columns = ("Column I", "Column II", "Column III")

axs.axis('off')

the_table = axs.table(cellText=data, colLabels=columns, loc='center')

for k, cell in the_table._cells.items():
    cell.set_edgecolor('black')
    if k[0] == 0 or k[1] < 0:
        cell.set_text_props(weight='bold', color='w')
        cell.set_facecolor('red')
        cell.set_alpha(0.5)
    else:
        cell.set_facecolor(['green', 'yellow'][k[0] % 2])
        cell.set_alpha(0.5)

plt.show()

輸出

這將產生如下輸出 −

更新於: 09-Oct-2021

537 瀏覽量

開啟你的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.