如何為 Matplotlib 表格中的特定單元格分配特定顏色?


為 Matplotlib 表格中的特定單元格分配特定顏色,我們可以採取以下步驟:

  • 設定圖形大小,並調整子圖間和周圍的填充。
  • 屬性建立一個元組。
  • 建立一個列表列表,即記錄列表。
  • 建立一個列表列表,即每個單元格的顏色。
  • 建立一個圖形和一組子圖。
  • 在軸ax 中新增一個表。
  • 關閉軸。
  • 要顯示圖形,請使用show() 方法。

示例

import matplotlib.pyplot as plt

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

columns = ('name', 'age', 'marks', 'salary')

cell_text = [["John", "23", "98", "234"],
["James", "24", "90", "239"]]

colors = [["red", "yellow", "blue", "green"],
["blue", "green", "yellow", "red"]]

fig, ax = plt.subplots()

the_table = ax.table(cellText=cell_text, cellColours=colors,
                     colLabels=columns, loc='center')
ax.axis('off')

plt.show()

輸出

更新於: 2021 年 6 月 15 日

2K+ 次檢視

開啟你的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.