更改使用matplotlib生成的表的单元格中的文本颜色 [英] Changing text color in cells for a table generated with matplotlib

查看:111
本文介绍了更改使用matplotlib生成的表的单元格中的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读并发现的所有内容都与更改单元格,行标题或列标题的背景有关.我想更改一行的文本颜色.有什么建议吗?

  colors = ['tab:blue','tab:orange','tab:green','tab:red','tab:purple']cell_colors = [[''tab:blue','tab:blue'],['tab:orange','tab:orange'],['tab:green','tab:green'],['tab:red','tab:red'],['tab:purple','tab:purple']]table = plt.table(cellText = data,cellColours = cell_colors,rowLabels = row_labels,rowColours =颜色,colLabels = col_labels,loc ='center') 

Everything I have read and found is about changing the background of a cell, row header or column header. I want to change the text color of a row. Any suggestions?

colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple']
cell_colors = [['tab:blue', 'tab:blue'], 
              ['tab:orange', 'tab:orange'],
              ['tab:green', 'tab:green'],
              ['tab:red', 'tab:red'],
              ['tab:purple', 'tab:purple']]
table = plt.table(cellText = data,
                  cellColours = cell_colors,
                  rowLabels=row_labels,
                  rowColours = colors, 
                  colLabels = col_labels, 
                  loc = 'center'
                 )

Color change I can make thus far

解决方案

I don't know if there is a way to do this at construction time. However, you can access the individual cells after creation and modify the properties of the text.

e.g.:

cell = the_table[2,3]
cell.get_text().set_color('red')

full code:

import numpy as np
import matplotlib.pyplot as plt


data = [[ 66386, 174296,  75131, 577908,  32015],
        [ 58230, 381139,  78045,  99308, 160454],
        [ 89135,  80552, 152558, 497981, 603535],
        [ 78415,  81858, 150656, 193263,  69638],
        [139361, 331509, 343164, 781380,  52269]]

columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]

values = np.arange(0, 2500, 500)
value_increment = 1000

# Get some pastel shades for the colors
colors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)))
n_rows = len(data)

index = np.arange(len(columns)) + 0.3
bar_width = 0.4

# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.zeros(len(columns))

plt.figure()
# Plot bars and create text labels for the table
cell_text = []
for row in range(n_rows):
    plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
    y_offset = y_offset + data[row]
    cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])
# Reverse colors and text labels to display the last value at the top.
colors = colors[::-1]
cell_text.reverse()

# Add a table at the bottom of the axes
the_table = plt.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom')


cell = the_table[2,3]
cell.get_text().set_color('red')

# Adjust layout to make room for the table:
plt.subplots_adjust(left=0.2, bottom=0.2)

plt.ylabel("Loss in ${0}'s".format(value_increment))
plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])
plt.title('Loss by Disaster')

plt.show()

这篇关于更改使用matplotlib生成的表的单元格中的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆