Matplotlib 表格 Y 轴对齐图表 [英] Matplotlib Table Y-axis Alignment to Chart

查看:41
本文介绍了Matplotlib 表格 Y 轴对齐图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上看到了很多带有图表的matplotlib表的x轴对齐示例,但是我无法弄清楚如何使我的表y轴对齐.下面的代码生成了它后面的表格.

I've seen plenty of examples online of x-axis alignment for matplotlib Table with charts, but I can't figure out how to y-axis align my table. The following code produces the table pictured after it.

# 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() 使其尽可能接近,但我无法使行与条形对齐,因为表格占据了第一行.

As you can see, despite me manually playing with the_table.scale() to get it as close as possible, I can't get the rows aligned with my bars, since the column headers of the table are taking up the first row.

任何帮助表示赞赏.谢谢.

Any help appreciated. Thanks.

推荐答案

以下内容会将表格放入一个比条形数 n 大 n 分之一的边界框中.然后,需要确保轴内的边距以及子图参数正确.下面只调整垂直方向(水平方向较长的标签需要手动调整).

The following would put the table into a bounding box that is one nth larger than the number of bars n. One then needs to make sure the margins within the axes are correct as well as the subplot parameter. The below only adjusts the vertical direction (the horizontal direction needs to be done by hand for longer labels).

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randint(1,999,size=(10,4))
col = list("ABCD")
row = np.arange(1, len(data)+1)

n = len(data)   # number of bars
barwidth = 0.8  # bar width
t = 0.05
b = 0.125

# Plot
fig, ax = plt.subplots()
fig.subplots_adjust(left=0.4, top=1-t-(1-t-b)/(n+1))

ax.margins(y=(1-barwidth)/2/n)

ax.barh(row, data[:,0], height=barwidth, label="bars", )

the_table = ax.table(cellText=data,
                      rowLabels=row,
                      colLabels=col,
                      cellLoc='center',
                      bbox=(-0.6, 0.0, 0.6, (n+1) / n))

the_table.auto_set_font_size(False)
the_table.set_fontsize(9)
fig.canvas.draw()   # need to draw the figure twice

plt.yticks([])
plt.legend()
plt.show()

这篇关于Matplotlib 表格 Y 轴对齐图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆