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

后端 未结 3 2108
旧时难觅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:00

    This will create an invisible 111 axis where you can set the general x and y labels:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    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'))
    ax = df.plot(kind="bar", subplots=True, layout=(2, 2), sharey=True,
                 sharex=True, rot=0, fontsize=12)
    
    fig = ax[0][0].get_figure()  # getting the figure
    ax0 = fig.add_subplot(111, frame_on=False)   # creating a single axes
    ax0.set_xticks([])
    ax0.set_yticks([])
    ax0.set_xlabel('my_general_xlabel', labelpad=25)
    ax0.set_ylabel('my_general_ylabel', labelpad=45)
    
    # Part of a follow up question: Modifying the fontsize of the titles:
    for i,axi in np.ndenumerate(ax):
        axi.set_title(axi.get_title(),{'size' : 16})
    

提交回复
热议问题