首页 > 解决方案 > 在 python 中(plt.table 在第一行设置 rowColours)

问题描述

嗨,当我想为表格的第一行设置颜色时,我总是会出错。我想创建一个表格并用颜色突出显示第一行。有没有其他方法可以塑造它?编辑这里是我的完整代码

import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import table


#first_page
DATA_FILE = "C:/test/corr.xlsx"

df = pd.read_excel("C:/test/corr.xlsx")
s = df.iloc[0:15, 0:10]

fig, ax = plt.subplots(figsize=(5, 1))
plt.suptitle("1.Excel-Daten", fontsize =15)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)  
ax.set_frame_on(False)  
tabla = table(ax, s, colWidths=[0.30]*len(df.columns), rowColours='r', 
colColours = 'r')
tabla.auto_set_font_size(False) 
tabla.set_fontsize(15)

tabla.scale(1.5,1.5)
plt.show

我的错误是:

runfile('C:/Users/ucarm.lokal/.spyder-py3/untitled17.py', wdir='C:/Users/ucarm.lokal/.spyder-py3') Traceback(最近一次通话最后):

文件“”,第 1 行,在 runfile('C:/Users/ucarm.lokal/.spyder-py3/untitled17.py', wdir='C:/Users/ucarm.lokal/.spyder-py3')

文件“C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py”,第 705 行,运行文件 execfile(文件名,命名空间)

文件“C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py”,第 102 行,在 execfile exec(compile(f.read(), filename, 'exec'), namespace)

文件“C:/Users/ucarm.lokal/.spyder-py3/untitled17.py”,第 25 行,在 tabla = table(ax, s, colWidths=[0.30]*len(df.columns), rowColours='r ', 颜色颜色 = 'r')

文件“C:\Anaconda\lib\site-packages\pandas\plotting_tools.py”,第 65 行,在表 colLabels=colLabels,**kwargs 中)

文件“C:\Anaconda\lib\site-packages\matplotlib\table.py”,第 652 行,表格中 text=colLabels[col],facecolor=colColours[col],

IndexError: string index out of range

标签: pythonpandasmatplotlib

解决方案


推荐阅读