Matplotlib表:如何设置垂直对齐方式?

时间:2020-06-03 05:54:45

标签: python pandas matplotlib

我的目标是将以下数据帧转换为image。我关注了这里的讨论:enter link description here

df = ma_dataframe_finale_df.reset_index().rename(columns={'index':'Pays'})

    Pays      Ratio   Confirmés   Guéris     En traitement    Décès
0   Eritrea    1.00     39          39            0            0
1   Seychelles 1.00     11          11            0            0
2   Mauritius  0.93    335         322            13           10
3   Tunisia    0.85   1084         964            120          48
4   Niger      0.83   958         844             114          65

我添加了以下代码:

import six
import matplotlib.pyplot as plt
import numpy as np

def render_mpl_table(data, col_width=3.0, row_height=0.625, font_size=14,
                     header_color='#40466e', row_colors=['#f1f1f2', 'w'], edge_color='w',
                     bbox=[0, 0, 1, 1], header_columns=0,
                     ax=None, **kwargs):
    if ax is None:
        size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
        fig, ax = plt.subplots(figsize=size)
        ax.axis('off')

    mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, **kwargs)

    mpl_table.auto_set_font_size(False)
    mpl_table.set_fontsize(font_size)

    for k, cell in six.iteritems(mpl_table._cells):
        cell.set_edgecolor(edge_color)
        if k[0] == 0 or k[1] < header_columns:
            cell.set_text_props(weight='bold', color='w')
            cell.set_facecolor(header_color)
        else:
            cell.set_facecolor(row_colors[k[0]%len(row_colors) ])
    return ax

结果是:

enter image description here

但是您可以看到它削减了赤道几内亚这样的国家名称。所以我想问问是否有可能在jupyter中垂直对齐数据框并在jpeg中将其转换?

0 个答案:

没有答案