如何旋转pyplot.table中的列标题? [英] How can I rotate column titles in pyplot.table?
问题描述
我在 matplotlib 中创建了一个表,但表头是长字符串,表值是只有几位数字的数字.这给我留下了两个不好的选择:我的表比必要的宽得多,或者我的标题重叠.要解决此问题,我想旋转表格标题(可能最多旋转90度).换句话说,我想在python中使用
我知道了.它不漂亮,但它有效.我为每列添加了两个注释-文本,以及一行将其与下一个列标题分开的行.我必须定义一些适用于表格和花哨标签(宽度、高度、col_width)的参数,以及一些使花哨标签正确排列的参数.此解决方案在我的30x30桌子上效果很好.
导入 matplotlib、numpy导入matplotlib.pyplot作为plt宽度=5高度= 3col_width=.075数据=numpy.array([[1, 2,5],[3,4,7],[7,9,5]])标题= [长标题1",长标题2",长3"]fig=plt.figure(figsize=(width,height))ax = fig.add_subplot(111,frameon = False,xticks = [],yticks = [])the_table = plt.table(cellText = data,rowLabels = headings,colWidths = [col_width] * data.shape [1],loc ='center')#删除colLabelsthe_table.auto_set_font_size(False)the_table.set_fontsize(10)the_table.scale(1, 1.6)#custom标题标题-新部分hoffset=0.42 #从试错中找到这个数字voffset = 0.66#通过反复试验找到此数字line_fac = 0.98#控制分隔线的长度计数=0对于标题中的字符串:ax.annotate(''+ string,xy =(hoffset + count * col_width,voffset),xycoords='轴分数', ha='left', va='bottom',旋转= 45,尺寸= 10)#添加分割线ax.annotate('', xy=(hoffset+(count+0.5)*col_width,voffset),xytext =(hoffset +(count + 0.5)* col_width + line_fac/width,voffset + line_fac/height),xycoords='轴分数', arrowprops={'arrowstyle':'-'})计数+=1plt.show()
I'm creating a table in matplotlib, but the table headers are long strings, and the table values are numbers with only a few digits. This leaves me with two bad options: either my table is much wider than necessary, or my headers overlap. To fix this, I'd like to rotate the table headings (possibly up to 90 degrees). In other words, I want to do this in python
Here's my simplified code right now:
import matplotlib, numpy
import matplotlib.pyplot as plt
data=numpy.array([[1, 2],[3,4]])
headings=['long heading 1', 'long heading 2']
fig=plt.figure(figsize=(5,2))
ax=fig.add_subplot(111, frameon=False, xticks=[], yticks=[])
the_table = plt.table(cellText=data, rowLabels=headings, colLabels=headings, colWidths=[0.3]*data.shape[1], loc='center') #0.3 for second image, 0.03 for first
#the_table.auto_set_font_size(False) #comment out for second image
#the_table.set_fontsize(10) #comment out for second image
the_table.scale(1, 1.6)
plt.show()
This produces either the squished image, or the super-wide image (both shown below). In my actual code, the table is ~30 x 30, so the cells can't be very wide. Does anybody know how to rotate the column titles to fix this spacing issue?
I figured it out. It's not pretty, but it works. I added two annotations for each column - the text, and a line to separate it from the next column header. I had to define some parameters that apply to the table and the fancy labels (width, height, col_width), and some parameters to make the fancy labels line up correctly. This solution worked fine on my 30x30 table.
import matplotlib, numpy
import matplotlib.pyplot as plt
width=5
height=3
col_width=.075
data=numpy.array([[1, 2,5],[3,4,7],[7,9,5]])
headings=['long heading 1', 'long heading 2', 'longish 3']
fig=plt.figure(figsize=(width,height))
ax=fig.add_subplot(111, frameon=False, xticks=[], yticks=[])
the_table = plt.table(cellText=data, rowLabels=headings,
colWidths=[col_width]*data.shape[1], loc='center') #remove colLabels
the_table.auto_set_font_size(False)
the_table.set_fontsize(10)
the_table.scale(1, 1.6)
#custom heading titles - new portion
hoffset=0.42 #find this number from trial and error
voffset=0.66 #find this number from trial and error
line_fac=0.98 #controls the length of the dividing line
count=0
for string in headings:
ax.annotate(' '+string, xy=(hoffset+count*col_width,voffset),
xycoords='axes fraction', ha='left', va='bottom',
rotation=45, size=10)
#add a dividing line
ax.annotate('', xy=(hoffset+(count+0.5)*col_width,voffset),
xytext=(hoffset+(count+0.5)*col_width+line_fac/width,voffset+line_fac/height),
xycoords='axes fraction', arrowprops={'arrowstyle':'-'})
count+=1
plt.show()
这篇关于如何旋转pyplot.table中的列标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!