How do I change colors for values above zero?

Hey,
I have some trouble changing the color of my bars with values bigger than zero. Can anybody help me please?
My script so far:
%%
clear, clc, close all
%% LOAD
load data.txt
data = load('data.txt')
%% MATRIX
M = readmatrix('data.txt')
x = M(:,1)
y = M(:,2)
%% PLOT
hold on
bar (x,y)
hold off

 Accepted Answer

new_color = [1 0 0]; % red
M = [(1:10).' randn(10,1)]; % random data
x = M(:,1);
y = M(:,2);
h = bar(x,y);
positive_bar = y > 0;
h.FaceColor = 'flat';
h.CData(positive_bar, :) = repmat(new_color,nnz(positive_bar),1);

More Answers (0)

Categories

Asked:

on 23 Jul 2023

Commented:

on 25 Jul 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!