Plot table alongside sns.barplot

前端 未结 1 1386
情歌与酒
情歌与酒 2021-01-19 17:26
#read data into dataframe
con=pd.read_csv(\'testOutput.csv\')

\'\'\'
testOutput.csv looks like :
Sample,Count
sample1,99.9
sample2, 96.6
\'\'\'


## set colours to          


        
相关标签:
1条回答
  • 2021-01-19 17:54

    In principle, the same strategies than in this question's answer apply here.

    Using bbox

    You may freely position the table on your graph using the bbox argument, bbox = [left, bottom, width, height] where left, bottom, width, height are fractions of the axes.

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    df = pd.DataFrame({"count" : np.sort(np.random.rand(100))*100})
    
    summary=pd.DataFrame(df['count'].describe())
    
    plt.barh(range(len(df)),df["count"])
    
    plt.table(cellText=summary.values,
              rowLabels=summary.index,
              colLabels=summary.columns,
              cellLoc = 'right', rowLoc = 'center',
              loc='right', bbox=[.65,.05,.3,.5])
    
    plt.savefig("out.png")
    plt.show()
    

    This would also allow to put the table outside the axes by choosing the paramters larger than 1. In order to make space for the table, you can then shrink the subplots, plt.subplots_adjust(right=0.75).

    plt.table(cellText=summary.values,
              rowLabels=summary.index,
              colLabels=summary.columns,
              cellLoc = 'right', rowLoc = 'center',
              loc='right', bbox=[1,.3,.3,.5])
    
    plt.subplots_adjust(right=0.75)
    

    Using dedicated subplot

    You may use a dedicated subplot to put the table in.

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    df = pd.DataFrame({"count" : np.sort(np.random.rand(100))*100})
    
    summary=pd.DataFrame(df['count'].describe())
    
    fig,(ax,tabax) = plt.subplots(ncols=2, gridspec_kw=dict(width_ratios=[2,1]))
    ax.barh(range(len(df)),df["count"])
    
    tabax.axis("off")
    tabax.table(cellText=summary.values,
              rowLabels=summary.index,
              colLabels=summary.columns,
              cellLoc = 'right', rowLoc = 'center',
              loc='center')
    
    plt.savefig("out.png")
    plt.show()
    

    0 讨论(0)
提交回复
热议问题