Search code examples
pandasmatplotlibreverse

pyplot Reverse x axis and reverse table subplot


When I reverse the x axis in pyplot the accompanying table does not get effected. How can i reverse the display of the table elements?

ax=ep.plot(kind='bar',table=True).invert_xaxis()

enter image description here


Solution

  • This is how I would do it ...

    import pandas as pd
    import matplotlib.pyplot as plt 
    
    # get the data ... note the order ...
    data = [4.77, 5.52, 5.93, 6.29, 6.0, 6.95, 7.7, 9.44, 10.94, 12.35, 13.45]
    df = pd.DataFrame({'data':data})
    
    # referse the dataframe
    df = df[::-1]
    
    # ... and plot ...
    ax = df.plot(kind='bar',table=True, figsize=(8,4)) # bar plots are categorical
    ax.xaxis.set_visible(False) # table heading and x-labels over-printing
    plt.show()
    

    Which yields ...

    chart generated by the above code

    And if we reverse it again ...

    df = df[::-1]
    

    We get ...

    enter image description here