importmatplotlib.pyplotaspltplt.rcdefaults()importnumpyasnpimportmatplotlib.pyplotaspltobjects=('Python','C++','Java','Perl','Scala','Lisp')y_pos=np.arange(len(objects))performance=[10,8,6,-4,2,1]plt.bar(y_pos,performance,align='center',alpha=0.5)# Get the axes objectax=plt.gca()# remove the existing ticklabelsax.set_xticklabels([])# remove the extra tick on the negative barax.set_xticks([idxfor(idx,x)inenumerate(performance)ifx>0])ax.spines["bottom"].set_position(("data",0))ax.spines["top"].set_visible(False)ax.spines["right"].set_visible(False)# placing each of the x-axis labels individuallylabel_offset=0.5forlanguage,(x_position,y_position)inzip(objects,enumerate(performance)):ify_position>0:label_y=-label_offsetelse:label_y=y_position-label_offsetax.text(x_position,label_y,language,ha="center",va="top")# Placing the x-axis label, note the transformation into `Axes` co-ordinates# previously data co-ordinates for the x ticklabelsax.text(0.5,-0.05,"Usage",ha="center",va="top",transform=ax.transAxes)plt.show()
python 条形图 负值_Matplotlib条形图x轴以下的负值
最新推荐文章于 2024-04-26 11:51:32 发布