【发布时间】:2019-07-09 22:56:44
【问题描述】:
我正在努力正确左/右对齐文本,而不会浪费列中的空间。中心/左/右使用相同的列宽:
- Center aligned: 正确居中,ok
- Left aligned: 左边浪费了空间
- 即使省略 colWidths 作为表参数并使用我最喜欢的解决方案 the_table._autoColumns = range (-1,len (colLabels)) 也不能改善这种情况
我做错了什么?这是 matplotlib 中的错误吗?
最好的问候, 勒内
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()
【问题讨论】:
标签: python matplotlib