使用MATLAB 畫柱狀圖 ,即bar (x,y),其橫坐標是默認 1、2、3、4、……的
% --v1 y1=[asum1,asum2,asum3,asum4,asum5,asum6,asum7,asum8,asum9,asum10]; x1=[1:10]; bar(x1,y1)
現在需要修改橫坐標名稱,使用命令:
% --v2 y2=[asum1,asum2,asum3,asum4,asum5,asum6,asum7,asum8,asum9,asum10]; x2=['<=10','10-25','25-50','50-90','90-300','300-350','350-450','450-550','550-700','>700']; bar(y2) %先 bar 后 set set(gca,'XTickLabel',x2)
但是顯示的時候出現文字重疊情況……
那么,更進一步的,使其名稱橫着顯示:
% --v3 y3=[asum1,asum2,asum3,asum4,asum5,asum6,asum7,asum8,asum9,asum10]; x3str={'<=10','10-25','25-50','50-90','90-300','300-350','350-450','450-550','550-700','>700'}; %新坐標的值 bar(y3) %先 bar 后 set xtb = get(gca,'XTickLabel');% 獲取橫坐標軸標簽句柄 xt = get(gca,'XTick');% 獲取橫坐標軸刻度句柄 yt = get(gca,'YTick'); % 獲取縱坐標軸刻度句柄 xtextp=xt;%每個標簽放置位置的橫坐標,這個自然應該和原來的一樣了。 ytextp=-0.1*yt(3)*ones(1,length(xt)); text(xtextp,ytextp,x3str,'HorizontalAlignment','right','rotation',46) set(gca,'XTickLabel',[]); %將原坐標(1,2,3,..)去掉
最終,加上 label :
%--添加坐標提示 xlabel('Rating Counts'); ylabel('Number of Ratings'); legend('FilmTrust');
設置 xlabel在右邊而非中間:
(最終版代碼)
% --v3 y3=[asum1,asum2,asum3,asum4,asum5,asum6,asum7,asum8,asum9,asum10]; x3str={'<=10','10-25','25-50','50-90','90-300','300-350','350-450','450-550','550-700','>700'}; %新坐標的值 bar(y3) %先 bar 后 set xtb = get(gca,'XTickLabel');% 獲取橫坐標軸標簽句柄 xt = get(gca,'XTick');% 獲取橫坐標軸刻度句柄 yt = get(gca,'YTick'); % 獲取縱坐標軸刻度句柄 xtextp=xt;%每個標簽放置位置的橫坐標,這個自然應該和原來的一樣了。 ytextp=-0.1*yt(3)*ones(1,length(xt)); text(xtextp,ytextp,x3str,'HorizontalAlignment','right','rotation',46) set(gca,'XTickLabel',[]); %將原坐標(1,2,3,..)去掉 %--添加坐標提示 xlabel('Rating Counts'); ylabel('Number of Ratings'); legend('FilmTrust'); % ----設置 xlabel在右邊而非中間 h=xlabel('Rating Counts'); xlim = get(gca,'XLim'); ylim = get(gca,'YLim'); set(h,'Position',[xlim(2)+(xlim(2)-xlim(1))*0.05,ylim(1)])