在 matplotlib 表格中设置列的背景色

129 阅读3分钟

如何在 matplotlib 表格中设置某一列的背景色?

  • 场景:给定多个文本文件,每个文件包含特定进程的内存使用信息,需要解析这些文件并绘制内存使用情况的折线图,同时在表格中标记 PID 值不同的列。
  1. 解决方案
    • 使用 matplotlib.pyplot.table 函数创建表格。
    • 使用 colColours 参数设置列的颜色。
    • 使用 get_celld() 方法访问特定单元格并设置其颜色。
import matplotlib.pyplot as plt
import numpy as np

# 假设这是从文本文件中解析出来的数据
native_heapsize_arr = [1000, 2000, 3000, 4000, 5000]
dalvik_heapsize_arr = [6000, 7000, 8000, 9000, 10000]
native_heapalloc_arr = [1100, 2100, 3100, 4100, 5100]
dalvik_heapalloc_arr = [6100, 7100, 8100, 9100, 10100]
pid_arr = [100, 200, 100, 200, 100]  # 假设这是与每个数据点对应的 PID

# 创建一个表格数据
tableData = np.asarray([native_heapsize_arr, dalvik_heapsize_arr, native_heapalloc_arr, dalvik_heapalloc_arr, pid_arr], dtype=int)

# 获取当前的 color cycle,然后重置 color cycle 到初始状态
colors = []
while True:
    colors.append(plt.gca()._get_lines.color_cycle.next())
    if colors[0] == colors[-1] and len(colors) > 1:
        colors.pop(-1)
        break

for i in xrange(len(colors) - 1):
    plt.gca()._get_lines.color_cycle.next()

# 创建一个表格
fig, ax = plt.subplots()
the_table = plt.table(cellText=tableData, rowLabels=["Native Heap Size", "Dalvik Heap Size", "Native Heap Allocated", "Dalvik Heap Allocated", "PID"],
                colLabels=["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"],
                loc='bottom', cellColours=colors, colColours=colors[:5])

# 设置 PID 值不同的列的颜色
for i in range(len(pid_arr)):
    if pid_arr[i] != pid_arr[0]:
        c = the_table.get_celld()[(4, i)]
        c.set_color('r')
        c = the_table.get_celld()[(3, i)]
        c.set_color('r')
        c = the_table.get_celld()[(2, i)]
        c.set_color('r')
        c = the_table.get_celld()[(1, i)]
        c.set_color('r')

# 显示表格和折线图
plt.show()
import matplotlib.pyplot as plt
import numpy as np

# 假设这是从文本文件中解析出来的数据
native_heapsize_arr = [1000, 2000, 3000, 4000, 5000]
dalvik_heapsize_arr = [6000, 7000, 8000, 9000, 10000]
native_heapalloc_arr = [1100, 2100, 3100, 4100, 5100]
dalvik_heapalloc_arr = [6100, 7100, 8100, 9100, 10100]
pid_arr = [100, 200, 100, 200, 100]  # 假设这是与每个数据点对应的 PID

# 创建一个表格数据
tableData = np.asarray([native_heapsize_arr, dalvik_heapsize_arr, native_heapalloc_arr, dalvik_heapalloc_arr, pid_arr], dtype=int)

# 获取当前的 color cycle,然后重置 color cycle 到初始状态
colors = []
while True:
    colors.append(plt.gca()._get_lines.color_cycle.next())
    if colors[0] == colors[-1] and len(colors) > 1:
        colors.pop(-1)
        break

for i in xrange(len(colors) - 1):
    plt.gca()._get_lines.color_cycle.next()

# 创建一个表格
fig, ax = plt.subplots()
the_table = ax.table(cellText=tableData, rowLabels=["Native Heap Size", "Dalvik Heap Size", "Native Heap Allocated", "Dalvik Heap Allocated", "PID"],
                colLabels=["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"],
                loc='bottom', cellColours=colors, colColours=colors[:5])

# 设置 PID 值不同的列的颜色
for i in range(len(pid_arr)):
    if pid_arr[i] != pid_arr[0]:
        c = the_table.get_celld()[(4, i)]
        c.set_color('r')
        c = the_table.get_celld()[(3, i)]
        c.set_color('r')
        c = the_table.get_celld()[(2, i)]
        c.set_color('r')
        c = the_table.get_celld()[(1, i)]
        c.set_color('r')

# 显示表格和折线图
plt.show()