我在网上看到了很多带有图表的matplotlib Table的x轴对齐示例,但是我无法弄清楚如何使我的表y轴对齐。下面的代码将生成下表所示的表格。
# For minimum viable example
dfx = [['ARS FX', 0.025346713729, 0.028238, 0.021889, 0.07701426, 0.0, 35, 39, '14.7%', 0.0, 0.07701426], ['BRL FX',1.83316130513e-05,0.025746,-0.072473, 0.143642325, 0.0, 40, 45, '12.3%', 0.0, 0.143642325], ['EUR FX', -0.301254060209, -0.300762, -0.290554, 0.0, -0.30127866, -60, -40, '5.2%', -0.30127866, 0.0], ['ZAR FX', 0.0515621470331, 0.053191, 0.044245, 0.07344438, 0.0, 10, 29, '14.1%', 0.0, 0.07344438], ['AR Eqity', 3.68762762118e-06, 0.0,0.0, 0.08817912, 0.0, 45, 45, '23.9%', 0.0, 0.08817912]]
dfx = pd.DataFrame(dfx)
dfx.columns = ['IdeaName', 'ModelWeight', 'Exposure_FXA', 'Exposure','Adj_Ubound', 'Adj_Lbound', 'lt_rob', 'st_rob', 'implied_vol', 'Lower Bound', 'Upper Bound']
# Plot
_, ax = plt.subplots()
dfx[['Lower Bound']].plot(kind='barh',ax=ax,color='red')
dfx[['Upper Bound']].plot(kind='barh',ax=ax,color='green')
plt.plot(dfx['ModelWeight'],range(len(dfx)), linestyle="", markersize=5, marker="o", color="#ff6600", label="ModelWeight", markeredgecolor="k")
plt.plot(dfx['Exposure'],range(len(dfx)), linestyle="", markersize=5, marker="o", color='lightblue', label="Exposure", markeredgecolor="k")
# Add a table at the bottom of the axes
columns = ['LT','ST','Vol']
the_table = ax.table(cellText=dfx[['lt_rob','st_rob','implied_vol']].values,
rowLabels=list(dfx['IdeaName']),
# rowColours=colors,
colWidths=[0.1 for x in columns],
colLabels=columns,
cellLoc='center',
loc=15)
the_table.auto_set_font_size(False)
the_table.set_fontsize(9)
the_table.scale(1, 1.05)
plt.subplots_adjust(left=0.4)
plt.yticks([])
plt.legend()
plt.show()
如您所见,尽管我手动使用the_table.scale()
使其尽可能接近,但由于表的列标题占据了第一个,所以我无法使行与条形对齐行。
任何帮助表示赞赏。谢谢。
答案 0 :(得分:2)
以下内容会将表格放入一个边框,该边框比条形数n大n分之一。然后,需要确保轴内的边距以及子图参数正确。下面仅调整垂直方向(对于较长的标签,水平方向需要手动完成)。
{
"person-test": {
"aliases": {},
"mappings": {
"person": {
"properties": {
"name": {
"type": "text",
"store": true,
"fields": {
"forSort": {
"type": "keyword",
"normalizer": "lowercase"
}
}
}
}
}
},
"settings": {
"index": {
"number_of_shards": "5",
"provided_name": "person-test",
"creation_date": "1563903601344",
"analysis": {
"normalizer": {
"lowercase": {
"filter": [
"lowercase"
],
"type": "custom",
"char_filter": []
}
}
},
"number_of_replicas": "1",
"uuid": "WhSFtV-xQ8acXxX6R0gLyg",
"version": {
"created": "6070299"
}
}
}
}
}