1

I created the following function to draw plots. I am trying to add a second layer of xticks on this plot. I checked similar questions like this, but this method changes the dataframe in order to add a second layer of labels. I don't want to do it since I get the data from a very large data set and it is hard to adjust the data for drawing every single plot. I am looking for a more automated approach.

def plotFun(means,errors,x_Names,x_label,pngPath, x_len=8, y_len=5):
        fig, ax = plt.subplots(figsize=(x_len,y_len))
        ax.set_yscale('log')
        xSize=len(means.index)
        means.plot.bar(yerr=errors, ax=ax, capsize=3, width = 0.65 ,edgecolor='black')
        plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
        plt.tight_layout()
        plt.xlabel(x_label)
        plt.xticks(np.arange(xSize),x_Names , rotation=0)
        plt.savefig(pngPath, dpi=300, bbox_inches='tight', transparent=True)
        plt.show()

means and errors are dataframes as follows:

File    A         B             C               D
1       2225.0    45981.6       1.301418e+06    48125.0
2       21751.0   28198700.0    1.202896e+09    3822348.0
3       62682.6   232038400.0   1.895988e+10    29267700.0
4       15966.0   2924685.0     2.650770e+08    2712005.0
5       112895.5  604058.5      2.117455e+06    53895.0

X_Names is

['1', '2', '3', '4','5']

My plot is like this now enter image description here

I'd like to add a second layer of the labels as follows:

['Type 1': {'1','2','3'}, 'Type 2':{'4','5'}]

Show on the plot in two levels like:

1   2   3      4   5  

 Type 1       Type 2

Any ideas on how to adjust my code to do that? I don't want to adjust the dataframe. Thanks

Sarah
  • 133
  • 11

1 Answers1

0

As per this question (very similar) Two level grouped plots in pandas the feature in matplotlib is still in wishlist.

Fabio Veronese
  • 7,726
  • 2
  • 18
  • 27