本文介绍了Matplotlib表行标签字体的颜色和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定下表:
将matplotlib.pyplot导入为plttable=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], # 行数据值rowLabels=['1','2','3','4','5'],cellLoc ="left",rowLoc ='左',bbox=[0,0,.2,1], # [left,bottom,width,height]edges =")
我想将数字(1-5)的颜色更改为灰色,并将字体大小更改为12磅.
解决方案
您需要获取单元格的文本字体属性:
将matplotlib.pyplot导入为plttable = plt.table(cellText = ['',','',',','],#行数据值rowLabels=['1','2','3','4','5'],cellLoc ="left",rowLoc ='左',bbox = [0,0,.2,1],#[左,底,宽,高]edges =")# 遍历表格的单元格table_props = table.properties()table_cells = table_props['child_artists']对于 table_cells 中的单元格:cell.get_text().set_fontsize(20)cell.get_text().set_color('灰色')plt.show()
另一种获取单元格文本属性的方法是使用单元格索引(i, j):
table[(i, j)].get_text().set_fontsize(12)table[(i, j)].get_text().set_color('red')
Matplotlib 文本字体属性描述如下:
Given the following table:
import matplotlib.pyplot as plt
table=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], # rows of data values
rowLabels=['1','2','3','4','5'],
cellLoc="left",
rowLoc='left',
bbox=[0,0,.2,1], # [left,bottom,width,height]
edges="")
I'd like to change the color of the numbers (1-5) to grey and the font size to 12 point.
解决方案
You need to get text font properties of the cells:
import matplotlib.pyplot as plt
table=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], #rows of data values
rowLabels=['1','2','3','4','5'],
cellLoc="left",
rowLoc='left',
bbox=[0,0,.2,1],#[left,bottom,width,height]
edges="")
# iterate through cells of a table
table_props = table.properties()
table_cells = table_props['child_artists']
for cell in table_cells:
cell.get_text().set_fontsize(20)
cell.get_text().set_color('grey')
plt.show()
Another method to get text properties of the cell is used cell indexes (i, j):
table[(i, j)].get_text().set_fontsize(12)
table[(i, j)].get_text().set_color('red')
Matplotlib text font properties are described here: http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties
As a result, the first code draw this figure:
这篇关于Matplotlib表行标签字体的颜色和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!