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()
均值和错误为数据帧,如下所示:
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是
['1', '2', '3', '4','5']
我想添加标签的第二层,如下所示:
['Type 1': {'1','2','3'}, 'Type 2':{'4','5'}]
在图上以两个级别显示:
1 2 3 4 5
Type 1 Type 2
关于如何调整我的代码来做到这一点的任何想法?我不想调整数据框。 谢谢
As per this question (very similar) Two level grouped plots in pandas the feature in matplotlib is still in wishlist.
Here an example of a workaround: https://stackoverflow.com/a/19242176/3805553