How to add a shared x-label and y-label to a plot created with pandas' plot?

后端 未结 3 2110
旧时难觅i
旧时难觅i 2021-02-07 20:25

One can create subplots easily from a dataframe using pandas:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({\'A\': [0.3, 0.2, 0.5, 0.2]         


        
3条回答
  •  你的背包
    2021-02-07 21:05

    Another solution: create a big subplot and then set the common labels. Here is what I got.

    The source code is below.

    import pandas as pd
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    axarr = fig.add_subplot(221)   
    
    df = pd.DataFrame({'A': [0.3, 0.2, 0.5, 0.2], 'B': [0.1, 0.0, 0.3, 0.1], 'C': [0.2, 0.5, 0.0, 0.7], 'D': [0.6, 0.3, 0.4, 0.6]}, index=list('abcd'))
    axes = df.plot(kind="bar", ax=axarr, subplots=True, layout=(2, 2), sharey=True, sharex=True, rot=0, fontsize=20)
    
    # Create a big subplot
    ax = fig.add_subplot(111, frameon=False)
    # hide tick and tick label of the big axes
    plt.tick_params(labelcolor='none', top='off', bottom='off', left='off', right='off')
    
    ax.set_xlabel('my_general_xlabel', labelpad=10) # Use argument `labelpad` to move label downwards.
    ax.set_ylabel('my_general_ylabel', labelpad=20)
    
    plt.show()
    

提交回复
热议问题