Python: создание меток столбцов / строк таблицы matplotlib жирным шрифтом - PullRequest
0 голосов
/ 20 сентября 2018

Я пытаюсь выяснить, как полужирный метки столбца и строки для таблицы matplotlib, которую я создаю.

Я просмотрел различные свойства таблицы иЯ могу понять, как стилизовать отдельные ячейки, но не фактические столбцы или метки строк.

Кроме того, я не могу выяснить, как выделять жирным шрифтом что-либо ... только размер шрифта, фактический шрифт ицвет.

Любая помощь?

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

fig, axs =plt.subplots(figsize = (10,6))
clust_data = np.random.random((10,3))
collabel=("col 1", "col 2", "col 3")
axs.axis('tight')
axs.axis('off')

df = pd.DataFrame(np.random.randn(10, 4), 
                  columns=['a','b','c','d'], 
                  index = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])

table = axs.table(cellText=df.values, colLabels = df.columns, rowLabels = df.index,  loc='center')

plt.show()

РЕДАКТИРОВАТЬ:

Понял, хотя это немного неуклюже.Вы можете найти метки столбцов / строк в свойстве "celld".Затем вы можете установить его жирным шрифтом, используя .set_text_props (fontproperties = FontProperties (weight = 'bold'). Т.е.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import pandas as pd

fig, axs =plt.subplots(figsize = (10,6))
clust_data = np.random.random((10,3))
collabel=("col 1", "col 2", "col 3")
axs.axis('tight')
axs.axis('off')

df = pd.DataFrame(np.random.randn(10, 4), 
                  columns=['a','b','c','d'], 
                  index = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])


table = axs.table(cellText=df.values, colLabels = df.columns, rowLabels = df.index,  loc='center')


P = []

for key, cell in table.get_celld().items():
    row, col = key
    P.append(cell)

for x in P[40:]:
    x.set_text_props(fontproperties=FontProperties(weight='bold'))

plt.show()

1 Ответ

0 голосов
/ 21 сентября 2018

Немного лучший метод, следуя документации :

from matplotlib.font_manager import FontProperties

for (row, col), cell in table.get_celld().items():
  if (row == 0) or (col == -1):
    cell.set_text_props(fontproperties=FontProperties(weight='bold'))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...