sud*_*nym 7 python colors matplotlib
在pylab_examples之后,我在matplotlib中创建了一个简单的2x5单元格表.
码:
# 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')
Run Code Online (Sandbox Code Playgroud)
现在,我想用单元格A1 color = "#56b5fd"和单元格A2 着色color = "#1ac3f5".所有其他细胞应保持白色.Matplotlib的table_demo.py以及此示例仅向我展示如何应用具有取决于单元格中的值的预定义颜色的颜色映射.
如何为Matplotlib生成的表中的特定单元格指定特定颜色?
Imp*_*est 12
对表格中单元格背景进行着色的最简单方法是使用该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()
Run Code Online (Sandbox Code Playgroud)
或者,您可以将特定单元格的面颜色设置为
the_table[(1, 0)].set_facecolor("#56b5fd")
the_table[(2, 0)].set_facecolor("#1ac3f5")
Run Code Online (Sandbox Code Playgroud)
产生与上面相同的输出.
| 归档时间: |
|
| 查看次数: |
6957 次 |
| 最近记录: |