Matplotlib表行标签字体的颜色和大小 - python

给出下表:

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="")

Matplotlib表行标签字体的颜色和大小 - python

我想将数字(1-5)的颜色更改为灰色,并将字体大小更改为12点。

python大神给出的解决方案

您需要获取单元格的文本字体属性:

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()

获取单元格文本属性的另一种方法是使用单元格索引(i,j):

table[(i, j)].get_text().set_fontsize(12)
table[(i, j)].get_text().set_color('red')

Matplotlib文本字体属性在此处描述:http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties

结果,第一个代码绘制了此图:
Matplotlib表行标签字体的颜色和大小 - python

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

Python:同时在for循环中添加到列表列表 - python

我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…

如何在Matplotlib条形图后面绘制网格线 - python

x = ['01-02', '02-02', '03-02', '04-02', '05-02'] y = [2, 2, 3, 7, 2] fig, ax = plt.subplots(1, 1) ax.bar(range(len(y)), y, width=…

在Python中迭代OrderedDict - python

我有以下OrderedDict:OrderedDict([('r', 1), ('s', 1), ('a', 1), ('n', 1), ('y', 1)]) 实际上,这表示单词中字母的出现频率。第一步-我将使用最后两个元素来创建一个这样的联合元组; pair…

在独立的scrapy脚本中使用自定义中间件 - python

我正在编写一个实现自定义下载器中间件的独立抓取脚本(update.py)。该脚本当前使用记录在here和here中的CrawlerProcess()API。看起来像这样:from scrapy.crawler import CrawlerProcess import scrapy class CustomMiddleware(object): .... cu…