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

 Akzeptierte Antwort

Voss
Voss am 23 Jul. 2023
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);

2 Kommentare

Mika
Mika am 25 Jul. 2023
It worked. Thank you!
Voss
Voss am 25 Jul. 2023
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 23 Jul. 2023

Kommentiert:

am 25 Jul. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by