【问题标题】:MATLAB : bar graph_ modify parameters on axisMATLAB:条形图_修改轴上的参数
【发布时间】:2015-03-12 14:00:21
【问题描述】:

我有一个包含 2 个不同数据的条形图(如图所示,每个数据都有不同的颜色)。我想移动 x 轴,使其在 y=-100 处相交(例如)。所以如果一个数据 = -40,我想要一个从 -100 到 -40 的条形图。

另一个问题:是否可以垂直写入 x 轴的每个值(因为所有值,我们什么都看不到)。

最后一个问题:x_axis 是否可以有 2 个不同的比例?

提前谢谢你,

最好的问候,

【问题讨论】:

    标签: matlab bar-chart


    【解决方案1】:

    这里有一些代码可以帮助你。一切都被注释了,所以应该很容易理解:

    clear
    clc
    close all
    
    %// Generate dummy data
    y = -90*rand(1,20);
    NumY = numel(y);
    
    HalfData = round(NumY/2);
    
    %// Loop to color half in pink and half in blue
    hold all
    for k = 1:NumY
    
        hBar = bar(k,y(k));
    
        if k <= HalfData
            set(hBar,'FaceColor',[1 0 1])
    
        else
            set(hBar,'FaceColor',[0 0 1])
    
        end
    end
    hold off
    
    %// Get xtick labels and position for future use
    xtLabels = cellstr(get(gca,'XTickLabel')).';
    xtPos = get(gca,'XTick');
    
    %// Change baseline value
    set(hBar,'BaseValue',-40)
    
    %// Get baseline to change its properties if you want
    hBaseL = get(hBar,'Baseline');
    
    set(hBaseL,'LineStyle','--','Color','k','LineWidth',3)
    
    %// Adjust axis limits. Remove labels to place them vertically
    set(gca,'XLim',[0 NumY],'XTickLabel',{''})
    
    %// Get correct position for xlabel text
    YLimPoArrays = min(get(gca,'YLim'));
    
    YLimPoArrays = repmat(YLimPoArrays,numel(xtPos),1);
    
    %// Place text positioned vertically with a small y offset
    text(xtPos,YLimPoArrays-3,xtLabels,'HorizontalAlignment','center','Rotation',90,'FontSize',15)
    

    还有输出:

    希望有帮助!

    【讨论】:

    • 非常感谢!一切都很完美!
    • 太棒了!如果这有帮助,您可以将答案标记为已接受吗? (旁边的绿色复选标记)。谢谢!
    猜你喜欢
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多