Python 更改使用matplotlib生成的表格单元格中的文字颜色
我读到的所有内容都是关于更改单元格、行标题或列标题的背景。我想更改行的文本颜色。有什么建议吗Python 更改使用matplotlib生成的表格单元格中的文字颜色,python,matplotlib,plot,text,colors,Python,Matplotlib,Plot,Text,Colors,我读到的所有内容都是关于更改单元格、行标题或列标题的背景。我想更改行的文本颜色。有什么建议吗 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'],
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'
)
我不知道是否有办法在施工时做到这一点。但是,您可以在创建后访问各个单元格并修改文本的属性 e、 g:
cell=_表[2,3]
单元格。获取文本()。设置颜色(“红色”)
完整代码:
将numpy导入为np
将matplotlib.pyplot作为plt导入
数据=[[663861742967513157790832015],
[ 58230, 381139, 78045, 99308, 160454],
[ 89135, 80552, 152558, 497981, 603535],
[ 78415, 81858, 150656, 193263, 69638],
[139361, 331509, 343164, 781380, 52269]]
列=(‘冻结’、‘风’、‘洪水’、‘地震’、‘冰雹’)
行=['%d年'%x为x年(100,50,20,10,5)]
数值=np.arange(0,2500500)
值\增量=1000
#为颜色准备一些柔和的色调
颜色=plt.cm.BuPu(np.linspace(0,0.5,len(行)))
n_行=len(数据)
索引=np.arange(列数))+0.3
钢筋宽度=0.4
#初始化堆叠条形图的垂直偏移。
y_偏移=np.零(列)
plt.图()
#打印栏并为表格创建文本标签
单元格文本=[]
对于范围内的行(n_行):
plt.bar(索引,数据[行],条宽,底部=y偏移,颜色=颜色[行])
y_偏移=y_偏移+数据[行]
单元格文本.append([%1.1f%%(x/1000.0)表示y\U偏移量中的x])
#反转颜色和文本标签以在顶部显示最后一个值。
颜色=颜色[:-1]
单元格_text.reverse()
#在轴的底部添加表格
_table=plt.table(cellText=cell_text,
行标签=行,
行颜色=颜色,
colLabels=列,
loc='bottom')
单元格=_表[2,3]
单元格。获取文本()。设置颜色(“红色”)
#调整布局以为桌子腾出空间:
plt.子批次调整(左=0.2,下=0.2)
plt.ylabel(“以${0}为单位的损失”。格式(值增量))
plt.yticks(值*value_增量,['%d'%val代表值中的值])
plt.xticks([])
plt.title(‘灾难损失’)
plt.show()
谢谢!虽然我更喜欢建筑,但我会采取任何有助于实现颜色变化目标的方法。