表格中的 Matplotlib 文本对齐 [英] Matplotlib Text Alignment in Table
问题描述
我正在努力使文本正确地向左/向右对齐,而不会浪费列中的空间.中心/左/右使用相同的列宽:
要克服此问题,需要将相关列的填充设置为较低的值,因为对于所有其他列,实际上需要10%.没有内置的选项可以设置填充,但是可以通过在列的单元格上循环并更改相应的
PAD
属性来对其进行操作.def set_pad_for_column(col,pad = 0.1):单元格 = the_table.get_celld()列= [如果cell [1] == col,则the_table.get_celld()中单元格的单元格]对于列中的单元格:cells [cell] .PAD = padset_pad_for_column(col = 1,pad = 0.01)
这仅对第二列(
col = 1
)产生1%的填充.I am struggling to left/right align text properly without having wasted space in the columns. The same col widths are being used for center/left/right:
- Center aligned: properly centered, ok
- Left aligned: wasted space on the left
- Even with omitting colWidths as table argument and playing around with my favorite solution the_table._autoColumns = range (-1,len (colLabels)) does not improve the situation
What am I doing wrong? Is this a bug in matplotlib?
Best Regards, René
import pandas as pd import matplotlib.pyplot as plt size_x, size_y = 12, 4 fig = plt.figure (figsize=(size_x, size_y)) ax = fig.add_subplot(111) col_ID = pd.Series ([ "R001", "R002", "R003", "R005", "R006", "R007" ]) col_Title = pd.Series ([ 50*"*", 10*"-", 70*"x", "R005", "R006", "R007" ]) col_x = pd.Series ([ 3, 1, 3, 4, 2, 3 ]) col_y = pd.Series ([ 4, 2, 3, 2, 4, 3 ]) legend_df = pd.DataFrame ({ "ID" : col_ID, "Title" : col_Title, "X-Pos" : col_x, "Y-Pos" : col_y, "Value" : col_x * col_y }) rowLabels = legend_df.index colLabels = legend_df.columns cellText = legend_df.values # Center-Aligned text looks ok the_table = plt.table (cellText=cellText, rowLabels=rowLabels, colLabels=colLabels, loc='upper center', cellLoc="center", colWidths=[0.05, 0.52, 0.05, 0.05, 0.05, 0.05]) # Bogus: Same colWidths, but left OR right aligned -> unwanted space in title column # the_table = ax.table (cellText=cellText, rowLabels=rowLabels, colLabels=colLabels, loc='upper center', cellLoc="left", colWidths=[0.05, 0.52, 0.05, 0.05, 0.05, 0.05]) the_table.auto_set_font_size(False) the_table.set_fontsize (8) # Set col width automatically # the_table._autoColumns = range (-1,len (colLabels)) ax.xaxis.set_visible (False) # Grafik-Achsen ausschalten, wir wollen nur Plot ax.yaxis.set_visible (False) # the_table.scale(2, 2) plt.show()
解决方案Per default the cells have a 10% padding to both sides, which is useful to let the text start a bit next to the table cell border. For very large cells 10% is then too much and leads to the undesired large space.
To overcome this, one would need to set the padding for the column in question to a lower value, as for all other columns 10% is actually desireable. There is no built-in option to set the padding, but one may manipulate it by looping over the cells of the column and changing the respective
PAD
attribute.def set_pad_for_column(col, pad=0.1): cells = the_table.get_celld() column = [cell for cell in the_table.get_celld() if cell[1] == col] for cell in column: cells[cell].PAD = pad set_pad_for_column(col=1, pad=0.01)
This produces a padding of 1% only for the second column (
col=1
).这篇关于表格中的 Matplotlib 文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!