如何分配在Matplotlib表特定颜色的特定细胞?
问题描述:
继pylab_examples,我在matplotlib创建了一个简单的2×细胞表。如何分配在Matplotlib表特定颜色的特定细胞?
代码:
# Prepare table
columns = ('A', 'B', 'C', 'D', 'E')
rows = ["A", "B"]
cell_text = [["1", "1","1","1","1"], ["2","2","2","2","2"]]
# Add a table at the bottom of the axes
ax[4].axis('tight')
ax[4].axis('off')
the_table = ax[4].table(cellText=cell_text,colLabels=columns,loc='center')
现在,我要的颜色单元格A1与color = "#56b5fd"
和单元格A2与color = "#1ac3f5"
。所有其他细胞应保持白色。 Matplotlib的table_demo.py以及this例如只告诉我如何应用彩色地图依赖于细胞中的值预先定义的颜色。
如何分配在Matplotlib生成表的具体颜色以特定细胞?
答
上色在表格单元格的背景最简单的方法是使用cellColours
参数。您可以提供与数据形状相同的列表或阵列列表。
import matplotlib.pyplot as plt
# Prepare table
columns = ('A', 'B', 'C', 'D', 'E')
rows = ["A", "B"]
cell_text = [["1", "1","1","1","1"], ["2","2","2","2","2"]]
# Add a table at the bottom of the axes
colors = [["#56b5fd","w","w","w","w"],[ "#1ac3f5","w","w","w","w"]]
fig, ax = plt.subplots()
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=cell_text,cellColours=colors,
colLabels=columns,loc='center')
plt.show()
替代地,可以设置一个特定小区的facecolor作为
the_table._cells[(1, 0)].set_facecolor("#56b5fd")
the_table._cells[(2, 0)].set_facecolor("#1ac3f5")
在相同的输出如上所得到的。