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

 채택된 답변

Voss
Voss 2023년 7월 23일
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

Mika
Mika 2023년 7월 25일
It worked. Thank you!
Voss
Voss 2023년 7월 25일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

태그

질문:

2023년 7월 23일

댓글:

2023년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by